convert my code ; Plot using ode function
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
% Constants
beta=5;
alfa = 2.*beta/(beta+1);
tau1=4.4;
tau2=5;
Tc=0.20;
gamma=alfa.*(2-exp(-tau1));
% Time frames
t1=0:0.01:tau1;
t11 = tau1:0.01:8;
t2 = tau1:0.01:tau2;
t22 = tau2:0.01:8;
t3 = tau2:0.01:8;
% Part A
EeA = @(t) -alfa .*exp(-t./Tc) + alfa;
EeA1 = EeA(t1);
EeA2 = EeA(t11);
plot(t1,EeA1,'-b',t11,EeA2,'--c','lineWidth',2)
Now, I wish to do it with a ode function; I tried the below but unsuccessful ; I get error- Error in solve_E (line 13)
function dEdt = simple_ode(t,E)
dEdt = @(t) -alfa .*exp(-t./Tc) + alfa;
end
function solve_E
initial_E = 0;
time_range = [0, 4.4];
%% Constants
beta=5;
alfa = 2.*beta/(beta+1);
tau1=4.4;
tau2=5;
Tc=0.20;
gamma=alfa.*(2-exp(-tau1));
[t_values, E_values]= ode15s(@(t,E) simple_ode(t,E),time_range,initial_E);
plot(t_values,E_values);
end
댓글 수: 0
답변 (1개)
Stephan
2019년 2월 4일
0 개 추천
Hi,
try:
solve_E
function solve_E
initial_E = 0;
time_range = [0, 4.4];
% Constants
beta=5;
alfa = 2.*beta/(beta+1);
tau1=4.4;
tau2=5;
Tc=0.20;
gamma=alfa.*(2-exp(-tau1));
[t_values, E_values]= ode15s(@simple_ode,time_range,initial_E);
plot(t_values,E_values);
function dEdt = simple_ode(t,~)
dEdt = -alfa .*exp(-t./Tc) + alfa;
end
end
gamma and tau2 are not needed in this code, they are unused.
Best regards
Stephan
댓글 수: 6
STP
2019년 2월 4일
See my comments:
solve_Efull
function solve_Efull
initial_E = 0;
time_range1 = [0, 4.4];
time_range2 = [4.4 5];
time_range3 = [5 10];
% Constants
beta=5;
alfa = 2.*beta/(beta+1);
tau1=4.4;
tau2=5;
Tc=0.20;
gamma=alfa.*(2-exp(-tau1));
[t_values1, E_values1]= ode15s(@EeA,time_range1,initial_E);
plot(t_values1,E_values1);
% Plot all lines in the same figure
hold on
% Initial value is last value of E_values1
[t_values2, E_values2]= ode15s(@EeB,time_range2,E_values1(end));
plot(t_values2,E_values2);
% Initial value is last value of E_values2
[t_values3, E_values3]= ode15s(@EeC,time_range3,E_values2(end));
plot(t_values3,E_values3);
hold off
function dEeAdt = EeA(t,~)
dEeAdt = -alfa .*exp(-t./Tc) + alfa;
end
function dEeBdt = EeB(t,~)
dEeBdt = gamma.*exp(-((t-tau1)./Tc)) -alfa;
end
function dEedt = EeC(t,~)
% Is the factor correct? EeB(tau2) --> ???
% I can not know - check this...
dEedt = E_values2(end).*exp(-((t-tau2)./Tc));
end
end
STP
2019년 2월 5일
Are the equations used in the non-ode version of your code already the solutions of the differential equations you want to plot? If you use the same solutions inside an ode-function, Matlab numerically integrates the functions. This is what happens here i think. You get the integral of your functions. If you want the functions themselves as result, you should put the differential equations to the ode-solver not the solutions of the differential equations.
STP
2019년 2월 5일
Stephan
2019년 2월 5일
Calculate the analytic derivates of your functions. If you have access to Symbolic Toolbox, you can use this to calculate the derivatives and then you can create a function that is suitable for ode solvers.
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!