Question

I know I can use Octave built-in ODE solver to solve a ODE system. Now I want to solve the multiple ODE systems of the same structure but with different coefficients. For example, I have the following ODEs:

function xdot=f(x,t)

a = 0.1;

xdot(1) = a * x(1) ^ 2 + x(2);
xdot(2) = x(1) - x(2);

endfunction

Initial conditions x(1)=0, x(2)=0 at t=0. I want to get x(1) and x(2) at point t=1. I know I can solve this easily by using lsode command.

Now I want to change the value "a" from 0.1 to 20 with step 0.1, that is a=[0.1:0.1:20]. So I can get x(1) and x(2) from different "a". How do I do this in a loop? Do I need a global variable "a"?

Was it helpful?

Solution

I can't say I've ever used lsode before, but when I've had a variable in a function that I've wanted to change, I've had to declare it a global variable and then redefine the function. Something like

%doesn't work
f = @(x) a*x+2
for a= 0.1:0.1:20
  f(1)
end

%does work  
for a= 0.1:0.1:20
  f = @(x) a*x+2 %your function here
  %I usually end up using arrayfun() here in my applications, maybe lsode would work?
end  

I'm no Octave expert, but this has worked for me, so maybe it'll work for you too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top