Derivative from ode45
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (2개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!