How can I obtain the derivative of a vector?

Hi, I have solved a system of ODEs with the solver ODE45 , obtaining the vector T and Y, which is a matrix of Nx6; N time points (length of vector of time points T) and 6 vectors, x1,x2,x3,dx1,dx2,dx3, displacements and velocities of three masses. I would like to obtain the vector of accelerations of x1,x2,x3. Could somebody please help me? Thank you very much!

Answers (3)

Plug your ‘t’ vector and ‘y’ values matrix back into your ODE function. It will give you all the derivatives because that’s what you wrote it to provide.

4 Comments

@Star. I think one can only get the velocity values that way, not accelerations.
@Roger — I’ve actually done that in the past to get derivatives of all the solved values the ODE solver produces. We don’t have the ODE function, but in my experience, it is coded to produce a vector of derivatives for all the first-order equations that the solver then integrates. Plugging the solved values back into the ODE function produces derivatives of all the solved variables.
The gradient function will work as you describe if the time difference is constant, and is then a convenient approximation. The only problem might be that there is no constant spacing of the solved values. (If ‘tspan’ is a two-element range vector and not a vector of discrete integration times, gradient may not be accurate.) The ‘exact’ derivative calculations in this instance can be obtained by doing as I described.
Hi Roger,
thank you for your answer. How can I plug the vectors into my function to obtain the answer? Sorry, but I´m new in Matlab...
I don’t know if you’re addressing me or Roger. Assuming me, because you’re Commenting on my Answer, you just plug the integrated values of ‘t’ and ‘y’ (returned by ode45) into your ODE function. So if ‘odefun’ is the name of your ODE function and it is written as:
function dy = odefun(t,y)
to get the derivative matrix, simply call it as:
dy = odefun(t,y);
where ‘t’ is the vector of times that ode45 returns, and ‘y’ is the matrix of solutions ode45 returns. The ‘dy’ matrix is the matrix of derivatives you want, at times corresponding to ‘t’.

Sign in to comment.

You can get an approximation to the derivative of dx1, dx2, and dx3 using matlab's 'gradient' function. If you would prefer a higher order approximation, you can find the code I wrote for a parabolic approximations at:
http://www.mathworks.com/matlabcentral/answers/175172
I guess you defined the ODEs as acceleration = f(displacement,velocity). So just repeat the calculations of acceleration (y'') for the solution variables you got from ODE45.
Best wishes
Torsten.

7 Comments

Yes, I did, Y has the velocities and displacements of 3dgof
[T,Y] = ode45(@(t, y) Function(t, y, fext(t), param),Tspan, y0,options);
where, yDot = Function(t, y, fext, param)
student
student on 18 Feb 2015
Edited: student on 18 Feb 2015
do you mean for example by writting:
[T1,Y1]=ode45(@(T, Y) Function(T, Y, fext(t), param),Tspan, y0,options)
? will T1 be T and Y1 the vector with the accelerations and ist derivatives? I guess I have to change somehting else, because it doesn´t work..
So you know now how to proceed ? Just call "Function" with the solution you obtained from ODE45 to get yDot.
Best wishes
Torsten.
Do you mean for example writting
my_vector= Function(T, Y, fext(t), param)?
Something like
for i=1:length(tspan)
t=tspan(i);
y(1)=Y(i,1);
y(2)=Y(i,2);
y(3)=Y(i,3);
y(4)=Y(i,4);
y(5)=Y(i,5);
y(6)=Y(i,6);
fext=...;
yDot=Function(t,y,fext,param);
YDOT(:,i)=yDot(:);
end
Best wishes
Torsten.
Because function already has a special meaning in MATLAB, you should avoid naming your function Function. Use a more descriptive name instead.

Sign in to comment.

Asked:

on 17 Feb 2015

Commented:

on 18 Feb 2015

Community Treasure Hunt

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

Start Hunting!