Accuracy of the first derivative with ode45

I am using ode45 to get solution of system with 4 differential equation of the first order. ode45 gives me result as value, but I need the first derivative of that value. So, I found two methods to do that.
First one, is the gradient command. The second one is sol and deval commands together. My question is, which of these two methods will give me more accurate result? And which of these two methods will give me accuracy of result same as accuracy of ode45?

Risposte (1)

Torsten
Torsten il 3 Lug 2018
The most accurate method is to use the derivatives you set by yourself in the function routine that ODE45 calls (the dydt(i) values).
Best wishes
Torsten.

5 Commenti

I G
I G il 3 Lug 2018
Modificato: I G il 3 Lug 2018
Do you mean derivative from my function file? I tried to call it but I don`t know how? Because ode45 gives me results as value, not first derivative, just normal value.
James Tursa
James Tursa il 3 Lug 2018
Modificato: James Tursa il 3 Lug 2018
Please post the code you are using. Basically, you simply pass the result that ode45 is giving you back into your derivative function that you wrote. If this function is not vectorized then you will have to call it in a loop.
I G
I G il 3 Lug 2018
Modificato: I G il 3 Lug 2018
This is my function file:
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);
In order to solve it with ode45 I am using
[zv,pv]=ode45(@fun,[1 0],[1; 0; 0; 0]);
I have tried to call function fun and in that way get first derivatives with command:
dp=fun(zv,pv)
but I got only four values, I need four series of values, for four derivatives. How to call function
fun
to get that?
Your derivative function is not vectorized, so you need to call it in a loop. E.g.,
dp = zeros(size(pv));
for k=1:size(pv,1)
dp(k,:) = fun(zv(k),pv(k,:));
end
This works. Thanks a lot!

Accedi per commentare.

Prodotti

Richiesto:

I G
il 3 Lug 2018

Commentato:

I G
il 4 Lug 2018

Community Treasure Hunt

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

Start Hunting!

Translated by