ODE function - plot not as desired

5 views (last 30 days)
STP
STP on 5 Feb 2019
Commented: STP on 7 Feb 2019
The above equations give me the plot shown And I am attaching the code for the same;
% 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
xlabel('t');
ylabel('E_e');
xticklabels({'0', '4.4', '5'})
xticks([ 0 4.4 5])
xticklabels({'0', '4.4', '5'})
xticklabels({'0', 't1', 't2'})
Now if I wish to use 'ode function' ; how to achieve the same output i.e. plot ? Do I need to use 'anti derivative' of these equations ? Objective is to get the same plot; maybe im understanding ode wrong - and the answer is simple . Any examples or reference would be appreicated :) thanks :)
  2 Comments
Cris LaPierre
Cris LaPierre on 6 Feb 2019
I notice this code is actually from another answer. What exactly are we helping you solve?
STP
STP on 7 Feb 2019
Hi, yes! :)
So I am new to matlab as you would have guessed. I have basically kept the same set of eqns to ask questions related to replicating certain plots but different ways; simple plot, odes, integral, diff, surf, mesh etc in order to understand how they work in a certain case and then use it in my zillion equations I work with as per my need. Thanks! This forum is great- keep up the great work! :)

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 5 Feb 2019
How are you trying to use odefunction? Or why?
it is for converting symbolic expressions to function handles for ODE solvers. To be used in an ODE solver, your equations need to be differential equations. Yours are not.
  2 Comments
STP
STP on 6 Feb 2019
Yeah. I realised it. Mine are the solutions fo the diff eqns, and I need to put in the diff eqns in the ode solver; Hence; I wanted to know how to get the eqns from the solutions and then put it in the ode solver as to achieve the same plot
Cris LaPierre
Cris LaPierre on 6 Feb 2019
I assume you have your reasons for going backwards...
Yes, you need to differentiate the equations you have, solve them with an ode solver, and then replot. I'm not worrying about recreating the plot exactly. I've got to leave something for you to do.
% Constants
beta=5;
alfa = 2.*beta/(beta+1);
tau1=4.4;
tau2=5;
Tc=1.24;
gamma=alfa.*(2-exp(-tau1));
Ee1 = -alfa.*exp(-tau1./Tc) +alfa;
Ee2 = gamma.*exp(-(tau2-tau1)./Tc) -alfa;
% Create difference equations
syms EeA(t) EeB(t) EeC(t)
eqA = diff(EeA(t),t) == diff(-alfa .*exp(-t./Tc) + alfa)
eqB = diff(EeB(t),t) == diff(gamma.*exp(-((t-tau1)./Tc)) -alfa)
eqC = diff(EeC(t),t) == diff(Ee2.*exp(-((t-tau2)./Tc)))
% Convert to fxn handles
odefunA = odeFunction(rhs(eqA),EeA(t))
odefunB = odeFunction(rhs(eqB),EeB(t))
odefunC = odeFunction(rhs(eqC),EeC(t))
% Solve ODE
[tA,yA] = ode45(odefunA,[0 tau1],0);
[tB,yB] = ode45(odefunB,[tau1 tau2],Ee1);
[tC,yC] = ode45(odefunC,[tau2 8],Ee2);
figure
plot(tA,yA,tB,yB,tC,yC)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!