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);
採用された回答
その他の回答 (1 件)
Steven Lord
2017 年 3 月 17 日
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.
カテゴリ
ヘルプ センター および File Exchange で Numeric Solvers についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!