필터 지우기
필터 지우기

Modeling a Projectile in Matlab

조회 수: 2 (최근 30일)
Mohamed Saeed
Mohamed Saeed 2015년 4월 12일
답변: Taylor McMillan 2018년 6월 8일
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