0% found this document useful (0 votes)
3 views

matlab_exercises

This document contains a series of Matlab exercises designed to teach users various functionalities of the software, including matrix operations, plotting, and function creation. It covers basic commands, array manipulations, and debugging techniques, along with practical applications such as solving equations and visualizing data. The exercises are structured in two parts, guiding users through increasingly complex tasks to enhance their Matlab skills.

Uploaded by

anwargngn10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

matlab_exercises

This document contains a series of Matlab exercises designed to teach users various functionalities of the software, including matrix operations, plotting, and function creation. It covers basic commands, array manipulations, and debugging techniques, along with practical applications such as solving equations and visualizing data. The exercises are structured in two parts, guiding users through increasingly complex tasks to enhance their Matlab skills.

Uploaded by

anwargngn10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Matlab Exercises

Part 1
version 7.1, EJP, 2019

1. Start matlab. 5. Enter the following

2. Enter the following a = [1 2 3]


b = [4 5 6]
1+2 Z = a + i*b
x=1+2
x = 1 + 2; i here is the square root of -1.
y = x^2 + 2*x + 8 You can use j instead if that is what
you are use to.
3. Enter the following
6. Display the real and
format longE imaginary parts of Z
pi and the conjugate of Z.

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

4. Enter the following

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);

B=A+4 If you forget to put in the


A+B semicolon, 10,000 numbers will be
A-B printed out.
A*B
A^2 Next create a column vector with
A' 100 numbers

11. Solve the simultaneous b = (1:100)'


equation on page 15 of the notes.
A should already be present from Now solve
exercise 10.
x = A1 \ b
b = [ 5 ; 11 ]
x=A\b Check that the solution is correct.

Now check that x is the correct A1 * x


solution.

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.

from exercise 10 in part 1. doc polyval


If not, then enter them again.
p = [4 0 -3 0]
2. Now try the following array y1 = polyval(p,x);
operations.
hold on
A .* B plot(x,y1,'g')
A ./ B
A .^ B 5. There are many functions that
sin(A * pi/6) handle polynomials. Look them up
D = A.^2 in the help. Enter doc polyval
sqrt(D) again, then click on Functions on
the blue banner at the top of the
window.
Clear the workspace of all
variables. What does the function roots do?
clear
6. Plot the roots of the polynomial
3. Plot the polynomial 3
2 x −x onto the graph.

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')

Don't close the figure containing Clear the figure


the plot. clf

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.

w = (1:5)' * (5:10) 11. Produce a script called


mygraph.
This produce a 5 by 6 matrix that
we can use in the next exercise. edit mygraph

Set the third element on the second In mygraph enter


row of w to 100.
x = linspace(-2*pi,2*pi,100);
9. Enter the following y = sin(x);
plot(x,y)
w(2:4,2:4) grid
w(2,:)
w(:,5) Save by clicking on the icon
w([1 5],[2,4]) and run by entering
w(:)
w(:) = 1:30 mygraph

10. By now you should have a in the command window.


nice collection of variables. Try

who 12. Add the following at the end of


whos the script created above.

If you cannot see the workspace


window, click on the HOME tab and hold on
then click on Layout in the y1 = mysin(x);
ENVIROMENT section and select plot(x,y1,'r:')
Three Column. axis( [-2*pi,2*pi,-2,2] )

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)

% My square root function.


%
% x = mysqrt(A)
%
% x is the square root of A.

x=1; %First guess


err=1; %Set error to something to get started

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.

Close the editor.

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.

axis([0 360 -1 1]) delete(2)


title('Sine Wave')
xlabel('degrees') And clear the workspace
ylabel('value') clear
grid

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

Close the editor and the figure.


24. Hold down the control key and
select X and Y in the workspace 26. Run the M file you have just
window. created.

On the MATLAB icon bar, select createfigure(X,Y)


the PLOTS tab.

Select plot.

25. Under View on the figure icon


bar, select Property Editor.

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.

You might also like