3/16/2018
Introduction to Programming in
COMPUTER AND MATLAB
COMPUTING • M-file Script
• M-file function
MODULE II: Programming in MATLAB • Plotting
BY • Looping
Engr. C. C. Obasi
• Selection
How to Write a Blank Script
M-file Script Click new from list banner
Click here to open the editor banner
items
• external file that contains a logical set of MATLAB commands
1
• This file is stored on the disk with file extension, .M
Type in your script contents
2 here
• After compiling M-file script, it can then be called by typing the file
3
name in the command line.
4
Click script from the drop
down list
3/16/2018
Sample Script Solution to Sample script
Step 1: Analyze the problem to be able to understand the theory
that is:
−2 + 6 − 8 = −2 a. Collect all the coefficients on the left hand into a matrix A
−2 6 −8
4 −2 −2=2 = 4 −2 −2
2 − +3=4 2 −1 3
b. Collect the elements on the right into a matrix b
−1
Find the solution of the set of equation = 2
4
c. equate Ax = b (same as original equations)
Therefore, =
Solution to Sample Script Cont’d Solution to sample script
Step 2: Open new Script from the editor banner
a. Create the matrix of A as follows: Normal matrix form
b. Create the matrix of b as follows
(Column matrix)
−2 6 −8 Normal matrix form −1
= 4 −2 −2 (3x3 matrix) = 2
2 −1 3 4
A = [-2 6 -8; 4 -2 -2; 2 -1 3];
b = [-2; 2; 4];
MATLAB form for
creating Column
MATLAB form for matrix
creating 3x3 matrix
3/16/2018
Solution to Sample Script Solution to Sample Script cont’d
c. Solve for = The final script file should look like this:
In MATLAB, the inverse of a square matrix can be obtained A = [-2 6 -8; 4 -2 -2; 2 -1 3];
using the function inv() b = [-2; 2; 4];
inv(A) * b
Hence the solution of the set of equations is given by
Save the script by clicking save from the editor banner.
inv(A) * b Use xVal.m as the file name.
Note: This will produce a column matrix for the values of x
Solution to Sample Script cont’d Class work
Step 3: Run the m-file script
Type the following commands at the command prompt Find the following for a given set of matrix A and B
1. A x B
>> xVal 2. A + B
x= 3. Determinants of A and B
4. Eigenvalue of A and B
1.4500
1.1500
0.7500
3/16/2018
M-File Function
Final Comment
• External file saved with .m extension like the m-file script
• Does not allow variable overwrite issue that is common
with m-file script
Every problems to be solved must be • Each function has its own workspace
understood. This make the computation • Recommended for complicated application.
easy.
Structure of m-file function Structure of m-file function Cont’d
• Function Definition Line: • Help or Comment Line
Output variable of the function.
Function definition statement More than one output variable can Input variable. More than one
be separated by commas input variable can be separated by % Comment lines begin with ‘%’ sign
commas % Comment line describe the commands or entire
program
function m = y( x ) % Comment lines are not executable, hence they
not compiled.
Function Name (MUST ALSO BE THE
SAME AS THE FILE NAME)
3/16/2018
Structure of m-file function Cont’d Program Flow Control
Function body: •Selection
This part is where the commands that is •Looping (Iteration or Repetition)
required to perform the program routine we
want the function execute is placed.
f = x*cos(x);
g = x^2+3*x+1;
m = sin(f/g);
Selection if … end statement
• If statement • Used to execute a given block of statements or commands when a
• If … else statement given condition is true
• Switch … case statement • Can also be used to cause another set of program statements or
commands execute when a condition evaluates to be false
3/16/2018
Structure of if statement This is usually a Boolean if … else statement Block of commands
expression, which evaluates to Introduction of false condition to execute if
Beginning of the if either zero (false or 0) or one condition is true
statement block (true 1)
if(condition)
if(condition) Block of commands Sequence of commands if true
to execute if else
block of commands condition is true Block of commands
Sequence of commands if false
Here if the to execute if
end condition evaluates end condition is false
to be false or zero
End of the if
(0), nothing
statement block
happens
Mathematical Symbols used to create
Looping
conditions
Mathematical MATLAB Usage example Description
Symbol equivalent •for … end loop
> > a>b a is greater than b
< < a<b a is less than b •While ... End loop
≥ >= a >= b a is greater than or equal to b
≤ <= a <= b a is less than or equal to b
≠ ~= a != b a is not equal b
= == a == b a is equal b
Logical operators
AND & a&b a AND b
OR | a|b a OR b
NOT ~ ~a NOT a
~b NOT b
3/16/2018
For … end loop Range of value that must be counted while … end
before the loop ends. Initial value is
Variable used to count the the fist value to count, the step is the Beginning and end of the while loop
number of time the loop values to skip before counting the next Beginning and end of the while loop
has undergone value, and he ending value is the last
number to count while condition
Beginnin commands
g and for counter = initial value : ending value
end of
statements to repeat
the for .. commands/ end
loop statements to repeat Block of code that execute
while condition remains Block of code to execute while
end true condition remains true
Differences between for … end & while … end
Examples
loops Write a function to display the sum of the first 20 integers
Sn for … end Loop while … end Loop function sum = eqn3()
sum = 0;
1 has boundary (known has no range for(x = 1:20)
range) sum = sum+x;
end
y = ['The sum of first 20 integers is: ', num2str(sum)];
disp(y)
2 ends when the boundary does not end until you end
is reached force it to end with
another condition
The sum of first 20 integers is: 210
3/16/2018
Sum of first 20 integers using while … end
Factorial example
function sum = eqn3()
sum = 0; function f = fact(n)
x = 1; %Display the factorial of a number n
while(x <= 20) %f = prod(1:n);
sum = sum+x;
f = 1;
x = x +1;
end for x = 1:n
y = ['The sum of first 20 integers is: ', num2str(sum)]; f = f * x;
disp(y) end
end
end
Factorial example cont’d
function f = fact2(n)
%Display the factorial of a number n
%f = prod(1:n);
f = 1;
x = 0;
while x < n
x = x+1;
f = f * x;
end
end