How to pass parameters?

2 visualizzazioni (ultimi 30 giorni)
Marco Sammito
Marco Sammito il 2 Dic 2016
Modificato: James Tursa il 2 Dic 2016
Hi. I have to solve this differential equation
where
Here is Mischa Kim's code:
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;

Risposta accettata

James Tursa
James Tursa il 2 Dic 2016
Modificato: James Tursa il 2 Dic 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

Più risposte (0)

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by