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)

 Réponse acceptée

Star Strider
Star Strider le 11 Mar 2021

0 votes

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 commentaires

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by