Second time derivative of ODE45 solution looks bumpy

3 ビュー (過去 30 日間)
Paul Miller
Paul Miller 2023 年 5 月 30 日
コメント済み: Torsten 2023 年 5 月 31 日
I'm trying to solve a second order differential equation in matlab using the ODE45 solver. I'm quite new to this and would like to plot the solution, as well as the first and second derivatives of the solution. I'm sure there is a better way to achieve this anyway, but I'm taking the numerical derivative of the ODE45 soution twice and plotting it. This worked for a couple differential equations, but for this specific differential equation produces odd results.
Here is my code:
dt = .001;
initz=[0; 0];
[t,z]=ode45(@f3, t, initz);
x1 = gradient(z(:,2))./dt;
x2 = gradient(x1)./dt;
figure (3)
plot(t, z(:,2), t, x1, t, x2)
Here is my function:
function dz=f3(t,z)
% z(1) = x1 = dx/dt; z(2)= x
dz=[-100*z(1)+cos(5*t-pi/3); z(1)];
Here is the function I'm trying to solve:
And here is the output plot showing the issue I'm having.
If theres a better way of doing this, I'm all ears. But I would also like to know what's going wrong here if anyone can figure it out.
Thank you!


Torsten 2023 年 5 月 30 日
編集済み: Torsten 2023 年 5 月 30 日
t=[0 5];
initz=[0; 0];
[t,z]=ode15s(@f3, t, initz, odeset('RelTol',1e-12,'AbsTol',1e-12));
for i=1:numel(t)
dz = f3(t(i),z(i,:));
x2(i) = dz(1,1);
plot(t, z(:,2), t, z(:,1), t, x2)
ylim([-0.05 0.05])
Here is my function:
function dz=f3(t,z)
% z(1) = x1 = dx/dt; z(2)= x
dz=[-100*z(1)+cos(5*t-pi/3); z(1)];
  4 件のコメント
Torsten 2023 年 5 月 31 日
and since it worked the first two times using gradient I was not sure why it didn't work for this equation.
As said, it also works for this equation, but the results are not as exact as the direct derivatives from the ODE solution.


その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 5 月 30 日
Reemmber that the gradient function usues an APPROXIMATION. And, that at the ends of the series, that approximation is less good than it is in the middle part.
Worse, differentiation is a noise amplifying process. Tiny errors in the data will be amplified. Then taking the second derivative, and you get amplification squared.
Yes, gradient will sometimes work. Sometimes you get lucky. On a bad day, with a problem that is not quite so easy to work with, and where the approximations used by gradient are less accurate, you will see problems. That should be no surprise.
So what did you see? Crap happens with gradient near the endpoints. Again, gradient does NOT compute the exact derivative, just an approximation.
The point is, you got exactly what you might expect (ok, what you should have expected.) A far better estimate of the derivatives will be gained from the ODE function itself, in your case, f3. This is because that is the function ode15s used to solve the ODE itself.
  1 件のコメント
Paul Miller
Paul Miller 2023 年 5 月 30 日
I see, so using @Torsten's method of plugging the values back into f3 minimizes the error which I introduced using the gradient function, and it only worked well when I did it before because I was using it for the right functions. Thank you.



Help Center および File ExchangeOrdinary Differential Equations についてさらに検索




Community Treasure Hunt

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

Start Hunting!

Translated by