How can I parametrize functions and then apply ode45 to this function?
When I explicitly give all parametrs as numbers ode45 runs fine, however when I change to variables MatLab has insufficient Input Arguments.
This Runs fine:
function [dx] = NI(t,x,sigma)
rho=0.01;
sigma=0.5;
a=0.3;
c=(9*a^2*rho*sigma+sqrt(3)*sqrt(4*a^6*rho^3+27*a^4*rho^2*sigma^2))^(1/3);
zeta=c/((2*9)^(1/3)*a)-(2/3)^(1/3)*a*rho/c;
thau=(1+a)/(a*zeta)*(rho*sigma-a*rho*zeta-a*sigma*zeta^2/(1+a));
dx=[-sigma^2*(x(1)^3-x(1)^2)+(rho+thau)*x(1)-rho-rho*x(2);(zeta^2-sigma^2*x(1)^2)*x(2)];
end
% followed in seperate script by
Time=[0,200];
x0=[0,0];
sigma=0.5;
[t,x]=ode45(@NI,Time,x0,sigma);
plot(x)
This gives the error:
function [dx] = NI(t,x,sigma)
rho=0.01;
a=0.3;
c=(9*a^2*rho*sigma+sqrt(3)*sqrt(4*a^6*rho^3+27*a^4*rho^2*sigma^2))^(1/3);
zeta=c/((2*9)^(1/3)*a)-(2/3)^(1/3)*a*rho/c;
thau=(1+a)/(a*zeta)*(rho*sigma-a*rho*zeta-a*sigma*zeta^2/(1+a));
dx=[-sigma^2*(x(1)^3-x(1)^2)+(rho+thau)*x(1)-rho-rho*x(2);(zeta^2-sigma^2*x(1)^2)*x(2)];
end
% followed in seperate script by
Time=[0,200];
x0=[0,0];
sigma=0.5;
[t,x]=ode45(@NI,Time,x0,sigma);
plot(x)

 채택된 답변

Star Strider
Star Strider 2021년 3월 11일

0 개 추천

Change the ode45 call to:
[t,x]=ode45(@(t,x)NI(t,x,sigma),Time,x0,sigma);
so the complete code is now:
function [dx] = NI(t,x,sigma)
rho=0.01;
a=0.3;
c=(9*a^2*rho*sigma+sqrt(3)*sqrt(4*a^6*rho^3+27*a^4*rho^2*sigma^2))^(1/3);
zeta=c/((2*9)^(1/3)*a)-(2/3)^(1/3)*a*rho/c;
thau=(1+a)/(a*zeta)*(rho*sigma-a*rho*zeta-a*sigma*zeta^2/(1+a));
dx=[-sigma^2*(x(1)^3-x(1)^2)+(rho+thau)*x(1)-rho-rho*x(2);(zeta^2-sigma^2*x(1)^2)*x(2)];
end
% followed in seperate script by
Time=[0,200];
x0=[0,0];
sigma=0.5;
[t,x]=ode45(@(t,x)NI(t,x,sigma),Time,x0,sigma);
plot(x)
See the documentation section on Passing Extra Parameters for a detailed discussion.

댓글 수: 2

Lukas Mevissen
Lukas Mevissen 2021년 3월 11일
Thank you for the quick response. It works now
Star Strider
Star Strider 2021년 3월 11일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2021년 3월 11일

댓글:

2021년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by