# CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008.

## Presentation on theme: "CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008."— Presentation transcript:

CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008

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…

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)

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

Logic Conditions ==,, ~=, ~ &, |, ~: element-by-element for matrices find( ‘ condition ’ ): return index of A ’ s elements that satisfy the condition

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

If If a == b …; elseif d==e …; else …; end

for for j = 1:n for k = 1:m … end

while while a>b … ; end

break vs. continue for i=1:n … ; break; End ------------------------ for i=1:n … ; continue; end

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, …

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.

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

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…

The End

Similar presentations