Derivative from ode45

3 ビュー (過去 30 日間)
jep
jep 2020 年 4 月 17 日
回答済み: madhan ravi 2020 年 4 月 17 日
I have a code that solves a second order differential equation denoted in a function called dfq
clear all
close all
y0 = [0;0];
tspan = [0 15];
[t,y] = ode45(@dfq,tspan,y0);
xresp = y(:,1);
plot(t,xresp)
title('Solution Plot')
xlabel('t(s)')
ylabel('x(m)')
grid on
where the response x(t) is delivered and plotted through xresp. I need to plot the derivative of xresp, or x(t), and attempted to use diff to do this but it shortened the size of the vector and could not be plotted. I am wondering if there is a better way to graph the derivative. I also tried to use gradient on xresp, which did not give the correct derivative plot either.

回答 (2 件)

Peter O
Peter O 2020 年 4 月 17 日
編集済み: Peter O 2020 年 4 月 17 日
Generally I send the result of ode45 back through my function as a post-processing step.
[t,x] = ode45(@myDerivFcn,tspan,x0)
You just need to be a little cautious of the problem dimensionality. arrayfun can be a neat trick if you've got a one-dimensional system or don't mind manipulating the cell array after the fact. Otherwise, a venerable for loop works:
dx = nan(n_dims_x,length(t))
for ix=1:length(t)
dx(:,ix) = myDerivFcn(t(ix),x(ix,:).') % Col major fill
end
dx = dx.' % Now it matches the dimensionality of x and has length(t) rows

madhan ravi
madhan ravi 2020 年 4 月 17 日

カテゴリ

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