matlab_exercises
matlab_exercises
Part 1
version 7.1, EJP, 2019
You can use the arrow keys and 7. Display the angle
the delete key to recall and edit and magnitude and of Z.
previous commands. Press the up
arrow key twice to recall the format 8. Try the following.
command and delete the "e" and
press enter. Then display pi again. Z'
Repeat with the following formats. Z.'
format shortE
format short
a = pi/6
sin(a)^2 + cos(a)^2
exp(2*log(3) + 3*log(2))
1
9. Enter the following matrix 12. It is just as easy to solve a
hundred simultaneous equations in
a hundred variables. First create a
[ ]
A= 1 2
3 4 100 by 100 matrix of random
numbers.
10. Try the following and ensure
you can follow what is happening. A1 = rand(100);
A*x
2
Part 2
1. You should have the two
matrices 4. Plot the polynomial 2 x 3− x
using the function polyval. First
A= [ ]
1 2
3 4
B=A+5
find out how to use polyval using
the help.
x = linspace(-1,1,100); r = roots(p)
y = 2*x.^3 -x; ry = zeros(3,1)
What happens if you don't The plot should still be held from
include the dot ? exercise 4.
plot(x,y) plot(r,ry,'rx')
3
7. Enter the following.
All variables should have been
a = 2: 0.5 : 4 saved to matlab.mat . If you can't
see this in the Current Folder
a(2) window, right click in the window
a([2 4]) and select refresh.
a(2:4)
a(2:end) The workspace window should be
empty. Double click on
matlab.mat to restore all your
8. Enter the following variables.
Enter the following in the command Click on the "Save and Run" icon
window.
save
clear
4
13. You should have a file called mysqrt.m. Edit this file
edit mysqrt
function x=mysqrt(A)
while(err>0.0001)
x = myfunction(A,x); % call to local function below
err = abs(A -x.^2); % calculate the error
end
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
5
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.
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.
6
15. Enter the following
Observe the resulting graph.
degrees = 0:6:360;
rad = degrees * pi /180; 19. Change the title and the plot
plot(sin(rad)) command so that a cosine wave is
plotted in green. Save as graph2.
Close the graph and repeat the first
two commands by double clicking 20. Change the title and the plot
on them in the Command History command so that tan is plotted in
window. If you cannot see this red. Change the y limits to -10 to
window, from the HOME tab select 10. Save as graph3.
Layout► Command History
►Docked 21. Now try a subplot command.
In the command window enter.
16. Drag the plot command
from the Command History into subplot(3,1,1)
the command window and change graph1
it to :- subplot(3,1,2)
graph2
plot(degrees,sin(rad)) subplot(3,1,3)
graph3
See how the plot changes.
Don't forget that you can use the
17. Hold down control and select cursor keys to recall previous
each of the following commands in commands.
the Command History Window,
22. Clear the figure and enter the
degrees = 0:6:360; following.
rad = degrees * pi /180;
plot(degrees,sin(rad)) clf
graph1
then press the right mouse button hold on
and select Create Script. graph2
Save the file as graph1.m hold off
figure
18. Annotate the graph by graph3
adding the following lines to the
script. Now delete the extra figures.
7
23. You should have a Click on the white background of
spreadsheet call XLfile.xls in the the graph.
current directory.
Add a title "My Graph" and a grid in
Double click on the file XLfile.xls the x and y directions.
For Output Type, select Column Set the XLabel to "Time" and set
vectors. the XLimits to 0 to 2*pi.
Click on the green tick to import the Set the YLabel to "Amplitude".
data.
From the file Menu, on the banner
Close the import window. of the figure, select
Generate Code.
If you look at the Workspace
window, you will see that the data A function containing the code is
from the spread sheet has been opened in the MATLAB editor.
imported into MATLAB. Save the file to createfigure.m
Select plot.