for loop ode 45 changing parameter values
이전 댓글 표시
I'm trying to run this code and use only the last value of the tspan for the ode45, however I always get 2 values for the final value of the tspan (when t=100)
This is the code:
ABP=linspace(40,170,131);
ti=0; tf=100;
%change in ABP at every loop
for i=1:1:length(ABP)
sol = ode45(@first,ti:1:tf, [0 0 0 0 0.205 62.516 12 13.201 0.2 1],[],ABP(i)); %(function, timespan, initial condition for xq,xc,xm1,xm,Ca,P1,V_sa,P2)
end
%%========= FUNCTION ==================
function dvdt = first(t,v,ABP)
%code
...ABP used in a set of differential eq...
if t==100
van= (Vsa+Vla)/(Vsab+Vla)
fileID=fopen('van.txt','a');
fprintf(fileID,' %4.3f\n',van);
fclose(fileID);
end
end
Hence, I should end up having a .txt file with 131 values of van, one for each value of ABP. Instead, I end up with double the amount. I also printed the ABP values in the txt file to check, and indeed there were two different (very similar) values of van for each value of ABP. Any suggestions on how to fix this?
채택된 답변
추가 답변 (1개)
Jan
2017년 12월 14일
Fo not include an output inside the function to be integrated. Remember that the integrator can call this function with different inputs to evaluate one step. The integrator can even reject steps.
Use the OutputFcn instead, which is called after a successful step:
options = odeset('OutputFcn', @yourOutput)
This has been suggested in your other thread https://www.mathworks.com/matlabcentral/answers/371408-selecting-last-value-of-tspan-ode45#answer_294967 already.
For completeness I repeat, that providing the parameters as input of ODE45 is deprecated for over 17 years now, see http://www.mathworks.com/matlabcentral/answers/1971> .
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!