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)

 Respuesta aceptada

Star Strider
Star Strider el 11 de Mzo. de 2021
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 comentarios

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Mzo. de 2021

Comentada:

el 11 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by