Lecture 4 Advanced Matlab Programming
Announcements Hands-on Session on Friday 1318 EB Read Chapters 3-6 in your MATLAB book HW 2 opens up Friday evening
Today Numerical analysis - I Visualization I Some more programming
Advanced Matlab Programming Functions Can have variable number of inputs and outputs (see: nargin, nargout, varargin, varargout) Can have internal functions Data types: more than just arrays and strings: Structures Cell arrays File handling Supports most C-commands for file I/O (fprintf, )
MATLAB Compiler Converts function M-files to C or C++ source code (and executables) Uses native compiler to create executables Creates MEX-file from function M-file, e.g. function.m >>mcc x function Creates stand-alone a executable ecutab e from function M-file >>mcc m function
Matlab Compiler
Matlab Compiler
Visualization Matlab is great for plotting functions x = [0:0.01:2*pi]; y = cos(x); () plot(x,y)
Visualization Add titles to graphs and axis title( this is the title ) xlabel( x ) x ylabel( y )
Visualization Multiple plots plot(x,y1, b-,x,y2, r: ) legend( y1, y2 )
Visualization Matlab can also do 3D plots, such as mesh surfaces: [X,Y] = meshgrid(-2:.2:2, -2:.2:2); Z = X.* exp(-x.^2 - Y.^2); mesh(x,y,z)
Visualization Or simple 3D plots: x = -2:.02:2; y = x; z = x.* exp(-x.^2 - y.^2); Plot3(x,y,z,. )
Visualization All calls to plot, plot3, mesh, etc. will overwrite on the current figure. Make sure that you want to erase the current figure. To create a new empty figure, use the command figure
Visualization Adding figures to reports: Print the figure directly Save it to a TIFF file and add to the report (File Export ) Copy to clipboard and paste to the report (Edit Copy Figure) The background is copied too! By default it is gray. e bac g ou d s cop ed oo y de au s g ay To change the background color use: set(gcf, color, white )
Save and Load functions save <filename> saves everything in the workspace (or the function variable space) to <filename>.mat. mat If no <filename> is given, saves to matlab.mat. load <filename> loads the file.
Advanced Read and Write For formatted read and write, learn how to use the fucntions: fprintf fscanf fopen fclose >> x = 0:.1:1; >> y = [x; exp(x)]; >> fid = fopen('exp.txt','w'); t' ' ') >> fprintf(fid,'%6.2f %12.8f\n',y); >> fclose(fid)
Numerical Methods Finding roots of equations Solving matrices (Linear Algebra) Regression / Curve-fitting Numerical Integration Solving ODE s More advanced topics ( solving PDEs) for later
Numerical Integration Use the routines trapz, quad >> help trapz >> help quad (Simpson s Rule) Other useful functions: SUM, CUMSUM, DBLQUAD, TRIPLEQUAD
Examples: Evaluate the following definite integrals
Initial Value Problems (IVPs) Very important topic a large number of practical problems in science and engineering g fall into this category The solution of IVPs (and most ODEs in general) can be challenging g if you do not have the right tools Most realistic systems involve ODEs that must be solved numerically MATLAB has a suite of tools (e.g., ode45)
Examples Imagine you parachute out of a plane v = downward velocity m = mass C = drag coefficient ce Initial Condition: v(0) = 0 dv m mg Cv dt = 2
Microbial uptake To describe microbial utilization of a substrate: dc X C = k a dt K + C S
Systems of Differential Equations dy dt dy dt dy dt dy 1 2 3 n dt y1(0), y2(0), y3(0) are known = f (, t y, y, y y ) 1 2 3 = f (, t y, y, y y ) 1 2 3 = (,,, ) f t y 1 y 2 y 3 y n = f (, t y, y, y y ) 1 2 3 n n n
The Runge-Kutta Algorithm Consider the simple first order equation: dy = f(, t y) dt y (0) = a 1 1 1 1 y = k 1 y + + k δ1 δ2 δ3 δ4 6 + 3 + 3 + 6 δ =Δtf( t, y) 1 k k 1 1 δ2 =Δ tf ( tk + Δ t, yk + δ1) 2 2 1 1 δ 3 = Δ t f ( t k + Δ t, y k + δ 2 ) 2 2 δ =Δ tf( t +Δ t, y + δ ) 4 k k 3
y k Example 1 dy 2 3 Solve: = 2y + e dt y (0) = 1 = 1 1 1 1 1 y + k δ1 2 3 4 6 + + δ δ δ 3 + 3 + 6 δ t t t 1 =Δ f y =Δ y + t 2 ( k, k ) 2 k exp( 3 k) 2 1 1 1 1 δ2 =Δ tf( tk + Δ t, yk + δ1) =Δt 2 yk + δ1 + exp( 3 tk + Δt ) 2 2 2 2 2 1 1 1 1 δ3 =Δ tf( tk + Δ t, yk + δ2) =Δt 2 yk + δ2 + exp( 3 tk + Δt ) 2 2 2 2 t 2 ( ) ( ) δ4 =Δ tf( tk +Δ t, yk + δ3) Δt 2 yk + δ3 + exp( 3 tk +Δt )
eek! too many details!
The same thing in MATLAB Suppose you want to solve the DE: dy 2 2y exp( 3 t); y(0) 1 from t =0 to 5; dt = + = Step 1 Create a function (an.m m file) called myode.m m with the 2 lines function dydt=myode(t,y); dydt=-2*y^2+exp(-3*t); Step 2 Type the following command in MATLAB: [t,y]=ode45('myode',[0,5],1); plot(t,y); 1 2 1.5 0.5 Quite Simple, my dear Watson! 0 0 5 10 15 20 Adventures of Sherlock Holmes --Sir Arthur Conan Doyle
Solving a system of two differential equation (DE) using MATLAB: Two Lakes in Series (Chapra, page 128) dc 1 dt dc2 dt = λ c 11 1 = λ c λ c 21 1 22 2 10 8 6 4 2 Step1. Create the function twolakes.m function [ypr] = twolakes(t,y); 0 0 0.2 0.4 0.6 0.8 1 ypr = zeros(2,1); % This is important! It preallocates a vector with two elements for c1 and c2. ypr(1)=-2.0*y(1); ypr(2)=1.667*y(1)-1.667*y(2); Step2: Type in MATLAB: >> tspan = [0 1]; y0 = [10,0.0];[t,y]=ode45('twolakes',tspan,y0);plot(t,y);