Beginning Programming in
MATLAB
MAM1043H
What we know so far?
Can do some basic math (+,-,*,/,^)
Know how to set up arrays/vectors using a
colon operator (x=1:10)
Plot graphs (plot(x,y))
Some basic functions (sin(), log()...)
Is that not all we need??
For the simplest problems, yes!
But... its basically no different to using a
calculator!
More complex calculations require more
steps.
Command line becomes tedious
Key Idea:
Can enter exactly any MATLAB commands in a
script file (called a m-file) and run it from
the command line.
How do we do this?
Create a new m-file: (File\New\M-File)
MATLAB script editor opens.
Type in any valid MATLAB statements.
Must be .m
Save the file (eg. mytest.m)
Run the m-file from the command window
>>mytest
A Very Simple Example
Create the m-file
Execute the m-file
Creating our own functions
Using m-files allows us to create our own
functions.
But... we need to re-interpret what we
think of as functions.
Black Box Functions
Think of functions as a black box that
performs some operation:
The input variable(s) to
the function: x
The function
name: f()
ff
The output variable
of the function: y
y=f(x)
The actual calculation is performed inside
the black box...
The Anatomy of a Function m-file
A function m-file is
identified with
function on the
first line
Save m-file as
f.m
The output variable
of the function: y
The function
name: f()
The input
variable(s) to the
function: x
The calculation of the output variable,
depending on the input variable(s)
Function m-files
Must start with function on the first line.
Must have at least one output variable.
Must have a unique name.
Must save the m-file as the function name.
Must assign a value to the output variable.
We will see some examples soon
Repeating Statements
Often, we want to repeat some operation a
number of times.
Examples you are already familiar with
Series: S = 1+2+3+...N
Factorial: F = 1x2x3x...xN
Taylor Series: sin(x) = x+x3/3! + ...
Newton's method: xn+1=xn -f(xn)/f'(xn)
The for Loop
One of the ways of repeating statements
is by a for loop.
Starting value
Structure:
The indexing variable
forx=Ni:Nf
...
Statements
end;
Ending value
The statements to be
repeated
For Loop Examples
Displays the squares
from 1 to 5
example02.m
example02.m
forn=1:5
forn=1:5
y=n^2;
y=n^2;
disp(y);
disp(y);
end;
end;
Need to keep adding n2
to the sum, stored in S
Calculates the sum of n2
from 1 to 5
Calculates 5!
example03.m
example03.m
example04.m
example04.m
N=5;
N=5;
S=0;
S=0;
N=5;
N=5;
F=1;
F=1;
forn=1:N
forn=1:N
S=S+n^2;
S=S+n^2;
end;
end;
forn=1:N
forn=1:N
F=F*n;
F=F*n;
end;
end;
disp('SUM=');
disp('SUM=');
disp(S)
disp(S)
Making a Factorial Function
Function to calculate N!
But is there is a problem with this?
What about 0! ??
The Fibonacci Sequence
Let us calculate the Fibonacci numbers:
[0,1,1,2,3,5,8,13,21, ]
The each number in the sequence is
defined as the sum of the previous two
numbers:
fk = fk-1 + fk-2
The Fibonacci Sequence
We will store the numbers in a vector f
We also know the first two elements:
f=[0 1];
So we know terms 1 and 2, we need to
start calculating from the 3rd term.
In MATLAB, we would use:
f(k)=f(k-1)+f(k-2);
The kth term of the vector
The Fibonacci Sequence
Still to come
How can we solve linear system of
equations?
Making decisions and logical operations.
Numerically solving differential equations.