How to pass parameters?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!