Updating the parameter of ODE45

조회 수: 7 (최근 30일)
alpedhuez
alpedhuez 2020년 5월 26일
댓글: Star Strider 2020년 5월 26일
I have an ODE system such as
b=1;
f = @(t,x) -(b/N)*x(1)*x(2);
ODE45
I want to write a loop that changes the value of b. But it does not seem to me that just changing the value of b will not update the equation f thus the solution.
b=2;
f = @(t,x) -(b/N)*x(1)*x(2)
ODE45
What should be done here?
  댓글 수: 2
alpedhuez
alpedhuez 2020년 5월 26일
Maybe Pass Extra Parameters to ODE Function at https://www.mathworks.com/help/matlab/ref/ode45.html#bu3uhuk?
Star Strider
Star Strider 2020년 5월 26일
That is the best solution. Include both constants in the argument list:
f = @(t,x,b,N) -(b/N)*x(1)*x(2);
then proceed as in the documentation example.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2020년 5월 26일
Actually, ‘f’ needs to be a (2x1) matrix, since there are two values of ‘x’, and they both need to be defined, so something like this will work:
f = @(t,x,b,N) [x(1); -(b/N)*x(1)*x(2)];
tspan = [0 5];
ic = rand(2,1);
b = rand;
N = 42;
[t,x] = ode45(@(t,x)f(t,x,b,N), tspan, ic);
I am not certain what you are doing, so this may need to be changed. However to integrate two dependent variables, the ODE function has to have that structure, even if you only use the ‘x(:,2)’ result. I would need to know the original differential equation to know if this is correct for it.
  댓글 수: 4
alpedhuez
alpedhuez 2020년 5월 26일
편집: alpedhuez 2020년 5월 26일
This seems to work:
f = @(t,x,b,d,r,N) [-(b/N)*x(1)*x(2);(b/N)*x(1)*x(2)-(r+d)*x(2);r*x(2);d*x(2)]
Star Strider
Star Strider 2020년 5월 26일
That definitely makes sense!
I am still not certain what you are modeling, however that is definitely the correct way to write the ODE function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by