# Octave Examples for Numerically Solving ODEs

```MathLab> octave
Octave, version 1.1.1.
Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.
```

## 1. ODE: x'(t)=-exp(-t)*x^2(t); x(0)=2

### Define the ODE:

```octave:1> #
octave:1> # Comment:  Define ODE RHS:
octave:1>  function xdot = f (x,t)
>  xdot=-exp(-t)*x^2;
> endfunction
```

### Numerical Solution on (0,5) Using LSODE (Hindmarsh):

```octave:2> #
octave:2> #  For help see prior Octave HTML page  link, or use:
octave:2> #   "help -i differential (Return); (Space)" for the ODE page.
octave:2> # Numerical Solution on (0,5):
octave:2> #
octave:2> x=lsode("f",2,(t=linspace(0,5,50)'));
octave:3> #
octave:3> # Comment:  Here 50 points are used the inteval [0,5].
octave:3> # Assuming no access to full screen, we get an ascii plot:
octave:3>   set term dumb;
octave:4>   plot(t,x)

2 *+-----+------+------+------+------+------+------+------+------+-----++
*      +      +      +      +      +      +      +      line 1 ****** +
*                                                                     |
1.8 +*                                                                   ++
|*                                                                    |
|*                                                                    |
1.6 ++*                                                                  ++
| *                                                                   |
|  *                                                                  |
1.4 ++  *                                                                ++
|   **                                                                |
1.2 ++    *                                                              ++
|      *                                                              |
|      **                                                             |
1 ++       **                                                          ++
|          **                                                         |
|            ****                                                     |
0.8 ++               ******                                              ++
|                      *************                                  |
+      +      +      +      +      +***********************************
0.6 ++-----+------+------+------+------+------+------+------+------+-----++
0     0.5     1     1.5     2     2.5     3     3.5     4     4.5     5

octave:5>  # For a  nicer, postscript version of the plot
("set term postscript" before "plot"):
```

## 2. Numerical Solution of System of Two ODEs:

### Define the ODE System:

```octave:6> #
octave:6> # Comment:  Define ODE RHSs:
octave:6>  function xdot = f (x,t)
>      r = 0.25;
>      k = 1.4;
>      a = 1.5;
>      b = 0.16;
>      c = 0.9;
>      d = 0.8;
>      xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
>      xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
>  endfunction
octave:7>  #
octave:7> # Using LSODE again, with initial conditions x(1)=1 and x(2)=2 on [0,50] with 200 points:
octave:7>  #
octave:7>  x = lsode ("f", [1; 2], (t = linspace (0, 50, 200)'));
octave:7>   #

octave:8>   set term dumb;
octave:9>   plot(t,x);

2.5 ++-----+------+------+------+------+------+------+------+------+-----++
+      +      +      +      +      +      +      +      line 1 ****** +
|                                                       line 2 ###### |
|                                                                     |
2 #+                                                                   ++
|#                                                                    |
|#                                                                    |
|#                                                                    |
1.5 +#                                                                   ++
|#                                              *********             |
| #                                       ******         ***          |
| #                                    ****                **         |
1 *+#                                  ***                    **       ++
* #                               ***                        **       |
* #                             ***                           **      |
*  #                          ***                              **     |
0.5 *+ ##                       ***                                 ***  ++
|*  #                     ***                                     *****
|*  ##                ****                                   ######   |
+*   ###      +********     +      +      +      +      + #### +   ####
0 ++**************##########################################-----+-----++
0      5     10     15     20     25     30     35     40     45     50

octave:10>  # For a  nicer, postscript version of the plot:
octave:10>   set term postscript;
octave:11>   plot(t,x);
```

### Quit:

```octave:12> quit

MathLab>
```

Web Source: http://www.math.uic.edu/~hanson/OctaveODE-EG.html

Email Comments or Questions to hanson@math.uic.edu