How to find trajectory of an internal variable while solving a diff equation using an ODE solver? (pls read details)

2 次查看(过去 30 天)
For example I have a differential equation where the forcing function is a function of the states.
x is a variable, p, xd,K1 are constants, t is time
tau= (xd-x)*K1;
dx/dt + px = tau
To find the trajectory of this I can wrap this up in a function and call an ode solver and it would return me an array of X and T .
function xdot=trajectory(t,X)
tau= K1(xd-x);
xdot=tau-p*x;
end
Now to call an ode solver.
[T,X]= ode45(@trajectory,[0 10],[0]) ;
What if I want to find to plot tau and T ?
One method is I can find tau using the values of X and T returned. Is there any simpler way where in I can get tau also as the output( as an array) along with X and T .

回答(1 个)

Star Strider
Star Strider 2015-5-24
Your function should be:
function xdot=trajectory(t,x,K1,xd,p)
tau = K1.*(xd-x);
xdot = tau-p.*x;
end
and your ode45 call becomes:
[T,X]= ode45(@(t,x)trajectory(t,x,K1,xd,p),[0 10],[0]) ;
assuming that ‘K1’, ‘xd’, and ‘p’ are scalars and exist in your workspace.
After you have solved your ODE, you have ‘X’, ‘T’, ‘xd’, ‘K1’, and ‘p’ in your workspace, so calculating ‘tau’ is simply:
tau = K1.*(xd-X);
that you can do outside your ODE function.

类别

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