Matlab Exercises
Part 1
version 5, EJP, 2013
1.
Start matlab.
2.
Enter the following
5.
a = [1 2 3]
b = [4 5 6]
Z = a + i*b
1+2
x=1+2
x = 1 + 2;
y = x^2 + 2*x + 8
3.
i here is the square root of -1.
You can use j instead if that is what
you are use to.
Enter the following
6.
Display the real and imaginary
parts of Z and the conjugate of Z.
7.
Display the magnitude and angle
of Z.
8.
Try the following.
format long e
pi
You can use the arrow keys and the
delete key to recall and edit previous
commands. Press the up arrow key
twice to recall the format command
and delete the "e" and press enter.
Then display pi again. Repeat with the
following formats.
Z'
Z.'
format short e
format short
4.
Enter the following
Enter the following
a = pi/6
sin(a)^2 + cos(a)^2
exp(2*log(3) + 3*log(2))
9.
Enter the two matrices
1 2
A
3 4
12. It is just as easy to solve a
hundred simultaneous equations in a
hundred variables. First create a 100
by 100 matrix of random numbers.
5 6
B
7 8
A1 = rand(100);
10. Try the following and ensure
you can follow what is happening.
If you forget to put in the semicolon,
10,000 numbers will be printed out.
A+5
A+B
A-B
A*B
A^2
A'
Next create a column vector with 100
numbers
b = (1:100)'
Now solve
11. Solve the simultaneous equation
on page 15 of the notes. A should
already be present from exercise 10.
x = A1 \ b
Check that the solution is correct.
b = [ 5 ; 11 ]
x=A\b
A1 * x
Now check that x is the correct
solution.
A*x
Part 2
1.
You should have the two matrices
1 2
A
3 4
4. Plot the polynomial 4 x 3 3 x
using the function polyval. First find
out how to use polyval using the help.
5 6
B
7 8
doc polyval
from exercise 10 in part 1.
If not, then enter them again.
p = [4 0 -3 0]
y1 = polyval(p,x);
2. Now try the following array
operations.
hold on
plot(x,y1,'g')
A .* B
A ./ B
A .^ B
sin(A * pi/6)
D = A.^2
sqrt(D)
5. There are many functions that
handle polynomials. Look them up in
the help. Enter doc polyval again,
then click on polynomials on the path
at the top of the page.
What does the function roots do?
Clear the workspace of all variables.
clear
3.
6. Plot the roots of the polynomial
onto the graph.
Plot the polynomial 2 x 3 x
r = roots(p)
ry = zeros(3,1)
x = linspace(-1,1,100);
y = 2*x.^3 -x;
The plot should still be held from
exercise 4.
What happens if you don't include
the dot ?
plot(r,ry,'rx')
plot(x,y)
Clear the figure
clf
Don't close the figure containing the
plot.
7. Enter the following.
All variables should have been saved
to matlab.mat . If you can't see this in
the "Current Folder" window, right
click in the window and select refresh.
a = 2: 0.5 : 4
a(2)
a([2 4])
a(2:4)
a(2:end)
The workspace window should be
empty. Double click on matlab.mat to
restore all your variables.
11. Produce and a script called graph.
8.
Enter the following
edit graph
w = (1:5)' * (5:10)
In graph enter
This produce a 5 by 6 matrix that we
can use in the next exercise.
x = linspace(-2*pi,2*pi,100);
y = sin(x);
plot(x,y)
grid
Set the third element on the second
row of w to 100.
9.
Enter the following
Save by clicking on the icon
and run by entering
w(2:4,2:4)
w(2,:)
w(:,5)
w([1 5],[2,4])
w(:)
w(:) = 1:30
graph
in the command window.
12. Add the following at the end of the
script created above.
10. By now you should have a nice
collection of variables. Try
hold on
y1 = mysin(x);
plot(x,y1,'r:')
axis( [-2*pi,2*pi,-2,2] )
who
whos
You will also be able to see your
variables in the workspace window.
Click on the "Save and Run" icon
Enter the following in the command
window.
save
clear
13.
You should have a file called mysqrt.m. Edit this file
edit mysqrt
function x=mysqrt(A)
% My square root function.
%
%
x = mysqrt(A)
%
% x is the square root of A.
x=1;
err=1;
%First guess
%Set error to something to get started
while(err>0.0001)
x = myfunction(A,x);
err = abs(A -x.^2);
end
% call to local function below
% calculate the error
function y = myfunction(A,x)
% Local function to calculate the
% Newton Raphson Iteration equation
y = (x.^2+A)./(2*x);
Test the function by enter the following into the command window.
mysqrt(9)
mysqrt(2)
help mysqrt
14.
You are now going to use the debugger on the function above.
In addition to the editor, you need to be able to see the workspace window. If you
cannot easily see the workspace window, click on the HOME tab and then click on
Layout in the ENVIROMENT section and select Default. Then maximise the
MATLAB window. Then click on the EDITOR tab.
In the editor, find the line containing x = 1; Between the line number and the line,
you will find a "-" sign on its own vertical bar. Clicking on this sign turns it into a red
dot. This is a break point. A break point is where the program will stop so that you
can debug the program. You can toggle the break point on and off by clicking on it.
With a break point set on the line noted above, run the program.
mysqrt(2)
The program will stop on the line with the breakpoint. A green arrow indicates the
next line to run. Note that the "Workspace" window is showing variables available in
the program. You can use the variable editor or the command line to change the value
of the variables.
On the icon bar you will now find the following icons.
If you put the cursor over an icon and leave it for a while, a description of what the
icon does is displayed.
The Step icon steps through the program one line at a time. Step through at least one
loop of the while loop. Notice the variables changing in the "Workspace" window.
The Step In icon is very like step, except when the line contains a call to a function.
Step will treat the function as one line of code, while Step In will step into the
function and step through the function. Try this out.
Step Out reverse a Step In. If you are inside a function, a Step Out will execute the
rest of the function, exit the function and then stop again. Step into the local function
and try a Step Out. If you hit Step Out in the top function, the program will run to
completion.
The Continue icon restarts the program from the current point in the program. It will
run to the next breakpoint or completion. Try this now.
Close the editor.
15.
Enter the following
19. Change the title and the plot
command so that a cosine wave is
plotted in green. Save as graph2.
degrees = 0:6:360;
rad = degrees * pi /180;
plot(sin(rad))
20. Change the title and the plot
command so that tan is plotted in red.
Change the y limits to -10 to 10. Save
as graph3.
Close the graph and repeat the first two
commands by double clicking on them
in the Command History window.
21. Now try a subplot command. In
the command window enter.
16.
Drag the plot command from
the Command History into the
command window and change it to :-
subplot(3,1,1)
graph1
subplot(3,1,2)
graph2
subplot(3,1,3)
graph3
plot(degrees,sin(rad))
See how the plot changes.
17. Hold down control and select
each of the following commands in the
Command History Window,
Don't forget that you can use the cursor
keys to recall previous commands.
degrees = 0:6:360;
rad = degrees * pi /180;
plot(degrees,sin(rad))
22. Clear the figure and enter the
following.
clf
graph1
hold on
graph2
hold off
figure
graph3
then press the right mouse button and
select Create Script.
Save the file as graph1.m
18. Annotate the graph by adding
the following lines to the script.
Now delete the extra figures.
axis([0 360 -1 1])
title('Sine Wave')
xlabel('degrees')
ylabel('value')
grid
delete(2)
And clear the workspace
clear
Observe the resulting graph.
23. You should have a spreadsheet
call XLfile.xls in the current directory.
Click on the white background of the
graph.
Double click on the file XLfile.xls
Add a title "My Graph" and a grid in
the x and y directions.
Click on the green tick to import the
data.
Set the XLabel to "Time" and set the
XLimits to 0 to 2*pi.
Close the import window.
Set the YLabel to "Amplitude".
If you look at the Workspace window,
you will see that the data from the
spread sheet has been imported into
MATLAB.
Click in the Hide Plot Tools icon.
24. Hold down the control key and
select X and Y in the workspace
window.
From the file Menu, on the banner of
the figure, select
Generate Code.
On the MATLAB icon bar, select the
PLOTS tab.
Select a normal plot.
Save the file to createfigure.m and
close the editor.
25. Click on the Show Plot Tools icon
on the figure icon bar.
26. Run the M file you have just
created.
createfigure(X,Y)
Click on the line of the graph.
Try different line styles, thickness and
colour.
Try different markers. Change the size
of the markers and try different fill and
line colours.