How to find velocity and position of this ODE

34 views (last 30 days)
Hello all.
I've been trying to find the velocity and position of an object with an accelration rate of a(x)=-2x+1 m/s^2 with the initial conditions of v(0) = 4 m/s, and x(0) = 5 m, all at a time of 5 seconds.. I did my code on it, and believed to have found position with my current plot, however I can't seem to figure out how to calculate and plot my x versus t graph for velocity. Can someone help me out please?
Code below:
[T,s]=ode45(@eom,linspace(0,5,101),[5,4]);
plot(T,s(:,l),'-k')
xlabel('Time, sec')
ylabel('x,m')
function ds=eom(T,s)
ds(1,1)=s(2)
ds(2,1)=-2*s(1)+1
end
%Above function was for position, which seems to work.
%Below was my attempt at the velocity plot.
function ds=eom(T,s)
ds(1,1)=s(2)
ds(2,1)=-2*s(2)^2+s(1)_
end

Answers (1)

David Goodmanson
David Goodmanson on 25 Jan 2019
Hi Peter,
since you have defined s(2) = ds(1)/dt, s(2) is the velocity function already. No more work required, exceptto plot it,
[T,s]=ode45(@eom,linspace(0,5,101),[5,4]);
plot(T,s)
xlabel('Time, sec')
legend('x, m','v ,m/s','location','southeast')
function ds=eom(T,s)
ds(1,1)=s(2)
ds(2,1)=-2*s(1)+1
end
Here the plot function plots out the colums of s independently, so you get x and v.
  1 Comment
madhan ravi
madhan ravi on 25 Jan 2019
ode45 with no output arguments plots by default thereby avoiding the plot call.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!