How to pass parameters?

2 views (last 30 days)
Marco Sammito
Marco Sammito on 2 Dec 2016
Edited: James Tursa on 2 Dec 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;

Accepted Answer

James Tursa
James Tursa on 2 Dec 2016
Edited: James Tursa on 2 Dec 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

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!