+ All Categories

lec10

Date post: 15-Nov-2015
Category:
Upload: manishtopsecrets
View: 213 times
Download: 0 times
Share this document with a friend
Description:
lecture
Popular Tags:
25
Ordinary Differential Equations
Transcript
  • Ordinary Differential Equations

  • OutlineAnnouncements:Homework II: Solutions on webHomework III: due Wed. by 5, by e-mailStructsDifferential EquationsNumerical solution of ODEsMatlabs ODE solversOptimization problemsMatlabs optimization routines

  • Stringscharcell={Greetings; People of Earth}disp(charcell) will output each cell on a new lineGreetingsPeople of EarthSearch lines using strmatchstrmatch('Greetings',charcell)Encapsulating dataFFTcell={a, b, dt}myfft could return a cell array in this formWould keep the related data togetherBut, user would have to know what is in each cellApplications of Cell-arrays

  • StructsRather than storing data in elements or cells, structs store data in fieldsBetter encapsulation than cellFFTstrct.a=a;FFTstrct.b=b;FFTstrct.dt=dt;Can have arrays of structsfor j=1:nstrctarr(j)=StrctFunc(inputs);endeach element must have same fields

  • Working with StructsLots of commands for working with structsfieldnames(S)--returns a cell array of strings containing names of fields in Sisfield(S,fname)--checks whether fname is a field of Srmfield(S,fname)--removes fname

  • Differential EquationsOrdinary differential equations (ODEs) arise in almost every fieldODEs describe a function y in terms of its derivativesThe goal is to solve for y

  • Example: Logistic GrowthN(t) is the function we want (number of animals)

  • Numerical Solution to ODEsIn general, only simple (linear) ODEs can be solved analyticallyMost interesting ODEs are nonlinear, must solve numericallyThe idea is to approximate the derivatives by subtraction

  • Euler Method

  • Euler MethodSimplest ODE scheme, but not very good1st order, explicit, multi-step solverGeneral multi-step solvers:(weighted mean of f evaluated at lots of ts)

  • Runge-Kutta MethodsMulti-step solvers--each N is computed from N at several timescan store previous Ns, so only one evaluation of f/iterationRunge-Kutta Methods: multiple evaluations of f/iteration:

  • ODE SolversLMS and RK solvers are general algorithms that can work with any ODE Need a way to pass the function f to the solverSolverN0 t0 TN1, N2,,NT

  • Passing FunctionsOne soln: force us to call our function fA better soln: pass the name of the function to the solverfuncname=myfsolver uses feval commanddN=feval(funcname,N,t);This is Matlab ca. 1998

  • Passing FunctionsNow, feval accepts function handles@myf creates a handle to myf.mSlightly faster than stringsNow, how do we solve the ODEs?

  • Matlabs ODE solversMatlab has several ODE solvers:ode23 and ode45 are standard RK solversode15s and ode23s are specialized for stiff problemsseveral others, check help ode23 or book

  • All solvers use the same syntax:[t,N]=ode23(@odefile, t, N0, {options, params })odefile is the name of a function that implements ffunction f=odefile(t, N, {params}), f is a column vectort is either [start time, end time] or a vector of times where you want NN0= initial conditionsoptions control how solver works (defaults or okay)params= parameters to be passed to odefileMatlabs ODE solvers

  • ParametersTo accept parameters, your ode function must be polymorphicf=odefile(t,x);f=odefile(t,x,params);

    function f=odefile(t,x,params);if(nargin

  • Matlab functions can be polymorphic--the same function can be called with different numbers and types of argumentsExample: plot(y), plot(x,y), plot(x,y,rp);In a function, nargin and nargout are the number of inputs provided and outputs requested by the caller.Even more flexibility using varargin and varargoutnargin

  • Example: Lorenz equationsSimplified model of convection cells

    In this case, N is a vector =[x,y,z] and f must return a vector =[x,y,z]

  • OptimizationFor a function f(x), we might want to knowat what value of x is f smallestlargest?equal to some value?All of these are optimization problemsMatlab has several functions to perform optimizationSame problem as with ODE: solver needs to work with YOUR functionsMust pass function handle

  • OptimizationSimplest functions arex=fzero(@fun, x0);%Finds f(x)==0 starting from x0x=fminbnd(@fun, [xL, xR]); %Finds minimum in interval [xL,xR]x=fminsearch(@fun,x0); %Finds minimum starting at x0All of these functions require you to write a function implementing f:function f=fun(x);%computes f(x)

  • Optimization ToolboxMore optimization techniques, but used in the same waySome of these functions can make use of gradient informationin higher dimensions, gradient tells you which way to go, can speed up procedurefdf/dx = 0

  • Optimization ToolboxTo return gradient information, your function must be polymorphic:x=fun(x) %return value[x,dx]=fun(x); %return value and gradient

    function [x,dx]=fun(x);if(nargout>1)dx=endx=

  • Optimization OptionsNeed to tell optimization toolbox that it can use gradientUse optimset:opts=optimest(ParameterName,value);Then pass opts to optimization functionLots of options--read docs!opts=optimset(GradObj,on);%Turns gradient onx=fminunc(@fun, x0,opts);%pass opts to function

  • SummaryODE solvers & optimization routines in Matlab are function functionsYou must write a function returningdx/dt (ODE)f (Optimization)You pass the name of the function to the solverSolver calls it repeatedly and returns answer


Recommended