Plot the acceleration with ODE45

조회 수: 5 (최근 30일)
Elnur Shahbalayev
Elnur Shahbalayev 2020년 11월 29일
댓글: Elnur Shahbalayev 2020년 11월 29일
Hello, I have the problem to plot accelaration i matlab with ODE45 function.
Here is the question:
I wrote the function like this:
function dxdt = vdp1(t,x)
dxdt = [x(2); (9.8 - 0.00324*sign(x(2))*x(2)^2-(1/7)*(x(1)-150)*heaviside(x(1)-150))];
end
For part 1 and 2,to get results I wrote this code:
%Part 1
[t,x] = ode45(@vdp1,[0 11.47], [0;0])
plot(t,x(:,1),'-o',t,x(:,2),'-o')
title('Solution with ODE45');
xlabel('Time t');
ylabel('Solution x');
legend('x_1','x_2')
% Part 2
[t,x] = ode45(@vdp1,[0 5.988], [0;0])
plot(t,x(:,1),'-o',t,x(:,2),'-o')
title('Solution with ODE45');
xlabel('Time t');
ylabel('Solution x');
% legend('x_1','x_2')
[t,x] = ode45(@vdp1,[0 11.47], [0;0])
plot(t,x(:,1),'-o',t,x(:,2),'-o')
title('Solution with ODE45');
xlabel('Time t');
ylabel('Solution x');
legend('x_1','x_2')
Now, I don't know how to find accelaration with with this way. Please can anyone help me. Thanks in advance.

답변 (1개)

James Tursa
James Tursa 2020년 11월 29일
편집: James Tursa 2020년 11월 29일
You find acceleration by taking the x solution from ode45( ) and feeding that back into your derivative function vdp1( ). Since your derivative function is not vectorized, you will have to do this in a loop.
  댓글 수: 3
James Tursa
James Tursa 2020년 11월 29일
편집: James Tursa 2020년 11월 29일
E.g., something like this:
[t,x] = ode45(@vdp1,[0 11.47], [0;0]); % solve the ode
n = size(x,1); % figure out how many rows are in the solution
xdot = zeros(size(x)); % allocate the derivative matrix
for k=1:n % loop through each solution vector to fill in the derivative matrix
xdot(k,:) = vdp1(t(k),x(k,:)); % calculate the derivative at time t(k)
end
acceleration = xdot(:,2); % pick off the acceleration values
Elnur Shahbalayev
Elnur Shahbalayev 2020년 11월 29일
Thank you so much for your help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by