MatLab Assignment Z -- Turn in on April 14 (Wednesday)


MatLab graphing facilities: plot, subplot, title
MatLab functions: ode23, ode45
If you have any question or problem with MatLab, please send me an email at: chenm or call me at: 953-7896.

  1. Take a few minutes to read the MatLab handout (4) to learn about MatLab graphing facilities: plot, subplot, title. Now we are going to use these functions to plot the input and output of a RLC-circuit or a mass-spring system.

  2. Here is an example. Recall that in class we solved the following problem:
        I''+4I'+20I=E'(t), where E(t)=85sin(4t), I(0)=0 and I'(0)=0. 
    
    The solution of this initial value problem is:
        I(t)=e-2t(-cos(4t)-4.5sin(4t))+cos(4t)+4sin(4t).
    
    Observe that as t is getting larger and larger, the term e-2t is getting closer and closer to zero, and the solution becomes
        Iss(t)=cos(4t)+4sin(4t)
    
    which is called the steady-state solution of I(t). The following commands are used to plot the graphs of the input E(t) and outputs I(t) and Iss(t) for 3 periods. (Recall the period of functions sin(bt) and cos(bt) is 2pi/b. So the period for sin(4t) is pi/2.)
        clf   %clear the screen
        t=0:.01:3*pi/2;
        cur=exp(-2*t).*(-cos(4*t)-4.5*sin(4*t))+cos(4*t)+4*sin(4*t);
        sscur=cos(4*t)+4*sin(4*t);
        volt=85*sin(4*t);
        subplot(211),plot(t,volt)
        title('RLC - circuit, voltage E(t)=85sin(4t)')
        subplot(212),plot(t,cur,'--',t,sscur,':')
        title('-- current I(t), ... current in the steady-state')
    
    The graphs are given at the end of this assignment.

  3. Here are your first two assignments.
  4. MatLab has functions ode23 and ode45 which solve numerically initial value problems for first order differential equations y'=f(t,y), y(a)=y0 or systems of first order differential equations Y'=F(t,Y), Y(a)=Y0. Type help ode23 and help ode45 to learn more about these two functions. Here is an example which uses ode23 and ode23 to solve the initial value problem:
    y'=-y/sqrt(2-y2), y(0)=1, 0 <= t <= 5.
    
    First we create a MatLab function (a text file) called fun1.m which evaluates the function f(t,y)=-y/sqrt(2-y2) for give (t,y). The file fun1.m is as follows.
       function f=fun1(t,y)
       f=-y/sqrt(2-y^2);
    
    Now we use MatLab functions ode23 and ode45 to solve the initial value problem numerically and then plot the numerical solutions y, respectively. In the MatLab window,
      >> clf
      >> clear
      >> [tv1 f1]=ode23('fun1',[0 5],1);
      >> [tv2 f2]=ode45('fun1',[0 5],1);
      >> plot(tv1,f1,'-.',tv2,f2,'--')
      >> title('dy/dt=-y/sqrt(2-y^2), y(0)=1')
    
    The graph is given below. Try it yourself to see if you will get the same graph.

  5. Here is your last part of the assignment. Solve the following initial value problem using MatLab functions ode23 and ode45 and plot the numerical solutions.
       y'=ty+2t-y^3, y(0)=0, 0 <= t <= 4.5.
    
    Are solutions obtained by these functions close? Graphs of the examples: