21.06.2014 Views

MATLAB demo - SAMSI

MATLAB demo - SAMSI

MATLAB demo - SAMSI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduction to Matlab<br />

Xueying Wang<br />

<strong>SAMSI</strong> Undergraduate Workshop Spring 2010<br />

February 26, 2010<br />

Xueying Wang<br />

Introduction to Matlab


Introduction<br />

<strong>MATLAB</strong> is an interactive software package which was developed<br />

to perform numerical calculations on vectors and matrices. Initially,<br />

it was simply a MATrix LABoratory. However, today it is much<br />

more powerful:<br />

• can do quite sophisticated graphics.<br />

• is quite easy to code complicated algorithms involving vectors<br />

and matrices.<br />

• can numerically solve a wide variety of ODEs and PDEs.<br />

• contains various toolboxes.<br />

Xueying Wang<br />

Introduction to Matlab


Getting started<br />

• To begin a <strong>MATLAB</strong> session, type matlab or click on a<br />

<strong>MATLAB</strong> icon and wait for the prompt, i.e ” ≫ ”, to appear. You<br />

are now in the <strong>MATLAB</strong> workspace.<br />

• To exit <strong>MATLAB</strong>, type exit or quit.<br />

• To get help, typing<br />

>> help <br />

or<br />

>> doc <br />

gives you complete information about the command.<br />

Xueying Wang<br />

Introduction to Matlab


Things to know<br />

1 Scalar, array and matrix calculations<br />

2 Graphics<br />

3 <strong>MATLAB</strong> functions<br />

4 Programming<br />

Xueying Wang<br />

Introduction to Matlab


Scalar, array and matrix calculations<br />

—– Scalar:<br />

>> (17-1/9+pi*2.1)^3<br />

>> ans % to provide the most recent answer<br />

>> sin(6)<br />

>> exp(2)<br />

>> log(10)<br />

—– Vectors:<br />

>> v = [2 3 4]<br />

>> v = 2:4<br />

To compute<br />

9∑<br />

i=0<br />

1<br />

, you can enter<br />

i<br />

>> 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9<br />

Xueying Wang<br />

Introduction to Matlab


Scalar, array and matrix calculations, ctd<br />

A simpler way is<br />

>> x=1:9<br />

>> 1./x % element-wise division<br />

>> sum(ans) (i.e.>> sum(1./[1:9]))<br />

—– Matrices:<br />

>> A=rand(3) % a 3-by-3 matrix consisting of random<br />

% numbers uniformly distributed on [0,1]<br />

>> M=randn(2,6) % a 2-by-6 matrix containing random<br />

% numbers which are drawn from<br />

% the standard normal<br />

>> zeros(4,7) % the 4-by-7 matrix of zeros<br />

>> size(M) % the dimension of the matrix M<br />

>> eye(3) % the 3-by-3 identity matrix<br />

>> ones(4) % the 4-by-4 matrix of 1s<br />

Xueying Wang<br />

Introduction to Matlab


Scalar, array and matrix calculations, ctd<br />

>> whos % list all variables in workspace<br />

>> B=[1 2 3; 4 5 6; 7 8 9];<br />

>> A*B % matrix multiplication<br />

>> A.*B % element-wise multiplication<br />

>> sqrt(B)<br />

>> det(A) % the determinant of matrix A<br />

>> inv(A) % the inverse of matrix A<br />

>> b = rand(3,1);<br />

To solve equations A x = b, we can type<br />

>> x = inv(A)*b<br />

or<br />

>> x = A\b<br />

Xueying Wang<br />

Introduction to Matlab


Scalar, array and matrix calculations, ctd<br />

>> % eigenvalues and eigenvectors of matrix A (A*V=V*D)<br />

>> [V,D] = eig(A)<br />

>> whos<br />

>> save % save workspace variables to ’matlab.mat’<br />

>> clear % removes all variables from the workspace<br />

>> U = rand(1000,1);<br />

>> N = randn(10^3,1)*3+1;<br />

>> [mean(N),std(N)]<br />

>> [min(N),max(N)]<br />

>> norm(N)<br />

Xueying Wang<br />

Introduction to Matlab


Graphics: 2-d plot<br />

>> n = 100;<br />

>> x = 2*pi*[1:n]/n;<br />

>> y1 = sin(x); y2 = 2*cos(x);<br />

>> plot(x,y1)<br />

>> hold on;<br />

>> plot(x,y2);<br />

>> hold off;<br />

>> plot(x,y1,x,y2,’r.’)<br />

>> xx = linspace(0, 2*pi,n);<br />

>> figure;<br />

>> plot(xx, sin(xx),’b*’, xx,2*cos(x),’r-’)<br />

>> subplot(2,2,1); ezplot(@exp)<br />

>> subplot(2,2,2); semilogy(x,exp(x));<br />

>> subplot(2,2,3:4); loglog(x,exp(x));<br />

Xueying Wang<br />

Introduction to Matlab


Graphics: 3-d plot<br />

>> x = [-3:0.1:3]’;<br />

>> y = [-2:0.1:2]’;<br />

>> [X, Y] = meshgrid(x, y);<br />

>> Z = (X + Y).*exp( -X.*X - 2*Y.*Y );<br />

>> mesh(X, Y, Z)<br />

>> surf(X, Y, Z)<br />

>> xlabel(’X’)<br />

>> ylabel(’Y’)<br />

>> zlabel(’Z’)<br />

>> contour(X,Y,Z)<br />

Xueying Wang<br />

Introduction to Matlab


<strong>MATLAB</strong> functions<br />

⊛ <strong>MATLAB</strong> has lots of built-in functions.<br />

⊛ We can create our own <strong>MATLAB</strong> functions. Type<br />

>> edit <br />

The first line of <strong>MATLAB</strong> function files is of the form<br />

function = (, ..., )<br />

or<br />

function [, ..., ] = (,<br />

..., )<br />

Xueying Wang<br />

Introduction to Matlab


Programming<br />

The general form of the for loop is<br />

for = <br />

<br />

...<br />

<br />

end<br />

Xueying Wang<br />

Introduction to Matlab


Programming, ctd<br />

The second <strong>MATLAB</strong> loop structure is the while statement. The<br />

general form of the while loop is<br />

>> while <br />

<br />

...<br />

<br />

end<br />

Xueying Wang<br />

Introduction to Matlab


Programming, ctd<br />

The simplest form of the if statement is<br />

>> if <br />

<br />

...<br />

<br />

end<br />

where the < statements > are evaluated as long as the<br />

< logicalexpression > is true.<br />

Xueying Wang<br />

Introduction to Matlab

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!