MATLAB TUTORIAL. Part 1.2: Plotting Solutions to ODEs

Prof. Vladimir A. Dobrushkin

This tutorial contains many matlab scripts.
You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. Any comments and/or contributions for this tutorial are welcome; you can send your remarks to

Email Vladimir Dobrushkin

Plotting Solutions

Solutions to initial value problems for ODEs

An initial value problem can be solved using the ode45 solver then plotted using the plot function.

function initial_value_problem

y0 = 1; xrange = [0, 2]; [x, y] = ode45(@IVP, xrange, y0); plot(x, y, 'k')

hold on

y0 = 1; xrange = [0, -2]; [x, y] = ode45(@IVP, xrange, y0); plot(x, y, 'k') function dydx = IVP(x, y) dydx = -2 * x * y; end end

The ode45 solver requires initial conditions and the interval of integration to be defined. In addition, the differential equation must be defined within an embedded function. The elements in the vector defining the interval of integration must be in order, either increasing or decreasing, and the ode45 solver imposes the initial conditions on the first element in the interval of integration vector. Since in this case the initial conditions corresponds with x = 0 which lies between the interval of integration of -2 to 2, the ode45 solver will need to be called upon twice for two different intervals of integration, one increasing in order from 0 to 2 and one decreasing in order from 0 to -2. This can be done by simply copying and pasting the code, then changing the elements in the interval of integration vector and using the hold on command to plot the solutions produced in both intervals of integration in the same window.

Plotting a sequences of solutions with different initial conditions can be done the same way, except changing the initial conditions values rather than the elements of the interval of integration vector.

y0 = 0.7;
              trange = [0.5, 0.02];
              [t, y] = ode45(@myODE, trange, y0);
            plot(t, y, 'k')

hold on

trange = [0.5, 5]; [t, y] = ode45(@myODE, trange, y0); plot (t, y, 'k') figure y0 = 1; trange = [0.5, 0.02]; [t, y] = ode45(@myODE, trange, y0); plot(t, y, 'k')

hold on

trange = [0.5, 5]; [t, y] = ode45(@myODE, trange, y0); plot (t, y, 'k')

with the differential equation defined in the file myODE:

function dydt = myODE(t, y)
  dydt = (y.^3 - 2 * t * y)./(t.^2);
  end

Plotting a family of solutions requires the use of a for loop but otherwise does not really differ from a typical plot function.

x = linspace(-3, 3);
              for c = -20/3:1:8
  plot(x, x * c + c/sqrt(c * c +1), 'k')
  hold on
  end

The for loop tells matlab to plug in a range of different values for the differential constant c and plot each solution in the same window. A singular solution can be plotted along with a family of solutions by telling matlab to also plot the solution to the initial value problem.

x = linspace(-3.5, 3.5);
              for c = -1:1
  plot(x, (x + c).^2)
  hold on
  end

plot(x, x.^2, 'LineWidth', 2); figure

function  Complete

We plot solutions to the Bernoulli equation \( t^2 \dot{y} = y^3 -2t\,y . \)


s = dsolve('t^2*Dy = y^3 - 2*t*y');
k = 0;
for C1=[0,0.005,0.05,.5,5]
  k = k + 1;
  c=['k';'r';'b']; 
  for i = 2:size(s)
  t = 0:.05:5;
  s1 = eval(s(i));
  plot(t,s1,c(i))
  hold on
  end
end
plot(t,eval(s(1)),'k.') % trivial solution 
legend('S_1','S_2','location','northwest'); 
title('S_1 and S_2 are non-trivial solutions. S_2 = - S_1')
xlabel('t')
ylabel('Solutions')

Example 1.1.2:

II. Plotting


                
Separable equations

Enter text here