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)

 Risposta accettata

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 Commenti

Thank you for the quick response. It works now
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by