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

4 visualizaciones (últimos 30 días)
Anna
Anna el 25 de Oct. de 2019
Comentada: Jim Riggs el 26 de Oct. de 2019
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?

Respuestas (1)

Jim Riggs
Jim Riggs el 25 de Oct. de 2019
Editada: Jim Riggs el 25 de Oct. de 2019
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 comentarios
Anna
Anna el 25 de Oct. de 2019
Editada: Anna el 25 de Oct. de 2019
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 el 26 de Oct. de 2019
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');

Iniciar sesión para comentar.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by