home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / ODEDEMO.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.5 KB  |  70 lines

  1. %ODEDEMO Demonstrate numerical integration of differential equations.
  2. echo on
  3. clc
  4. %   ODE23 and ODE45 are functions for the numerical solution of
  5. %   ordinary differential equations.  They employ automatic step
  6. %   size Runge-Kutta-Fehlberg integration methods.  ODE23 uses a
  7. %   simple 2nd and 3rd order pair of formulas for medium accuracy
  8. %   and ODE45 uses a 4th and 5th order pair for higher accuracy.
  9. %   This demo shows their use on a simple differential equation.
  10.  
  11. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  12.  
  13. pause   % Strike any key to continue.
  14. clc
  15. %   Consider the pair of first order ordinary differential equations
  16. %   known as the Lotka-Volterra predator-prey model.
  17.  
  18. %             y1' = (1 - alpha*y2)*y1
  19. %             y2' = (-1 + beta*y1)*y2
  20. %
  21. %   The functions y1 and y2 measure the sizes of the prey and predator
  22. %   populations respectively.  The quadratic cross term accounts for the
  23. %   interations between the species.  Note that the prey population
  24. %   increases when there are no predators, but the predator population
  25. %   decreases when there are no prey.
  26.  
  27. %   To simulate a system, we create a function M-file that returns
  28. %   state derivatives, given state and time values.  For this
  29. %   example, we've created a file called LOTKA.M.  Here's what it
  30. %   looks like:
  31. type lotka
  32. pause   % Strike any key to continue.
  33. clc
  34. %   To simulate the differential equation defined in LOTKA over the
  35. %   interval  0 < t < 15, we invoke ODE23:
  36.  
  37. t0 = 0;
  38. tfinal = 15;
  39. y0 = [20 20]';   % Define initial conditions.
  40.  
  41. % [t,y] = ode23('lotka',t0,tfinal,y0);
  42.  
  43. pause   % Strike any key to start ODE23 solution.
  44.  
  45. tol = 1.e-3;    % Accuracy
  46. trace = 1;
  47. tfinal = tfinal*(1+eps);
  48. [t,y] = ode23('lotka',t0,tfinal,y0,tol,trace);
  49. plot(t,y), title('Lotka-Volterra equation time history'), pause
  50. plot(y(:,1),y(:,2)), title('Lotka-Volterra equation - phase plane plot'),pause
  51. clc
  52. %   We will now simulate LOTKA using ODE45, instead of ODE23.
  53. %   ODE45 takes longer at each step, but also takes larger steps.
  54. %   (On some computers ODE23 and or ODE45 may be hard-coded in
  55. %   MEX-files for speed, which makes time comparisons difficult).
  56.  
  57. % [T,Y] = ode45('lotka',t0,tfinal,y0);
  58.  
  59. pause   % Strike any key to start ODE45 solution.
  60.  
  61. [T,Y] = ode45('lotka',t0,tfinal,y0,tol,trace);
  62.  
  63. plot(T,Y), title('Lotka-Volterra equation time history'), pause
  64.  
  65. plot(y(:,1),y(:,2),'-',Y(:,1),Y(:,2),'-')
  66. xlabel('ODE45 steps are larger than ODE23 steps'), pause
  67.  
  68. echo off
  69. disp('End')
  70.