How can I simulate data when I only know the derivative?

조회 수: 6 (최근 30일)
Anna
Anna 2019년 10월 25일
댓글: Jim Riggs 2019년 10월 26일
For example, I have the equation dx/dt = 3x/(2b-a)+4c where x represents a population of something at time t.
I know a, b, and c and I can use any value I want for x. Is there a way to somehow make a x vs. t plot out of this? If not, what can of graph can I make?

답변 (1개)

Jim Riggs
Jim Riggs 2019년 10월 25일
편집: Jim Riggs 2019년 10월 25일
This is exactly how time-based simulations are done. You have the mathematical model of the system state derivative(s) (i.e. dx/dt, where x is the system state or state vector) and you treat it as an initial value problem. You specify the inital conditions (all the state parameters at time=0) then numerically integrate dx/dt over the desired period of time to obtain x(t).
In your case, you are given a, b, and c - this defines dx/dt. Now you pick a starting value for x (typically at t=0, call it x0) and integrate dx/dt from t=0 to some desired period of time.
  댓글 수: 3
Anna
Anna 2019년 10월 25일
편집: Anna 2019년 10월 25일
If I'm saving my equation in a script where
function dx = function_1(a,b,c,x)
3*x/(2*b-a)+4*c;
Then what would the syntax look like for the ODE45 function since the variable t is not present in equation dx/dt? Would it be something similar to
tspan = [0 100];
x0 = 0;
[t,x] = ode45(@(t,x) function_1, tspan, x0);
Should I be using odefun?
Jim Riggs
Jim Riggs 2019년 10월 26일
First, you create an "anonymous" function for your derivative:
a = .. ; % assign numeric values to a, b, and c.
b = .. ;
c = .. ;
dxdt = @(t,x) 3*x/(2*b-a) +4*c;
You have to asign numeric values for a, b, and c. Then when you define the anonymous function (i.e. when the fourth line, above, is executed), these values for a, b, and c become embedded in the function. This means that if you want to change any of these three values, you have to re-define the function also.
Note that this function is defined as a function of x and time (t) , even though t does not show up in the equation. This is because the ODE solver wants the function to be in this format.
Now set up the ODE solver and integrate the function. I'll use ode45 because it is very popular:
t0 = .. ; % define the start time, typically zero
tf = .. ; % define the final time
tspan = [t0, tf]; % this is the time span for integration
x0 = .. ; % this is the initial value of the function at t0.
[t,x] = ode45(dxdt, tspan, x0); % call ode45 solver to integrate the function
After this is run, the ODE45 solver returns a time vector, t, and a solution vector x. It does this by nmerically integrating the function dxdt using the parameters that you have specified (the time span and the initial value for x)
If you want to, you can also evaluate the derivative function too using:
dx = dxdt(t,x);
This uses the time and x vector solution from the ode45 solver, and creates the derivative that is associated with the t,x solution.
Now you can plot x vs, time (and also the derivative, dx vs time);
figure;
plot(t,x,'r');
hold on;
plot(t,dx,'b');
grid on;
legend('x', 'dx');

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

카테고리

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