Change input at each time step of the ODE solver 'ode45'

조회 수: 25 (최근 30일)
KC
KC 2015년 12월 5일
댓글: Jan 2017년 5월 5일
I am not sure how to change an input parameter 'β' at each time step. My code is below - which gives me an error. Can anybody help please!
t = [7 14 21 28 35 42 49 56 63 70 77 84];
for i=1:12;
beta(i) = 0.43e-08 + (4.28e-08 - 0.43e-08)*exp(-0.20*t(i));
end
f = @(t,x) [3494-0.054*x(1)-beta*x(1)*x(3); beta*x(1)*x(3) - 0.41*x(2); ...
50000*x(2) - 23*x(3)];
[t,xa1] = ode45(f,t,[64700 0 0.0033],beta);
  댓글 수: 1
Jan
Jan 2015년 12월 5일
And the error message is:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in @(t,x)[3494-0.054*x(1)-beta*x(1)*x(3);beta*x(1)*x(3)-0.41*x(2);50000*x(2)-23*x(3)]

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2015년 12월 6일
Please consider, that Matlab's ODE integrators cannot handle non-smooth functions sufficiently. See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047 .
The only reliable method to run the integration is a loop over the time intervals:
function yourIntegration
tResult = [];
xResult = [];
tStep = [7 14 21 28 35 42 49 56 63 70 77 84];
x0 = [64700 0 0.0033];
for index = 2:numel(tStep)
% Integrate:
beta = 0.43e-08 + (4.28e-08 - 0.43e-08) * exp(-0.20*t(index - 1))
af = @(t,x) f(t, x, beta);
t = tStep(index-1:index);
[t, x] = ode45(af, t, x0);
% Collect the results:
tResult = cat(1, tResult, t);
xResult = cat(1, xResult, x);
% Final value of x is initial value for next step:
x0 = x(end, :);
end
function dx = f(t,x, beta)
dx = [3494-0.054*x(1)-beta*x(1)*x(3); ...
beta*x(1)*x(3) - 0.41*x(2); ...
50000*x(2) - 23*x(3)];
  댓글 수: 7
Saiprasad Gore
Saiprasad Gore 2017년 5월 5일
Thanks a lot, I had a similar problem. I wanted to switch the eqn depending on condition after every step. I hope this will work in my case too. Can you tell me how to give ode45 just 1 step without intermediate adaptive steps?
Jan
Jan 2017년 5월 5일
@Saiprasad Gore: This is not possible with ode45.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 12월 6일
f = @(T,x) [3494-0.054*x(1)-interp1(t,beta,T,'linear','extrap')*x(1)*x(3); interp1(t,beta,T,'linear','extrap')*x(1)*x(3) - 0.41*x(2); ...
50000*x(2) - 23*x(3)];
  댓글 수: 2
KC
KC 2015년 12월 12일
Thanks Walter!
sam
sam 2016년 6월 15일
편집: sam 2016년 6월 16일
@Walter Roberson
Hi Walter,
Why do we have to do interpolation if we already know the exact expression of the variables? Couldnt we just input the exact expression of the variables into the Matlab ode45 solver? If we could, could you kindly tell me how to do this? Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by