if true
% code
clear all
%Drag Parameters
Cd = 0.47;
p = 1.1225; %Air density at 15degrees
D = 0.043 ; %Diameter of Golf Ball
A = pi*(D/2)^2; %Cross Sectional area
D = (Cd*p*A)/2; %Drag Constant
%Projectile Parameters
alpha = 45; %Launch Angle
m = 0.045; %Mass of Golf Ball/kg
g = 9.81; %Acceleration Due to Gravity/m/s^2
w = m*g; %Weight of the Golf Ball/N
v = 40; %Initial Velocity/m/s
vxi = v*cos(alpha); %Initial Velocity Resolved in x
vyi = v*sin(alpha); %Initial Velocity Resolved in y
%Arrays
n = 100;
xdisp = zeros(1, n+1); %x-displacment Array
ydisp = zeros(1, n+1); %y-displacment Array
vx = zeros(1, n+1); %x-velocity Array
vx(1) = vxi;
vy = zeros(1, n+1); %y-velocity Array
vy(1) = vyi;
t = zeros(1, n+1); %Time Array
%Iteration
dt = 0.01;
i=1;
while i<n
ax=-(D/m)*v.*vx;
ay=-g-(D/m)*v.*vy;
vx=vx+ax*dt;
vy=vy+ay*dt;
xdisp(i+1)=xdisp(i)+vx(i)*dt+0.5*ax*dt^2;
ydisp(i+1)=ydisp(i)+vy(i)*dt+0.5*ay*dt^2;
t(i+1)=t(i)+dt;
v(i+1) = sqrt(vx(i).^2 + vy(i).^2);
i=i+1;
end
plot(x,y)
end
On the line xdisp(i+1)=xdisp(i)+vx(i)*dt+0.5*ax*dt^2;
i get the error In an assignment A(:) = B, the number of elements in A and B must be the same.
Any help would be greatly appreciated

댓글 수: 1

Giulio Giovannetti
Giulio Giovannetti 2022년 9월 22일
편집: Giulio Giovannetti 2022년 9월 22일
Pay attention to the vxi and vyi initialization, you should use cosd(*) instead of cos(*) since the angle has been defined in deg.
vxi = v*cosd(alpha); %Initial Velocity Resolved in x
vyi = v*sind(alpha); %Initial Velocity Resolved in y

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2017년 12월 11일

0 개 추천

As you have written your code, you need to subscript all your vectors:
while i<n
ax(i)=-(D/m)*v(i).*vx(i);
ay(i)=-g-(D/m)*v(i).*vy(i);
vx(i+1)=vx(i)+ax(i)*dt;
vy(i+1)=vy(i)+ay(i)*dt;
xdisp(i+1)=xdisp(i)+vx(i)*dt+0.5*ax(i)*dt^2;
ydisp(i+1)=ydisp(i)+vy(i)*dt+0.5*ay(i)*dt^2;
t(i+1)=t(i)+dt;
v(i+1) = sqrt(vx(i).^2 + vy(i).^2);
i=i+1;
end
I cannot figure out what you want to plot. I leave that for you to sort.

댓글 수: 2

Conor Stevenson
Conor Stevenson 2017년 12월 11일
편집: Conor Stevenson 2017년 12월 11일
Thank you for your input, since posting i have reproached and solved the issue
Star Strider
Star Strider 2017년 12월 11일
My pleasure.
I’m glad you sorted it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Simulation에 대해 자세히 알아보기

질문:

2017년 12월 11일

편집:

2022년 9월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by