Extract values from ODE45

조회 수: 15 (최근 30일)
Ali zain
Ali zain 2019년 3월 23일
편집: Torsten 2025년 1월 15일
function [] = Example2()
clc
clear
close all
y0 = [100.; 0; 0]; % Initial values for the dependent variables
tspan= linspace(0,20,50);
options = optimset('display','off');
[t,x]= ode45(@ODEfun,tspan,y0,options);
plot(t,x)
legend('Fa','Fb','Fc')
end
function dYdv = ODEfun(v,s);
Fa = s(1);
Fb = s(2);
Fc = s(3);
Kc = 0.01;
Ft = Fa + Fb + Fc;
Co = 1;
K = 10;
Kb = 40;
ra = 0 - (K * Co / Ft * (Fa - (Co ^ 2 * Fb * Fc ^ 2 / (Kc * Ft ^ 2))));
Ka = 1;
Ra = Ka * Co * Fa / Ft;
Rb = Kb * Co * Fb / Ft;
dFadv = ra - Ra;
dFbdv = 0 - ra - Rb;
dFcdv = -2 * ra;
dYdv = [dFadv; dFbdv; dFcdv];
end
How Do I extract the values of Ra and Rb and plot them ?

채택된 답변

Star Strider
Star Strider 2019년 3월 24일
How Do I extract the values of Ra and Rb and plot them ?
You don’t ‘extract’ them. Just recalculate them from the integrated values.
Make a few changes to your ‘Example2’ function:
function [] = Example2()
% clc
% clear
% close all
y0 = [100.; 0; 0]; % Initial values for the dependent variables
tspan= linspace(0,20,50);
options = optimset('display','off');
[t,x]= ode45(@ODEfun,tspan,y0,options);
figure
plot(t,x)
legend('Fa','Fb','Fc')
Fa = x(:,1);
Fb = x(:,2);
Fc = x(:,3);
Kc = 0.01;
Ft = Fa + Fb + Fc;
Co = 1;
K = 10;
Kb = 40;
ra = 0 - (K .* Co / Ft .* (Fa - (Co .^ 2 * Fb .* Fc .^ 2 ./ (Kc * Ft .^ 2))));
Ka = 1;
Ra = Ka * Co * Fa / Ft;
Rb = Kb * Co * Fb / Ft;
figure
plot(t, [Ra Rb])
legend('R_a', 'R_b')
end
This will produce the plot you want. You could plot them in the same figure as the first plot (using the hold function), however you will likely not be able to see them well. I would use separate plots.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2019년 3월 24일
For example, make ODEfun a nested function that writes each Ra and Rb at the end of a shared variable.
Warning: time does not always advance linearly for ode45. ode45 itself promises that time will not go in reverse (which is not true for all of the ode*() routines), but if there is more than one variable then ode45 will sometimes evaluate at the same time and different boundary conditions.
Warning: the times (v) that ode45 evaluates at are not the same times that ode45 will report at in the t vector that is the output. You are using a vector of more than two elemetns for tspan, so ode45 will report for exactly those times, but it will predict the values at those times based upon the evaluations it does at whatever times it deems necessary to maintain the integration tolerances. ode45 will almost always evaluate at a significant number of more times than it reports for.
It is common to discover that you do not want to plot the intermediate values at "whatever times the ode function happened to be evaluated at": it is common to realize that you want the intermediate values at the same times that ode45 reports in the output t vector. The way to handle that situation is to code something like
function [dYdv, Ra, Rb] = ODEfun(v, s);
...
end
and then after you have received the solution from ode45, run through each of the rows
nrow = size(t,1);
Ra = zeros(nrow,1);
Rb = zeros(nrow,1);
for row = 1 : nrow
[~, Ra(row), Rb(row)] = ODEfun(t(row), s(row,:));
end

Natasha
Natasha 2025년 1월 15일
hie, i want to get value of probability at a given time , how can i get using ode45
  댓글 수: 1
Torsten
Torsten 2025년 1월 15일
편집: Torsten 2025년 1월 15일
Use "deval" with the solution you get from "ode45" (assuming that "probability" is a solution variable):

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by