how to take the derivative for the retained value from ode45?
古いコメントを表示
I'm using the ode45 to find the velocity, now I need to find the acceleration how can I take the derivative of the retained value.
My ode45 is:
[ts,xs]= ode45(@my_function,[0,10],[5,0])
Thanks in advance!
回答 (2 件)
Walter Roberson
2013 年 7 月 31 日
gradient(xs(1,:), ts(:))
6 件のコメント
Richard Brown
2013 年 7 月 31 日
編集済み: Richard Brown
2013 年 8 月 1 日
With gradient you provide the spacing, not the independent variable coordinates. Also, you need to index the column not row of the solution.
If you get the ode solver to return values at uniformly spaced points, you can then use gradient
e.g.
h = 1e-2;
ts = 0:h:10;
[~,xs] = ode45(@my_function, ts, [5, 0]);
gradient(xs(:, 1), h);
assuming velocity is your first variable
edit actually you can provide the coordinates, as Walter points out below). So the only correction needed is
gradient(xs(:, 1), ts)
I'll leave the rest of the comment up to avoid confusion later, but that's all you need
Walter Roberson
2013 年 7 月 31 日
Thanks, Richard.
Richard Brown
2013 年 7 月 31 日
No problem. Confusingly the symbolic toolbox gradient works the way you described
Walter Roberson
2013 年 8 月 1 日
Richard,
gradient(xs(:,1), ts)
will work as well. The use of a coordinate vector instead of a scalar spacing is documented for the 2D case but not for the 1D case, but it works anyhow.
Richard Brown
2013 年 8 月 1 日
I stand corrected, good to know!
Jan
2013 年 8 月 1 日
gradient uses a first order method when the spacing is not equidistant. You can use the faster method FEX: DGradient and some other equivalent tools from the FEX, which apply a 2nd order method to get more accurate results for the derivatives.
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!