In: Computer Science
In matlab I have created the following function that calculates
the distance a projectile travels when shot from
a cannon given initial velocity and theta.
function [distance, xplot, yplot] = Cannon_lab8(V, theta)
g = -9.81; % gravity m/s^2
k = 0.35; % drag coefficient
Vx = V*cos(theta); %velovity in x-direction
Vy = V*sin(theta); %velovity in y-direction
dt = 0.01; % seconds, time step
x = 0;
y = 0;
xplot(1) = 0;
yplot(1) = 0;
i = 2;
while y >= 0
ax = -k*Vx;
ay = -k*Vy + g;
Vix = Vx;
Viy = Vy;
Vx = Vx + ax*dt;
Vy = Vy + ay*dt;
Vx_avg = (Vix +Vx)/2;
Vy_avg = (Viy +Vy)/2;
dy = Vy_avg * dt;
dx = Vx_avg * dt;
y = y + dy;
x = x +dx;
xplot(i) = x;
yplot(i) = y;
i = i + 1;
end
distance = x; % meters
scatter(xplot,yplot)
axis equal
I am now trying to determine the theta value needed to hit a target. The general aproach to solving for theta is by creating a new function that takes the guess value of theta, the initial velocity, and target distance, and solves for theta needed to hit the target. In this function the distance that the cannon misses the taret by can be calculated and this can be repeaditly calculated until the miss is within 2. so far I have:
function [theta_target] = aim_lab9(theta_guess, target,
V)
miss = Cannon_lab8(V, theta) - target;
dtheta = 0.1;
while abs(miss) > 2
miss = Cannon_lab8(V, theta_guess) - target;
theta_new = theta_guess + dtheta;
miss2 = Cannon_lab8(V,theta_new) - target;
end
I need help determining the theta_target. I would also like for the code to check and see if the target is within range.
thank you!
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Basically instead of calculating miss2 you should actually increment theta by a factor in a loop. Check only miss, miss2 is not needed since we are already using loop to calculate miss again at new theta
Also, abs(miss)>2 is very big error 2 in angle means 2*180/pi=114 degree approx. So, every theta you pass will break the loop because error chosen by you I very large. Also, decrement the step accordingly.
I have corrected the code
aim_lab9(0.001,3,10)
function [theta_target] = aim_lab9(theta_guess, target,
V)
miss = Cannon_lab8(V, theta_guess) - target;
dtheta = 0.001;
while abs(miss) > 0.01
miss = Cannon_lab8(V, theta_guess) - target;
theta_guess = theta_guess + dtheta;
if(theta_guess>=pi/2)
disp('out of range');
break;
end
end
theta_target=theta_guess;
end
function [distance, xplot, yplot] = Cannon_lab8(V, theta)
% the function calculates the distance a projectile travels when
shot from
% a cannon given initial velocity and theta.
g =
-9.81; % m/s^2
k = 0.35; % drag coefficient
Vx = V*cos(theta);
Vy = V*sin(theta);
dt = 0.01; % seconds
x = 0;
y = 0;
xplot(1) = 0;
yplot(1) = 0;
i = 2;
while
y >= 0
ax = -k*Vx;
ay = -k*Vy + g;
Vix = Vx;
Viy = Vy;
Vx = Vx + ax*dt;
Vy = Vy + ay*dt;
Vx_avg = (Vix +Vx)/2;
Vy_avg = (Viy +Vy)/2;
dy = Vy_avg * dt;
dx = Vx_avg * dt;
y = y + dy;
x = x +dx;
xplot(i) = x;
yplot(i) = y;
i = i + 1;
end
distance = x; % meters
scatter(xplot,yplot)
axis equal
end
Kindly revert for any queries
Thanks.