Exercises MATLAB
Exercises MATLAB
Part 1
version 5, EJP, 2013
1.
Start matlab.
2.
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.
7.
8.
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.
9.
3 4
5 6
B
7 8
A1 = rand(100);
A+5
A+B
A-B
A*B
A^2
A'
x = A1 \ b
Check that the solution is correct.
b = [ 5 ; 11 ]
x=A\b
A1 * x
Part 2
1.
3 4
5 6
B
7 8
doc polyval
p = [4 0 -3 0]
y1 = polyval(p,x);
hold on
plot(x,y1,'g')
A .* B
A ./ B
A .^ B
sin(A * pi/6)
D = A.^2
sqrt(D)
r = roots(p)
ry = zeros(3,1)
x = linspace(-1,1,100);
y = 2*x.^3 -x;
plot(r,ry,'rx')
plot(x,y)
a = 2: 0.5 : 4
a(2)
a([2 4])
a(2:4)
a(2:end)
8.
x = linspace(-2*pi,2*pi,100);
y = sin(x);
plot(x,y)
grid
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.
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.
13.
%First guess
%Set error to something to get started
while(err>0.0001)
x = myfunction(A,x);
err = abs(A -x.^2);
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
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.
degrees = 0:6:360;
rad = degrees * pi /180;
plot(sin(rad))
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,
degrees = 0:6:360;
rad = degrees * pi /180;
plot(degrees,sin(rad))
delete(2)
And clear the workspace
clear