How to plot the calculation made in ODE function solver ?

Hi All, I have multiple ode to be solved along with some calculation (D and E) as shown in MYCODE_FUN. I am calling this function with ode solver and it works also.
I am able to use fprintf commanad for D and E. How should i plot these calculated values for D and E with respect to time t?
function xdot=MYCODE_FUN(t,x)
K1= 50.7; Ktr = 6189; Kr = 8.89*((10)^8); Kp = 30000; Fe= 0.0000006; Minitial=0.01; Winitial =0.01; dVdT = 0.0428;
I = x(1); A = x(2,1); M = x(3,1);
V=( (53*Minitial)/(1000)) + ( (9.54*t)/(1000)) + ( (18*Winitial)/(1000)) +((30.72*t)/(1000));
xdot(1,1) = (0.0002/V) - K1*Fe*I - ((I*dVdT)/V);
xdot(2,1) = (0.0003/V) - K1*Fe*I - Ktr*A - ((A*dVdT)/V);
xdot(3,1) = (0.18/V) - Kp*M - ((M*dVdT)/V);
D = V* xdot(1,1) + V* xdot(2,1); E = K1*Fe*I + Ktr*A + ((M*dVdT)/V);
%fprintf('%6.2f %15f %20f %35f\n', t, I, A, M); end
Here is the ode solver code for MYCODE_FUN
clear clf
x_ss = [0;0;0];
[t,x] = ode45('MYCODE_FUN',[0:0.01: 0.05],x_ss);
I = x(:,1); A = x(:,2); M = x(:,3);
figure(1); plot(t,I);
figure(2) plot(t,A);
figure(3) plot(t,M);

 Akzeptierte Antwort

Torsten
Torsten am 17 Mär. 2017
Bearbeitet: Torsten am 17 Mär. 2017
function xdot=MYCODE_FUN(t,x,K1,Ktr,Kr,Kp,Fe,Minitial,Winitial,dVdT)
I = x(1); A = x(2,1); M = x(3,1);
V=( (53*Minitial)/(1000)) + ( (9.54*t)/(1000)) + ( (18*Winitial)/(1000)) +((30.72*t)/(1000));
xdot(1,1) = (0.0002/V) - K1*Fe*I - ((I*dVdT)/V);
xdot(2,1) = (0.0003/V) - K1*Fe*I - Ktr*A - ((A*dVdT)/V);
xdot(3,1) = (0.18/V) - Kp*M - ((M*dVdT)/V);
%fprintf('%6.2f %15f %20f %35f\n', t, I, A, M); end
clear clf
K1= 50.7; Ktr = 6189; Kr = 8.89*((10)^8); Kp = 30000; Fe= 0.0000006; Minitial=0.01; Winitial =0.01; dVdT = 0.0428;
x_ss = [0;0;0];
[t,x] = ode45(@(t,x)MYCODE_FUN(t,x,K1,Ktr,Kr,Kp,Fe,Minitial,Winitial,dVdT),[0:0.01: 0.05],x_ss);
for i=1:numel(t)
I = x(i,1); A = x(i,2); M = x(i,3);
V=( (53*Minitial)/(1000)) + ( (9.54*t(i))/(1000)) + ( (18*Winitial)/(1000)) +((30.72*t(i))/(1000));
D(i,1)=V*((0.0002/V) - K1*Fe*I - ((I*dVdT)/V))+V*((0.0003/V) - K1*Fe*I - Ktr*A - ((A*dVdT)/V));
E(i,1)=K1*Fe*I + Ktr*A + ((M*dVdT)/V);
end
I = x(:,1); A = x(:,2); M = x(:,3);
figure(1) plot(t,I);
figure(2) plot(t,A);
figure(3) plot(t,M);
figure(4) plot(t,D);
figure(5) plot(t,E);
Best wishes
Torsten.

Weitere Antworten (1)

Steven Lord
Steven Lord am 17 Mär. 2017
Torsten's approach duplicates some of the code (specifically the line that defines V) from your ODE function. I would instead reuse the whole ODE function. Here's the main function:
function solveMyOdes
x_ss = [0;0;0];
[t,x] = ode45(@MYCODE_FUN,[0:0.01: 0.05],x_ss);
numTS = numel(t); % number of time steps
D = zeros(numTS, 1);
E = zeros(numTS, 1);
for k = 1:numTS
[~, D(k), E(k)] = MYCODE_FUN(t(k), x(k, :).');
end
plot(t, D);
hold on
plot(t, E);
Here is the ODE function used by ode45 and directly inside solveMyOdes. It is the same as the code you wrote, just formatted a little differently (in particular, defining one variable per line) in a way that I find easier to read and with two extra outputs in its function declaration line.
function [xdot, D, E] = MYCODE_FUN(t,x)
K1= 50.7;
Ktr = 6189;
Kr = 8.89*((10)^8);
Kp = 30000;
Fe= 0.0000006;
Minitial=0.01;
Winitial =0.01;
dVdT = 0.0428;
I = x(1);
A = x(2,1);
M = x(3,1);
V=( (53*Minitial)/(1000)) + ( (9.54*t)/(1000)) + ( (18*Winitial)/(1000)) +((30.72*t)/(1000));
xdot(1,1) = (0.0002/V) - K1*Fe*I - ((I*dVdT)/V);
xdot(2,1) = (0.0003/V) - K1*Fe*I - Ktr*A - ((A*dVdT)/V);
xdot(3,1) = (0.18/V) - Kp*M - ((M*dVdT)/V);
D = V* xdot(1,1) + V* xdot(2,1);
E = K1*Fe*I + Ktr*A + ((M*dVdT)/V);
Just because ode45 will only call your function with one output argument doesn't mean you can't write it to return multiple outputs so you can call it later.

1 Kommentar

HI Steven Lord,
it shows error as ??? Error: File: solveMyOdes.m Line: 9 Column: 5 Expression or statement is incorrect--possibly unbalanced (, {, or [.
Line no 9 is [~, D(k), E(k)] = MYCODE_FUN(t(k), x(k, :).');
Could you please help me in that.
Tarun

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by