250 likes | 403 Views
Introduction to Matlab. Desktop Tools (Matlab v6). Command Window type commands Workspace view program variables clear to clear double click on a variable to see it in the Array Editor Command History view past commands save a whole session using diary Launch Pad
E N D
Desktop Tools (Matlab v6) • Command Window • type commands • Workspace • view program variables • clear to clear • double click on a variable to see it in the Array Editor • Command History • view past commands • save a whole session using diary • Launch Pad • access tools, demos and documentation
Matlab Files (.m) • Use predefined functions or write your own functions • Reside on the current directoryor the search path • add with File/Set Path • Use the Editor/Debugger to edit, run
Matrices • a vectorx = [1 2 5 1] x = 1 2 5 1 • a matrixx = [1 2 3; 5 1 4;3 2 -1] x = 1 2 3 5 1 4 3 2 -1 • transposey = x.’ y = 1 2 5 1
Matrices • x(i,j) subscription • whole row • whole column y=x(2,3) y = 4 y=x(3,:) y = 3 2 -1 y=x(:,2) y = 2 1 2
Operators (arithmetic) + addition - subtraction * multiplication / division ^ power ‘ complex conjugate transpose .* element-by-element mult ./ element-by-element div .^ element-by-element power .‘ transpose
Operators (relational, logical) == equal ~= not equal < less than <= less than or equal > greater than >= greater than or equal & AND | OR ~ NOT • pi 3.14159265… • j imaginary unit, • i same as j
zeros(M,N) MxN matrix of zeros ones(M,N) MxN matrix of ones rand(M,N) MxN matrix of uniformly distributed random numbers on (0,1) x = zeros(1,3) x = 0 0 0 x = ones(1,3) x = 111 x = rand(1,3) x = 0.9501 0.2311 0.6068 Generating Vectors from functions
[ ] concatenation ( ) subscription x = [ zeros(1,3) ones(1,2) ] x = 0 0 0 1 1 x = [ 1 3 5 7 9] x = 1 3 5 7 9 y =x(2) y = 3 y =x(2:4) y = 3 5 7 Operators
Matlab Graphics x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function')
t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) grid on Multiple Graphs
Multiple Plots t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); subplot(2,2,1) plot(t,y1) subplot(2,2,2) plot(t,y2)
Graph Functions (summary) • plot linear plot • stem discrete plot • grid add grid lines • xlabel add X-axis label • ylabel add Y-axis label • title add graph title • subplot divide figure window • figure create new figure window • pause wait for user response
Math Functions • Elementary functions (sin, cos, sqrt, abs, exp, log10, round) • type help elfun • Advanced functions (bessel, beta, gamma, erf) • type help specfun • type help elmat
Functions function f=myfunction(x,y) f=x+y; • save it in myfunction.m • call it with y=myfunction(x,y)
Flow Control • if statement • switch statement • for loops • while loops • continue statement • break statement if A > B 'greater' elseif A < B 'less' else 'equal' end for x = 1:10 r(x) = x; end
Miscellaneous • Loading data from a file • load myfile.dat • Suppressing Output • x = [1 2 5 1];
Getting Help • Using the Help Browser (.html, .pdf) • View getstart.pdf, graphg.pdf, using_ml.pdf • Type • help • help function, e.g. help plot • Running demos • type demos • type help demos
Random Numbers x=rand(100,1); stem(x); hist(x,100)
Coin Tosses • Simulate the outcomes of 100 fair coin tosses x=rand(100,1); p=sum(x<0.5)/100 p = 0.5400 • Simulate the outcomes of 1000 fair coin tosses x=rand(1000,1); p=sum(x<0.5)/1000 p = 0.5110
Coin Tosses • Simulate the outcomes of 1000 biased coin tosses with p[Head]=0.4 x=rand(1000,1); p=sum(x<0.4)/1000 p = 0.4160
Sum of Two Dies • Simulate 10000 observations of the sum of two fair dies
Sum of Two Dies • Simulate 10000 observations of the sum of two fair dies x1=floor(6*rand(10000,1)+1); x2=floor(6*rand(10000,1)+1); y=x1+x2; sum(y==2)/10000ans =0.0275 p[2]=0.0278 sum(y==3)/10000ans =0.0554 p[3]=0.0556 sum(y==4)/10000ans =0.0841 p[4]=0.0833 sum(y==5)/10000ans =0.1082 p[5]=0.1111 sum(y==6)/10000ans =0.1397 p[6]=0.1389 sum(y==7)/10000ans =0.1705 p[7]=0.1667 sum(y==8)/10000ans =0.1407 p[8]=0.1389 sum(y==9)/10000ans =0.1095 p[9]=0.1111 sum(y==10)/10000ans =0.0794 p[10]=0.0833 sum(y==11)/10000ans =0.0585 p[11]=0.0556 sum(y==12)/10000ans =0.0265 p[12]=0.0278
Sum of Two Dies for i=2:12 z(i)=sum(y==i)/10000 end bar(z)