Some Matlab basics and extensions to (potentially) more
useful stuff
1. Matlab basics
Type
Comment
a=5*6;
creates a variable 'a' equal to 5 x 6, the semi-colon (;) stops the
result from displaying on the screen.
displays the value of the variable 'a'
disp(a)
does the same as above using the matlab function disp
help disp
gives basic information on the disp function
B=5/4;
B
b=a^B
note matlab is caSe sensitive. The '^' means to the power, a .
whos
displays a list of the current variables in the workspace, their size,
memory size (bytes) and class, this information can be useful when
debugging programs.
clc
this clears the screen, but leaves all the variables in the workspace.
clear a
just clears the variable 'a'
whos
no longer have 'a'
clear
clears all variables in the workspace
whos
no longer have anything
clc
2. In built functions, simple vectors and simple plotting
Type
Comment
x=2; sin(x)
note the argument 'x' to the function sin must be in radians
x=30; sin(pi*x/180)
converting from degrees to radians (2 rads = 360). NB pi
in matlab represents .
if we wanted to plot sin(x) 0<x<2 we could create a number of points individually and plot
each point, or we could create a small m-file with a loop. However matlab has a very much
easier way to make a list of numbers (vector).
x=0:0.1:2*pi;
creates a vector from 0 to 2 in steps of 0.1, it overwrites
the previous value of 'x'.
st
x(1)
displays the 1 value of x, in this case 0.
x(2)
displays the 2
x(end)
displays the last (end) value (which is as close to 2 as
nd
value of x
can be got in steps of 0.1)
length(x)
gives the length of x
x(length(x))
also gives the last value of x
y=sin(x);
the sin and most matlab functions can accept vectors and
matrices as arguments not just single numbers.
plot(x,y)
plots x vs y hopefully the figure appears familiar
help plot
gives some useful information on basic plots
plot(x,cos(x),'ro',x,sin(x),'g+')
creates a plot of cos(x) in red circles and sin(x) in green
pluses, it overwrites the previous data in figure(1).
figure; plot(x,cos(x).*sin(2*x))
opens a new figure window and plots the new curve. NB
the '.*' is important.
help whitebg
whitebg
changes the colour of the figure
whitebg
changes the colour back (the command works as a
toggle)
close all
closes all open figure windows
clear; clc
3. Matrices and simultaneous equations
Matrices are easy to deal with in Matlab (which is short for Matrix Laboratory).
A=rand(4)
creates a random 4 x 4 matrix
B=zeros(4,6)
creates 4 rows and 6 columns of 0's
C=ones(2,8)
creates 2 rows and 8 columns of 1's
D=eye(5)
creates a 5 x 5 identity matrix
A
nd
st
A(1,2)
indexing a matrix this selects the 2
A(4,1)
selects the 1 value in the 4 row
A(3,2)=10
overwrites this particular value in A and makes it equal to 10
whos
notice that in size it gives us how big the matrices are
size(A)
gives us the size of A (compare with the function length)
x=1:5;
creates a vector x of 1 to 5 in steps of 1
x^2
tries to find the square of x you will get an error message saying
st
value in the 1 row
th
matrix must be square.
x.^2
takes the square of each element of x this is what we most often
want to do
x*x
error message
x.*x
this works
x./x
this works also
A^2
works, but gives a very different result to
A.^2
think about matrix multiplication
Simultaneous equations
x+y=3
2x + y = 5
can be written as a matrix equation
1 1 x 3
=
2 1 y 5
A X = B
would like to be able to write X =
. Matlab provides a way to do this
B=[3;5]
note to enter a matrix by hand enter each row with the numbers
A=[1 1;2 1]
separated by a space and end each row with a semi-colon (;).
X=A\B
note this is a backslash not a normal divide sign and is a special
matlab function (type help slash for more info)
B=rand(8,1)
A=rand(8)
X=A\B
This has just solved a random set of 8 simultaneous equations (and
pretty quickly too)
clear; clc
4. Getting data into Matlab
(i) type it into the workspace or an m-file
(ii) load it from a text file
These two are the simplest open the help desk (type 'helpdesk' at the command prompt)
and search for 'Importing data into Matlab' for more options.
5. Polynomials in Matlab
N
General form is y = P(1) X + P(2) X
3
N-1
+ + P(N) X + P(N+1)
So for example y = 4x + 2x + 5x 7
P=[4 2 5 -7];
creates a vector P of the correct form for the example
X=-10:0.01:10;
Y=polyval(P,X);
calls the matlab function polyval, which is why P must take the
form above, otherwise function will not work.
plot(X,Y)
Note find out for yourself about polyfit a matlab function for fitting polynomials
Roots of a polynomial equation
We want to know the values of x that make the equation y = f(x) = 0.
3
For example y = x 6x + 11x 6
p=[1 -6 11 -6];
r=roots(p)
finds the roots of the polynomial described by p
y=polyval(p,r)
evaluates the polynomial at the roots close but not perfect
clear; close all; clc
6. Programming
The following parametric equations describe an ellipse with major axis 2A and minor axis 2B
and centre at the origin.
x = A cos(t)
y= B sin(t)
0 < t < 2
Create some matlab code to plot the ellipse to understand the meaning of A and B.
Method 1: at the command line, time them in one at a time, if you make a mistake you kinda
have to start again.
A=2;
Create a variable A and give it a value of 2
B=3;
t = linspace (0, 2*pi, 200);
Create a vector t using the matlab function linspace between 0
and 2 with 200 points.
x = A*cos(t);
Create variable x (as now have A and t)
y = B*sin(t);
plot(x,y,r)
Plot x vs y make the line red
axis equal
Makes the axis the same size (so a circle will look like a circle)
xlabel(x)
Give the x axis the label x
ylabel(y)
title(ellipse)
Give the figure a title of ellipse
Method 2: writing a script m-file
Select <file> <new> <script>
This will open the editor, write the following in the file note the % are comments to try and
explain what is being done.
Method 3: writing a function file
Select <file> <new> <function>
This will open the editor, with the default structure for a function file in it.
On line 1 - 3 make the following changes
So x, y will be the outputs from this function fplot_ellipse and A, B are the input arguments.
Now from line 4 add the following note the similarity to what we have already done.
note DO NOT use clear in a function file otherwise you clear the input arguments!
Now to run this function you have some choices you can either type something like below at
the command line in matlab
>> fplot_ellipse(2,3);
Or if we were trying to be a little more clever write a script file to do this and some other bits
and bobs as below