May 30, 2005 Matlab Tutorial 1. Arrays and Matrices • Row Vector � � � � r = [1 , 2 , 3 , 4] or r = 1 2 3 4 r = 1 2 3 4 • Column Vector 1 2 c = [1; 2; 3; 4] c = 3 4 • Matrix 1 2 3 m = [1 , 2 , 3; 4 , 5 , 6; 7 , 8 , 9] m = 4 5 6 7 8 9 • Transposes � � ctrans = c ′ ctrans = 1 2 3 4 mtrans = m ′ mtrans= • Exercises x1=zeros(3,1) x1= x2=zeros(1,3) x2= x3=zeros(3) x3= y1=ones(1,3) y1= 1
y2=ones(3,2) y2= y3=ones(3) y3= I=eye(3) I= 2. Operators • Matrix operators: +, -, *, /, ˆ, \ • Array (element-by-element) operators: +, -, .*, ./, .ˆ • Example � 1 � 2 A = B = 3 4 C = A ∗ B ; D = A. ∗ B C = D = • Exercises 1 0 2 1 K = , H = 3 2 4 3 and compute if you can the following and check your answers using MATLAB 1) 2 − 3 ∗ 2 2) (2 − 3) ∗ 2 3) K + 2 2
4) 2 − K 5) K − H 6) K ∗ H 7) K. ∗ H 8) K./H 9) K/ 2 3. .m-files • Write programs or subprograms • script - defined variables are global on the workspace • function - defined variables are local (helps keep code easy to read and more portable) • Programs (script m-file) clear all; %clear all variables in MATLAB memory x=0:pi/12:2*pi; % x goes from 0 to 2 π with the stepsize π 12 f=sin(x); • Programs (function m-file) function output =exfunc1(input) 3
x=input;%save input as x f=sin(x); output=f; ⋆ help function • Exercises 1) Create a script m-file to calculate 2sin(x) for x ∈ [ − 2 π, 2 π ] with the stepsize=.01 2) Create a function m-file to calculate f=bsin(x) for x ∈ [ − 2 π, 2 π ] with the stepsize=.01 where the input is b and the output is f. Run the program for b=2,3,5. 4. Programming Syntax • The ’for’ loop N=1000; M=50; for i=1:N for j=1:M x(i,j)= i + j 2 ; end end ⋆ help for • The ’if’ statement i=1; j=1; if i==j x=1; else x=100; 4
end • Other Boolean Operators: ˜ (NOT); < =; > =; < ; > ⋆ help if 5. Statistical Tool Box v =[1; 2; 3; 4] • mean(v); • var(v); • std(v); 6. Graphical Results (Plotting) x=0:pi/12:2*pi; % x goes from 0 to 2 π with the stepsize π 12 f=sin(x); g=cos(x); • Plot the function against the values plot(x,f,’r’);% plot sin(x) with a red line xlabel(’x’); % labeling the x-axis ylabel(’f’); % labeling the y-axis title(’Plot of sin(x)’); % title of the graph • Plot multiple functions on the same figure plot(x,f,’- -’,x,g,’r’);% plot sin(x) with a dashed line and cos(x) with a red line xlabel(’x’); % labeling the x-axis ylabel(’y’); % labeling the y-axis title(’Plot of sin(x) and cos(x)’); % title of the graph 5
legend(’sin(x)’,’cos(x)’ ); % give a legend to match the plots ⋆ help plot or run demos 7. ODE Solvers The main command: [t,y]=ode23(@fun,[t0 tf],y0,options,a1,a2,...) • Input - Name of a function (@fun) - Timespan ([t0 tf]) - Initial conditions (y0) - Options for ODE solver (options=(’AbsTol’,1e-4,’RelTol’,1e-4)) - Values to pass to the function • Output - The variable timestep values of t - Values of solution at corresponding values of t (y(t)) The function where ODE is written out: function dy=fun(t,y,flag,a1,a2,...,aN) • Function dy evaluates y ′ = f ( t, y ( t )) • Input - Values of t and y to evaluate y ′ - flag is the place holder for options - a1,...,aN are extra values to pass into the function besides t, y Higher Order of ODE • Rewrite ODE as system of first order ODE: y ′ = Ay + b 6
• Pass values in A and b as a1,...,aN Matlab Codes tspan=0:.01:5; %define tspan from 0 to 20 y0=1; [t,y]=ode23(@odefun,tspan,y0); plot(t,y);% plot the results function dy=odefun(t,y) dy=t*y; Other ODE solvers: ode45, ode113, ode23s, ode15s ⋆ help ode45; help ode23, etc.; run demos • Exercises: Write a program to solve ¨ y − 3 ˙ y − 4 y = 0, ˙ y (0) = 2, y (0) = 1 from [0,10]. Plot y(t). 8. Optimization Methods (fminsearch) • Find a local minimizer X of the function FUN • FUN accepts input X and returns a scalar value evaluated at X • X can be a scalar, vector, or matrix • Matlab commands: X = fminsearch (@ FUN, X 0 ); % find a value X to minimize the funtion FUN with the initial guess X 0 function f=FUN(x) f = cos ( x ) ∗ exp (2 ∗ x 2 );% function that we want to minimize 7
⋆ help fminsearch; demos • Exercises: Using fminsearch, find a minimizer of x 2 9. Data Saving and Loading Matlab command: save(’filename.dat’,’variablename’);%saving %variablename to filename.dat s=importdata(’filename.dat’); %save the contents of filename in s ⋆ help load m=[5;6;7;8]; save(’data.dat’,’m’);%saving m to file data.dat Exercises: load up the file data.dat into variable z 8
Recommend
More recommend