Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

function - Matlab ode45. How to change a parameter inside it while calling it?

I'm new with Matlab. I hope you can help me. I have to solve a system of ODEs using ODE45 function. Here is the function which describes my equitions.

function dNdt = rateEquations(t, y)
  %populations of corresponding state
  Ng = y(1);
  Ns = y(2);
  Nt =  y(3);

  %All constants used are dropped for the sake of easy reading.

Note the parameter F.

  %rate equations
  dNs = s0 * Ng * F - Ns/ t_S1;
  dNt = Ns / t_ISC - Nt / t_T1;
  dNg = -dNt - dNs;

  dNdt = [dNg; dNs; dNt];

end

Then, in my script .m-file i call the ode45 function in 'for loop'. During each iteration i have to change the parameter F and pass it to my 'rateEquations' - function. But i don't know how to realize it.

for T = Tmin: dt : Tmax
  %initial conditions
  initialConditions = [N0 0 0];
  timeSpan = [T T+dt];

before calling ODE45 F is to be changed.

  [t,N] = ode45('rateEquations', timeSpan, initialConditions)

and so on ...

end

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You want make F an argument of your derivative function and pass the right anonymous function to ode45:

[t,N] = ode45(@(t,y) rateEquations(t,y,F), timeSpan, initialConditions)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...