How to pass parameters?

조회 수: 2 (최근 30일)
Marco Sammito
Marco Sammito 2016년 12월 2일
편집: James Tursa 2016년 12월 2일
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;

채택된 답변

James Tursa
James Tursa 2016년 12월 2일
편집: James Tursa 2016년 12월 2일
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개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by