How to pass parameters?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marco Sammito
el 2 de Dic. de 2016
Editada: James Tursa
el 2 de Dic. de 2016
Hi. I have to solve this differential equation
where
function piecewise()
x0 = 1;
tspan = linspace(0,3,1000);
[T,X] = ode45(@DE, tspan, x0);
plot(T,X)
end
%
function dX = DE(t,x)
A = log(2);
B = log(3);
C =1;
dX = -13.925*A*B*C*x + f(t);
end
%
function fval = f(t)
if (t <= log(2))
fval = exp(-t);
elseif (t > log(2)) && (t <= log(3))
fval = 4;
else
fval = 0;
end
end
How can I pass parameters A, B and C to the f function? I would like to write the if statement this way:
if (t <= A)
fval = exp(-Ct);
elseif (t > A) && (t <= B)
fval = 4;
0 comentarios
Respuesta aceptada
James Tursa
el 2 de Dic. de 2016
Editada: James Tursa
el 2 de Dic. de 2016
Not sure if this is what you want, but just pass them in:
dX = -13.925*A*B*C*x + f(t,A,B,C);
:
function fval = f(t,A,B,C)
:
etc
If you want to pass them in from upstream of the ode45 call, then:
A = log(2);
B = log(3);
C = 1;
DEABC = @(t,x) DE(t,x,A,B,C);
[T,X] = ode45(DEABC, tspan, x0);
:
function dX = DE(t,x,A,B,C)
dX = -13.925*A*B*C*x + f(t,A,B,C);
:
function fval = f(t,A,B,C)
:
etc
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!