MATLAB Tutorials
Transcript of MATLAB Tutorials
![Page 1: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/1.jpg)
MATLAB Tutorials
Violeta Ivanova, [email protected]
Department of Nuclear Science and Engineering - Spring 2008
web.mit.edu/acmath/matlab/nuclear
![Page 2: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/2.jpg)
MATLAB Desktop Interface
Start MATLAB now …
Command Line Window
Current Directory Window
Command History Window
![Page 3: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/3.jpg)
Help in MATLAB
Help BrowserHelp->MATLAB Help
Command line help >> help <command>
e.g. help cos >> lookfor <keyword>
e.g. lookfor cosine
![Page 4: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/4.jpg)
Variables Begin with an alphabetic character: a Case sensitive: a, A No data typing: a=5; a=‘ok’; a=1.3 Default output variable: ans Built-in constants: pi i j Inf clear removes variables who lists variables Special characters
[] () {} ; % : = . … @
![Page 5: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/5.jpg)
Vectors
Row vector>> R1 = [1 6 3 8 5]>> R2 = [1 : 5]>> R3 = [-pi : pi/3 : pi]
Column vector>> C1 = [1; 2; 3; 4; 5]>> C2 = R2'
![Page 6: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/6.jpg)
Matrices
Creating a matrix>> A = [1 2.5 5 0; 1 1.3 pi 4]>> A = [R1; R2]
Accessing elements>> A(1,1)>> A(1:2, 2:4)>> A(:,2)
![Page 7: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/7.jpg)
Matrix Operations Operators + and -
>> X = [1 2 3]>> Y = [4 5 6]>> A = X + Y A =
5 7 9
Operators *, /, and ^>> Ainv = A^-1 Matrix math is default!
![Page 8: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/8.jpg)
Element-Wise Operations
Operators .*, ./, and .^>> Z = [2 3 4]’>> B = [Z.^2 Z Z.^0]B =
4 2 1
9 3 116 4 1
![Page 9: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/9.jpg)
File Input / Output
Import Wizard for data importFile->Import Data …
File input with loadB = load(‘datain.txt’)
File output with savesave(‘dataout’, ‘A’, ‘-ascii’)
![Page 10: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/10.jpg)
2D Graphics
Linear plots>> plot (X, Y)
Plotting commands open the Figure editor.
Graphics customization Plot Edit mode: Annotation: View->Property Editor Publication: File->Save as
![Page 11: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/11.jpg)
Multiple Plots
Multiple datasets on a plot>> plot(xcurve, ycurve)>> hold on>> plot(Xpoints, Ypoints)>> hold off
Subplots on a figure>> subplot(1, 2, 1)>> plot(time, velocity)>> subplot(1, 2, 2)>> plot(time, acceleration)
![Page 12: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/12.jpg)
Exercise One
Frequency response: subplots.m Import data from response.dat Create graph with two subplots Plot yin(t) and yout(t) in entire interval Define amplitude and phase change
Follow instructions in the m-file …
![Page 13: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/13.jpg)
Data StatisticsFigure editor: Tools->Data Statistics
![Page 14: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/14.jpg)
Basic Fitting Figure editor: Tools->Basic Fitting …
![Page 15: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/15.jpg)
M-File Programming
Script M-Files Automate a series of steps. Share workspace with other scripts and the
command line interface.
Function M-Files Extend the MATLAB language. Can accept input arguments and return
output arguments. Store variables in internal workspace.
![Page 16: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/16.jpg)
Function M-FilesExample: amodel.m
function Y = amodel(t, A, B, a, w, p)% H1 line: AMODEL computes step response.
% Help text: appears when you type
% “help amodel” in command line window.
% Comment: function body is below.
Y = A * exp(-b.*t).*cos(w.*t + p) + B;
![Page 17: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/17.jpg)
Script M-Files
Example: model.m
% Define input
T = [0 : 0.01 : 30];
% Compute model
Y = exp(-T);
% Plot model
plot (T, Y);
![Page 18: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/18.jpg)
MATLAB Program
Always has one script M-File Uses built-in and user-defined functions Created in MATLAB Editor>> edit model.m
Run from Command Line Window>> model
![Page 19: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/19.jpg)
Built-in Functions
MATLAB ”main”>> m = mean(X)>> s = std(X)
Toolboxes, e.g., Statistics>> Y = exppdf(X, mu)>> m = expfit(data)
![Page 20: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/20.jpg)
Statistics Toolbox
Hypothesis Testing Null and alternative hypotheses Test statistic and P-value
Correlation coefficient
!X ,Y
=cov X,Y( )"
X"
Y
![Page 21: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/21.jpg)
Data Analysis Example Correlation and confidence interval
>> [R, P] = corrcoef(X);>> [i, j] = find(P < 0.05);
![Page 22: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/22.jpg)
Analysis of Variance (ANOVA)
One-way ANOVA>> anova1(X,group)
![Page 23: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/23.jpg)
Exercise Two Carcinogens: correlation.m
Import data from smokingcancer.dat Plot and graphically analyze data Compute correlation coefficients and P’s Quantitatively test hypotheses …
Follow instructions in the m-file …
![Page 24: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/24.jpg)
Polynomial Fitting Tool>> polytool(X, Y)
![Page 25: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/25.jpg)
Distribution Fitting Tool>> dfittool
![Page 26: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/26.jpg)
Curve Fitting Tool>> cftool
![Page 27: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/27.jpg)
Goodness of Fit Statistics
![Page 28: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/28.jpg)
Analyzing a Fit
![Page 29: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/29.jpg)
Exercise Three World population: regression.m
Import data from worlddata.dat Fit quadratic with polytool Fit exponential with cftool Forecast population in 2050
Follow instructions in the m-file …
![Page 30: MATLAB Tutorials](https://reader031.fdocumento.com/reader031/viewer/2022012101/61dba7a64f5eb138752cc9b8/html5/thumbnails/30.jpg)
References
Mathematical Tools at MITweb.mit.edu/ist/topics/math
QUESTIONS?