ODE45 with a parameter which changes in every time step

12 次查看(过去 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.

采纳的回答

Torsten
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.

更多回答(1 个)

Steven Lord
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.

类别

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