Help with Octave for system of ODEs

In summary, the author is having trouble with Octave and is asking for help. They list a problem and explain how Python solves the same problem. They also show a plot of the same data.
  • #1
McLaren Rulez
292
3
Hi,

I am having a lot of trouble with Octave as I try to solve a system of ODEs. Any help is appreciated, I am a complete newbie with Octave and numerical solving.

Let's try a very simple one. Suppose I had a pair of ODEs with a and b being functions of time

[tex] \frac{da}{dt}=2ba[/tex]
[tex]\frac{db}{dt}=1[/tex]
Initial conditions are a(0)=1, b(0)=0

This is clearly the solved by [tex]a(t)=e^{t^{2}}[/tex] [tex]b(t)=t[/tex] My Octave code was this:

function xdot=f(x,t);
xdot=zeros(2,1)
xdot(1)=2*x(1)*x(2)
xdot(2)=1
endfunction

t=linspace(0,10,100);
x=lsode("f",[1;0],t)

I want to plot a(t) against t or b(t) or some combination of a and b against t. Here are my issues

1) The t=linspace() part. What numbers are appropriate? Sometimes, I got an error saying convergence failure but this combinations worked through blind luck. In general, what should I choose and why does it seem to matter? As I understand, this tells Octave to take t from 0 to 10 and have 100 intervals. I thought any numbers there would have worked?

2) This is more important. I tried plot(t,x(1)) but I got a blank plot. plot(t,x(2)) also gave me a blank plot. plot(t,x) gave me something but it's really weird. Isn't x now a column vector? I'm not sure what exactly lsode outputs here. What should be the correct command to get a(t) against t, which must of course be an exponential t squared against t graph?

There's also the fact that when I do it for my actual set of ODEs which are slightly more complicated, it inevitably hits an error or gets something 'x' undefined at a certain column and certain line. I'm quite lost :(

Thank you for you help.
 
Last edited:
Physics news on Phys.org
  • #2
Well...don't know ODEs nor Octave, but a quick look at python's scipy revealed a similar function.

Code:
import numpy as np
from scipy.integrate import odeint

import matplotlib.pyplot as plt

def dxdt(x,t):
    xdot = np.zeros(2)
    xdot[0] = 2.0*x[0]*x[1]
    xdot[1] = 1.0
    return xdot
    
t = np.linspace(0,5,51)
x = odeint(dxdt, [1.0,0.0], t)

fig = plt.figure()

ax1 = fig.add_subplot(211)
ax1.plot(t,x[:,0])
ax1.set_ylabel('a(t)')

ax2 = fig.add_subplot(212)
ax2.plot(t,x[:,1])
ax2.set_xlabel('t')
ax2.set_ylabel('b(t)')

plt.show()

See attached plot, too.
 

Attachments

  • ab-ode.png
    ab-ode.png
    6.6 KB · Views: 547

Related to Help with Octave for system of ODEs

1. How do I define a system of ODEs in Octave?

To define a system of ODEs in Octave, you can use the odepkg package and the ode function. First, load the package by typing pkg load odepkg in the Octave command window. Then, use the ode function to define your system of ODEs. For example, if you have a system of two ODEs, dy/dt = f(y,z) and dz/dt = g(y,z), you can define it as function dy = my_odes(t,y) and dy = [f(y,z); g(y,z)]. You can then use this function in the ode function to solve the ODE system.

2. How do I solve a system of ODEs in Octave?

To solve a system of ODEs in Octave, you can use the ode function. This function takes in the ODE system, initial conditions, and a time vector as inputs. For example, if you have a system of two ODEs, dy/dt = f(y,z) and dz/dt = g(y,z), you can solve it by typing [t,y] = ode(@my_odes, tspan, y0) in the Octave command window, where tspan is the time vector and y0 is the initial condition vector.

3. How do I plot the solution of a system of ODEs in Octave?

To plot the solution of a system of ODEs in Octave, you can use the plot function. This function takes in the time vector and the solution vector as inputs. For example, if you have solved a system of two ODEs and stored the solution in a variable y, you can plot it by typing plot(t,y) in the Octave command window. You can also use the plot function to plot specific components of the solution vector, such as y(:,1) for the first component and y(:,2) for the second component.

4. How do I change the solver used for solving a system of ODEs in Octave?

To change the solver used for solving a system of ODEs in Octave, you can use the odeset function. This function allows you to set various options for the ode function, including the solver type. For example, if you want to use the ode23 solver instead of the default ode45 solver, you can type options = odeset('solver','ode23') in the Octave command window before calling the ode function.

5. How do I solve a system of ODEs with additional parameters in Octave?

To solve a system of ODEs with additional parameters in Octave, you can pass the parameters as additional arguments in the ode function. For example, if you have a system of two ODEs, dy/dt = f(y,z) and dz/dt = g(y,z), and you want to pass in a parameter a to the ODE system, you can define your ODE function as function dy = my_odes(t,y,a) and use it in the ode function as [t,y] = ode(@my_odes, tspan, y0, a). You can also use the odeset function to pass in parameters and specify their values.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
292
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top