



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to using MatLab, a powerful software tool for mathematics, engineering, and sciences. It covers the basics of entering and manipulating matrices, performing arithmetic operations, and solving linear equations. The document also demonstrates how to find eigenvalues and eigenvectors, and includes examples related to the class. Additionally, it explains how to create and save scripts and functions in MatLab.
Typology: Study Guides, Projects, Research
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Fall 2015 Math 337
MatLab is a powerful software created by MathWorks, which is used extensively in mathe- matics, engineering, and the sciences. It has powerful numerical and graphic capabilities, so is an invaluable tool for students to learn. This sheet is an introduction to the software for Math
A = [1 0 -2 3 -1 2 0 5 6 0 1 4 3 -1 -3 0];
B = [2 1 0 4;-1 -3 5 1;6 0 0 -2;1 -2 -1 5];
The matrices in MatLab are stored in the variables A and B. One enters the rows starting with a [, then numbers are entered either separated by a space or comma. New rows to the matrix are produced either with with a ; separating the rows or using enter to move to the next line. The matrix ends when ] is entered. If this is followed by a ;, then MatLab does not output the matrix. However, if enter is used, MatLab displays the matrix. Normal arithmetic operations are easily performed, such as A + B, A − B, or raising a power A^3 , in the last case typing A^3. Below shows the matrix multiplication, AB, and the inverse, A ^1. Note that if no variable is speci ed, then MatLab automatically stores the answer in the temporary variable ans.
A*B ans = -7 -5 -3 23 1 -17 5 23 22 -2 -4 42 -11 6 -5 17
inv(A) ans = -0.3919 0.1486 0.1081 0. -1.5811 1.0135 -0.0811 1. 0.1351 -0.1892 0.1351 -0. 0.5541 -0.1757 0.0541 -0.
MatLab can easily solve the linear equation Ax = b. We note that the transpose of a matrix is given by b′^ in MatLab. Below we show two ways to solve this equation.
b = [2 0 1 5];
x = linsolve(A,b’) x =
inv(A)*b’ ans =
-1. -0.
Similarly, MatLab can readily nd eigenvalues and eigenvectors.
eig(A) ans = 2.3090 + 4.9965i 2.3090 - 4.9965i -1.
[v,d]=eig(A) v = Columns 1 through 3 -0.1627 - 0.3412i -0.1627 + 0.3412i 0. -0.3486 - 0.0541i -0.3486 + 0.0541i 0. -0.7459 -0.7459 -0. 0.0000 - 0.4199i 0.0000 + 0.4199i -0. Column 4
-0. -0. d = Columns 1 through 3 2.3090 + 4.9965i 0 0 0 2.3090 - 4.9965i 0 0 0 -1. 0 0 0 Column 4 0 0 0
We note that all of the previous scripts were produced in MatLab and stored in a text le (test.txt). This is readily done by the MatLab commands:
diary(’test.txt’) diary on MatLab entries here
k = log(4/3); Te = 22; yp = -k*(y - Te); % This is the RHS of the DE end
Note that MatLab uses % beginning any comments that are not executed. The next step is to use MatLab's numerical ODE solver to create the solution of the differential equation from the time of death, td, found earlier for about 12 hours after the murder. Below we show the solving of the model and plotting of the graph.
[t1,y1] = ode23(@murder,[0,td],30); [t2,y2] = ode23(@murder,[0,12],30); plot(t1,y1,’b-’,t2,y2,’b-’);grid;
This produces a blue solution from the time of death until 12 hours after t = 0. This is quick way to produce a graph, but we want to illustrate the power of MatLab to produce a very good looking, professional graph. Below we present an M- le, Script, which executes a series of commands to produce a good graph. There are comments interior to the program to help understand what is happening, and clearly one could explore other options to improve the graph.
clear % Clear previous definitions figure(1) % Assign figure number clf % Clear previous figures hold off % Start with fresh graph
mytitle = ’Body Temperature’; % Title xlab = ’$t$’; % X-label ylab = ’$T(t)$’; % Y-label
f = @(k) 22+8exp(-k)-28; % Function for finding heat coefficient k = fzero(f,0.3); % Find the heat coefficient ft = @(t) 22+8exp(-k*t)-37; % Function for finding the time of death td = fzero(ft,-5); % Find the time of death
[t1,y1] = ode23(@murder,[0,td],30); % Simulate the heat equation [t2,y2] = ode23(@murder,[0,12],30); % Simulate the heat equation
plot(t1,y1,’k-’,’LineWidth’,1.5); % Plot from death to t = 0 hold on % Plots Multiple graphs plot(t2,y2,’k-’,’LineWidth’,1.5); % Plot from t = 0 to 12 plot([-9,td],[37,37],’r-’,’LineWidth’,1.5); % Plot from t = -9 to td plot([-9,12],[22,22],’b:’,’LineWidth’,1.5); % Plot room temperature plot([td,td],[20,40],’k:’,’LineWidth’,1.5); % Plot time of murder grid % Adds Gridlines text(-2.7,28,’Time of death, $t_d$’,’rot’,90,’FontSize’,14,... ’FontName’,’Times New Roman’,’interpreter’,’latex’); text(-4.8,22.6,’Room Temperature’,’color’,’blue’,’FontSize’,14,... ’FontName’,’Times New Roman’); axis([-9 12 20 40]); % Defines limits of graph
fontlabs = ’Times New Roman’; % Font type used in labels xlabel(xlab,’FontSize’,14,’FontName’,fontlabs,’interpreter’,’latex’); % x-Label size and font ylabel(ylab,’FontSize’,14,’FontName’,fontlabs,’interpreter’,’latex’); % y-Label size and font title(mytitle,’FontSize’,16,’FontName’,’Times New Roman’,’interpreter’,’latex’); % Title size/font set(gca,’FontSize’,12); % Axis tick font size
print -depsc body_temp.eps % Create figure as EPS file print -djpeg body_temp.jpg % Create figure as JPEG file
Note: All graphing scripts should have the rst 4 commands to start with a clean Workspace, clear old and label a new gure, and refresh the gure window. The last lines starting with fontlabs give the user a lot of control on the appearance of writing on the graphs, so are very useful to have readily available for any graph. Finally, the last two lines are necessary to have the graph readily available for use in a document (EPS for LATEXand JPG for Word). In subsequent graphing script these heading and trailing lines of code will not appear in these write-ups to avoid repetition. The product of MatLab script entitled murder plot, which is available from my webpage, is
−8 −6 −4 −2 0 2 4 6 8 10 12 20
22
24
26
28
30
32
34
36
38
40
Time of death,
t d
Room Temperature
t
(t