Exercises PDF
Exercises PDF
Exercises PDF
Part 1
version 5, EJP, 2013
format short e
format short
a = pi/6
sin(a)^2 + cos(a)^2
exp(2*log(3) + 3*log(2))
1
9. Enter the two matrices 12. It is just as easy to solve a
hundred simultaneous equations in a
1 2 5 6 hundred variables. First create a 100
A B by 100 matrix of random numbers.
3 4 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 Next create a column vector with 100
A-B numbers
A*B
A^2 b = (1:100)'
A'
Now solve
11. Solve the simultaneous equation
on page 15 of the notes. A should x = A1 \ b
already be present from exercise 10.
Check that the solution is correct.
b = [ 5 ; 11 ]
x=A\b A1 * x
Now check that x is the correct
solution.
A*x
2
Part 2
1. You should have the two matrices
4. Plot the polynomial 4 x 3 −3 x
1 2 5 6 using the function polyval. First find
A B out how to use polyval using the help.
3 4 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
5. There are many functions that
A .^ B
handle polynomials. Look them up in
sin(A * pi/6)
the help. Enter doc polyval again,
D = A.^2
then click on polynomials on the path
sqrt(D)
at the top of the page.
3
7. Enter the following. All variables should have been saved
to matlab.mat . If you can't see this in
a = 2: 0.5 : 4 the "Current Folder" window, right
click in the window and select refresh.
a(2)
a([2 4]) The workspace window should be
a(2:4) empty. Double click on matlab.mat to
a(2:end) restore all your variables.
who hold on
whos y1 = mysin(x);
plot(x,y1,'r:')
You will also be able to see your axis( [-2*pi,2*pi,-2,2] )
variables in the workspace window.
Click on the "Save and Run" icon
Enter the following in the command
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. 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.
6
15. Enter the following
19. Change the title and the plot
degrees = 0:6:360; command so that a cosine wave is
rad = degrees * pi /180; plotted in green. Save as graph2.
plot(sin(rad))
20. Change the title and the plot
Close the graph and repeat the first two command so that tan is plotted in red.
commands by double clicking on them Change the y limits to -10 to 10. Save
in the Command History window. as graph3.
7
23. You should have a spreadsheet Click on the white background of the
call XLfile.xls in the current directory. 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 Click in the Hide Plot Tools icon.
spread sheet has been imported into
MATLAB.
createfigure(X,Y)