Modeling a Projectile in Matlab

2 次查看(过去 30 天)
Hello, I was wondering if anyone could help me figure out what's wrong with my matlab code, I'm attempting to use the Euler method to plot the trajectory of a cannon but I want it to plot only until y >= 0 and I don't know how to do it. I tried using the return function but that doesn't work.
Here is my code:
function projectile(theta,v0,dt,tf)
t = 0:dt:tf;
vx = v0*cos(theta);
vy = v0*sin(theta);
vx(1) = vx;
vy(1) = vy;
v = sqrt((vx^2)+(vy^2));
v(1) = v;
x(1) = 0;
y(1) = 0;
B2divm = 0.00004;
g = 9.81;
for i = 1:length(t)-1
v = sqrt((vx(i)^2)+(vy(i)^2));
x(i+1) = x(i) + vx(i)*dt;
vx(i+1) = vx(i) - (B2divm*v*vx(i))*dt;
y(i+1) = y(i) + vy(i)*dt;
vy(i+1) = vy(i) - g*dt - (B2divm*v*vy(i))*dt;
end
plot(x,y,'r')
end

回答(1 个)

Taylor McMillan
Taylor McMillan 2018-6-8
Try ylim([0, inf]).

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by