MatLab Handouts (2) --- MatLab Functions: int, subs, syms, ezplot, ode23, ode45
--- Picard's Method and the Runge-Kutta Method


  1. A Short-cut for Constructing Symbolic Objects.
    MatLab function syms sets a given letter to a symbolic object. For example,
        >> syms x t alpha beta
    
    Now x,t,alpha,beta can be used as variables in functions.

  2. Symbolic Substitution:
    MatLab function subs is designed for the symbolic substitution. Let g be a function in x. Then subs(g,x,t) replaces x in g by t. For example,
        >> syms x t 
        >> g=sin(x)+x^2+exp(-x)
           g = 
           sin(x)+x^2+exp(-x)
        >> subs(g,x,t)
           g = 
           sin(t)+t^2+exp(-t)
    
    Type help subs to learn more about this function.

  3. Symbolic Integration:
    MatLab function int is designed for the symbolic integration. Let g be a function in x. Then int(g,x,a,b) is the definite integral of g with respect to x from a to b. For example,
        >> syms x t a b
        >> g=sin(x)+x^2+exp(-x);
        >> int(g,x,0,b)
           (-cos(b)*exp(b)+1/3*b^3*exp(b)-1)*exp(-b)+2
        >> int(g,x,a,b)
           (-cos(b)*exp(b)+1/3*b^3*exp(b)-1)*exp(-b)
           +(cos(a)*exp(a)-1/3*a^3*exp(a)+1)*exp (-a)
    
    Type help int to learn how to find indefinite integrals and learn more about this function.

  4. Sketch the Graph of a Function over an interval [a,b]:
    ezplot(f,xmin,xmax) or ezplot(f,[xmin,xmax]) plots the graph of f(x) where f is a string or a symbolic expression representing a mathematical expression involving a single symbolic variable x. For example, the graph of f=-(xv+1)+4*exp(xv-1) for x in [1,5] can be generated by the following MatLab commands.
        >> syms x
        >> f=-(xv+1)+4*exp(xv-1); 
        >> ezplot(f,1,5)
    
    Type >>help ezplot to learn more about this function.

  5. Runge Kutta Methods:
    We will cover the topic of the Runge Kutta Method in class later. MatLab functions ode23 and ode45 implement the 2nd & 3rd order Runge Kutta Method and the 4th & 5th order Runge Kutta Method, respectively, for solving numerically the initial value problem:
         y'=f(x,y),  y(x0)=y0.
    
    Consider the initial value problem: y'=x+y, y(1)=2. Create a m-function, say funode.m, to evaluate f(x,y)=x+y:
         function f=funode(x,y);
         f=x+y;
    
    The following MatLab scripts use ode45.m to compute a sequence of { y1, ..., yn } where yi approximates y(xi) for xi in [1,b] (x0=1, b is given).
         >> [xv,yv]=ode45('funode',[1,b],2)
    
  6. Picard's Method:
    Now we can use above MatLab functions to compute approximations of the solution of a initial value problem
         y'=f(x,y),  y(x0)=y0
    
    by Picard's Method. Recall that the Picard Method generates a sequence of approximations: y1(x), y2(x), .... Review your class notes on Picard's Method if it is necessary.

    Consider the initial value problem: y'=x+y, y(1)=2. The solution of this problem is: y=-(x+1)+4ex-1. The following MatLab scripts compute the approximations y1(x), y2(x), y3(x) and y4(x) of y using Picard's Method and plot the graphs of y1(x), y2(x), y3(x), y4(x) and y for x in [1,5]; and also plot { y1, ..., yn } computed by MatLab function ode45.m.
        syms x t;
        y0=2;
        x0=1;
        f=t+y0;
        y1=int(f,t,x0,x)+y0;
        f=t+subs(y1,x,t);
        y2=int(f,t,x0,x)+y0;
        f=t+subs(y2,x,t);
        y3=int(f,t,x0,x)+y0;
        f=t+subs(y3,x,t);
        y4=int(f,t,x0,x)+y0;
        ysol=-(x+1)+4*exp(x-1);
        [tt,yy]=ode45('funode',[1,5],2);
        ezplot(y1,1,5)
        hold
        ezplot(y2,1,5)
        ezplot(y3,1,5)
        ezplot(y4,1,5)
        ezplot(ysol,1,5)
        plot(tt,yy,'-.')
        hold off
        title('dy/dx=x+y, y(1)=2, Picard Method')
        text(1.2, 200,'solution y=-(x+1)+4e^{(x-1)}')
        text(1.2, 170,'y_1,y_2,y_3,y_4 -> solution')
        text(1.2, 140,'-. y_5 obtained by ODE45')
        text(4.5,150,'y')
        text(4.5,110,'y_4')
        text(4.5,80,'y_3')
        text(4.5,50,'y_2')
        text(4.5,25,'y_1')
    
    Here is the graph: