How to find velocity and position of this ODE

16 次查看(过去 30 天)
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

回答(1 个)

David Goodmanson
David Goodmanson 2019-1-25
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.

类别

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