ODE45 with a parameter which changes in every time step

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Torsten
Torsten il 9 Lug 2015
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.

Più risposte (1)

Steven Lord
Steven Lord il 8 Lug 2015
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.

Community Treasure Hunt

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

Start Hunting!

Translated by