Azzera filtri
Azzera filtri

Modeling a Projectile in Matlab

2 visualizzazioni (ultimi 30 giorni)
Mohamed Saeed
Mohamed Saeed il 12 Apr 2015
Risposto: Taylor McMillan il 8 Giu 2018
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

Risposte (1)

Taylor McMillan
Taylor McMillan il 8 Giu 2018
Try ylim([0, inf]).

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by