Download presentation
Presentation is loading. Please wait.
1
CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008
2
Matlab Basic How to get start For Windows: trivial… For Unix: %module load math/matlab/v.7.0.4 %matlab Outside the campus, need SSH…
3
Matrix Matrix is a main data type in Matlab Matlab stands for Matrix Laboratory How to build a matrix A=[1 2 3; 4 5 6; 7 8 9] Special matrices zeros(n,m), ones(n,m), eye(n,m)
4
Basic Operations All the operators in Matlab defined on matrices: + - * / ^ sqrt sin cos Element wise operator with preceding dot:.*./.^ Comment: % Assignment: = num = 6 %assignment & display num = 6; %assignment
5
Logic Conditions ==,, ~=, ~ &, |, ~: element-by-element for matrices find( ‘ condition ’ ): return index of A ’ s elements that satisfy the condition
6
Flow Control Matlab has five flow control constructs (very similar with other languages): if statements for loops while loops switch statements break (or continue) statements
7
If If a == b …; elseif d==e …; else …; end
8
for for j = 1:n for k = 1:m … end
9
while while a>b … ; end
10
break vs. continue for i=1:n … ; break; End ------------------------ for i=1:n … ; continue; end
11
Basic Graphing (2D Plots) plot x = linspace(0,2*pi,200); f1 = sin(x); plot(x,f1) title('Plot of f_1 = sin(x)'); xlabel('x'); ylabel('f_1'); plot3, mesh, contour, …
12
Scripts and Functions There are two kinds of.m files Script: which do not accept input arguments or return output arguments. They operate on data in the workspace. Function: which can accept input arguments and return output arguments. Internal variables are local to the function.
13
Functions in Matlab Function: keyword as add new function Example: Suppose a file on disk called STAT.M: function [mean,stdev] = stat(x) %STAT Interesting statistics n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n);
14
Matlab Help In the Matlab help Click MATLAB Help, then search index for … On the web http://www.mathworks.com/support http://mathworld.wolfram.com http://web.mit.edu/course/10/10.34/www/M ATLAB_tutorial_master.html http://web.mit.edu/course/10/10.34/www/M ATLAB_tutorial_master.html Check course website for more…
15
The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.