How to get derivative as result of Matlab ode45?

1k Views Asked by At

I am using Matlab ode45 to solve system of first order differential equation presented with:

function f=fun(z,p)

R=1; sig=1; beta=1;

f=zeros(4,1);
f(1)=-32*1*beta/(R.^4*p(1));
f(2)=(-(2-sig)*8*f(1)/(sig.*R)-f(1)*p(2))/p(1);
f(3)=(-p(2)*f(2)+(2-sig)*(-8*f(2)/R-8*f(1)/(R.*R*p(1)))/sig-f(1)*p(3))/p(1);
f(4)=(-f(2)*p(3)-f(3)*p(2)+(2-sig)*8*(-f(3)/R-(f(2)/p(1)-p(2)*f(1))/(p(1).*p(1)*R.*R))/sig -f(1)*p(4))/p(1);

where f(1), f(2), f(3), f(4) are first derivatives of variables p(1), p(2), p(3), p(4). When I call this function with:

[zv,pv]=ode45(@fun,[1 0],[1; 0; 0; 0]);

As result I got variables p(1), p(2), p(3), p(4), I guess that is some way of working of ode45.

But I also need values of first derivatives f(1), f(2), f(3), f(4). I tried to calculate them with Matlab diff function, but I got imprecise results. Is there some way to get values of derivatives f(1), f(2), f(3), f(4) directly from ode45 function?