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

Answers (1)

Stephan
Stephan on 4 Feb 2019
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 Comments

% Constants
beta=5;
alfa = 2.*beta/(beta+1);
tau1=4.4;
tau2=5;
Tc=1.24;
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)
hold on
Ee1 = EeA(tau1);
scatter(tau1,Ee1,'ro','lineWidth',2,'MarkerFaceColor','r')
% Part B
EeB = @(t) gamma.*exp(-((t-tau1)./Tc)) -alfa;
EeB1 = EeB(t2);
EeB2 = EeB(t22);
plot(t2, EeB1,'-b',t22, EeB2,'--c','lineWidth',2)
Ee2 = EeB(tau2);
scatter(tau2,Ee2,'ro','lineWidth',2,'MarkerFaceColor','r')
% Part C
EeC = @(t) Ee2.*exp(-((t3-tau2)./Tc));
EeC1 = EeC(t3);
plot(t3, EeC1,'-b','lineWidth',2)
hold off
I converted the above with the help of ode( your example) to below:
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);
[t_values2, E_values2]= ode15s(@EeB,time_range2,initial_E);
plot(t_values2,E_values2);
[t_values3, E_values3]= ode15s(@EeC,time_range3,initial_E);
plot(t_values3,E_values3);
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,~)
dEeCdt = EeB.*exp(-((t3-tau2)./Tc));
end
end
I get these errors:
solve_Efull
Not enough input arguments.
Error in solve_Efull/EeB (line 29)
dEeBdt = gamma.*exp(-((t-tau1)./Tc)) -alfa;
Error in solve_Efull/EeC (line 33)
dEeCdt = EeB.*exp(-((t3-tau2)./Tc));
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0,
options, varargin);
Error in solve_Efull (line 20)
[t_values3, E_values3]= ode15s(@EeC,time_range3,initial_E);
Stephan
Stephan on 4 Feb 2019
Edited: Stephan on 4 Feb 2019
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
Thanks for the input ; But I realised the output is wrong. If I compare the plot of my simple code and the plot via the ode solver. Because in the ode I need to put in the 'linear function' or anti derivative ; that shall give me the same output as the equations given in my normal code. But what we are doing is again differentiating those equations, hence we get something other than the goal. Do you know how to go about that ?
Stephan
Stephan on 5 Feb 2019
Edited: Stephan on 5 Feb 2019
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.
yes, precisely. Thankyou!! :)
So if I just have the solutions; and I need to use the ode solver to get the plot as an output ; is there a way I can find the differential equations to put in the ode solver? As I just have the solutions and the output I wish to generate.
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.

This question is closed.

Asked:

STP
on 4 Feb 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!