Input argument for ode45 function type error

조회 수: 1 (최근 30일)
Aleem Andrew
Aleem Andrew 2021년 11월 7일
댓글: Star Strider 2021년 11월 7일
I am trying to modify the following code.
tspan = [0 10];
x0 = 0;
[t,x] = ode45(@(t,x) (-4.76e+1*x^2 + 5.69e-9*x + 7.98e-2)/(1.67e+4*x + 1.12), tspan, x0);
plot(t,x,'b')
xlim([0 0.001])
The input argument to the ode45 function is directly typed in. If instead you write it as
tspan = [0 10];
x0 = 0;
f = (-4.76e+1*x^2 + 5.69e-9*x + 7.98e-2)/(1.67e+4*x + 1.12);
[t,x] = ode45(@(t,x) f, tspan, x0);
plot(t,x,'b')
xlim([0 0.001])
then there is an error, even though the type of the argument is the same. Can anyone explain how the argument can be input as a variable that stores the expression?

채택된 답변

Star Strider
Star Strider 2021년 11월 7일
Yes.
Create it as an anonymous function at the outset —
tspan = [0 10];
x0 = 0;
f = @(t,x) (-4.76e+1*x^2 + 5.69e-9*x + 7.98e-2)/(1.67e+4*x + 1.12);
[t,x] = ode45(f, tspan, x0);
figure
plot(t,x,'b')
grid
xlim([0 0.001])
.
  댓글 수: 4
Aleem Andrew
Aleem Andrew 2021년 11월 7일
Thank you, I appreciate your help
Star Strider
Star Strider 2021년 11월 7일
As always, my pleasure!
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by