ODE45 with a parameter which changes in every time step
조회 수: 5 (최근 30일)
이전 댓글 표시
I am trying to solve a first order differential equation of form dY/dTp = -K*Y for K it needs to be numerical integrated a separate function which depends on Tp.
The script file is
R,T1 ,T2,k0,ME,MS,m,E0,sigma,E1,E2,Y0 are input parameters
TP=T1:T2;
Tp = TP';
[r,n] = size(Tp);
K = zeros(r,n);
fun = (@(E,Tp) ((1/(sigma*(sqrt(2*pi)))).*exp((((-(k0*R*Tp*Tp))./(m.*E)).*exp(-E./(R*Tp)))-(((E-E0).^2)./(2*sigma*sigma)))));
for i=1:(numel(Tp)-1)
gun = (@(E) fun(E,Tp(i)));
K(i) = integral(gun,E1,E2);
Tp_span=[Tp(i),Tp(i+1)];
[Tp(i),Y(i)]=ode45(diff,Tp_span,Y0);
end
and the function file for changing K value at every Tp is
function dYdTp = diff(Tp,Y,K)
dYdTp = zeros(1,1);
dYdTp = -K*Y;
and the error prompt occurring in third line of function file is Not enough input arguments and in script file error is occurring at ode solver line.
댓글 수: 0
채택된 답변
Torsten
2015년 7월 9일
1. E1, E2 and Y0 are undefined.
2. Your call to ODE45 must look somehow like
[Tp,Y]=ode45(@(Tp,Y)diff(Tp,Y,K(i)),Tp_span,Y0);
Best wishes
Torsten.
댓글 수: 0
추가 답변 (1개)
Steven Lord
2015년 7월 8일
Your code attempts to call your diff function (by the way, I recommend changing the name so as to avoid conflict with the DIFF function included in MATLAB) with 0 inputs and use the output argument from that call as the first input to the ODE solver. That's not going to work. Instead, call your function using the techniques shown in the documentation (which will also allow you to pass the additional parameter K into your ODE function.) Those examples use FZERO, but the same techniques will work with the ODE solvers as well.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!