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
0 个评论
回答(1 个)
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.
1 个评论
madhan ravi
2019-1-25
ode45 with no output arguments plots by default thereby avoiding the plot call.
另请参阅
类别
在 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!