Question

In matlab I have created the following function that calculates the distance a projectile travels when...

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`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.

Add a comment
Know the answer?
Add Answer to:
In matlab I have created the following function that calculates the distance a projectile travels when...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In matlab I have created the following function function [distance, xplot, yplot] = Cannon_lab8(V, theta) %...

    In matlab I have created the following function 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...

  • Write a MATLAB Graphical User Interface (GUI) to simulate and plot the projectile motion – the...

    Write a MATLAB Graphical User Interface (GUI) to simulate and plot the projectile motion – the motion of an object projected into the air at an angle. The object flies in the air until the projectile returns to the horizontal axis (x-axis), where y=0. This MATLAB program should allow the user to try to hit a 2-m diameter target on the x-axis (y=0) by varying conditions, including the lunch direction, the speed of the lunch, the projectile’s size, and the...

  • Question 3 (20 marks) Two random variables X and Y have the following joint probability density...

    Question 3 (20 marks) Two random variables X and Y have the following joint probability density function. $(x,y)={{(6xy? +3y2 + 2x+) 0<x<2, 1<y<2, 0, otherwise Determine the value of the constant k. (5 Determine the marginal density functions and hence check if X and Y are inde Work out the difference between E{var(Y|X} and V ar{ECY|x)} (10 ho Question 4 (20 marks) ay -Express x in terms of Work our screen of /ke X VY J=dx

  • Consider the following initial value problem: dy = sin(x - y) dx, y(0) 1. Write the...

    Consider the following initial value problem: dy = sin(x - y) dx, y(0) 1. Write the equation in the form ay = G(ax +by+c), dx where a, b, and c are constants and G is a function. 2. Use the substitution u = ax + by + c to transfer the equation into the variables u and x only. 3. Solve the equation in (2). 4. Re-substitute u = ax + by + c to write your solution in terms...

  • The temperature at a point (x, y) is T(x,y), measured in degrees Celsius. A bug crawls...

    The temperature at a point (x, y) is T(x,y), measured in degrees Celsius. A bug crawls so that its position after t seconds is given by x = 4 + t, y = 8 where x and y are measured in centimeters. The temperature function satisfies Tx(5, 9) = 2 and Ty(5, 9) = 7. How fast is the temperature rising on the bug's path after 21 seconds? Step 1 We know that the rate of change of the temperature...

  • A firm’s demand function is p=60-0.5Q If fixed cost are 10 and variable cost are Q+3...

    A firm’s demand function is p=60-0.5Q If fixed cost are 10 and variable cost are Q+3 per unit, find the maximum profit. Q2. Which of the following could represent a function, f(x,y), with first-order partial derivatives af/ax = 3xy(xy+2) af/ay=x^2(2xy+3) Q3. Find the value of dy/dx at the point (-2,1) for the function which is defined implicitly by x^2 - x/y =6 Q4. Find the value of the marginal rate of technical substitution for the production function Q=300 (K^2/3 )(L^1/2)...

  • 1 15 oints) Deterine if the following propositions are TRUE or FALSE. Note that p, q...

    1 15 oints) Deterine if the following propositions are TRUE or FALSE. Note that p, q r are propositi Px) and P(x.y) are predicates. RUE or FALSE.Note that p, q, r are propositions. (a).TNE 1f2小5or I + 1-3, then 10+2-3or 2 + 2-4. (b).TRvE+1 0 if and only if 2+ 2 5. (d). _ p v T Ξ T, where p is a proposition and T is tautology. V x Px) is equivalent to Vx - Px) (g). ㅡㅡㅡ, y...

  • can i have a step by step of 1, 3, and 5 please In Problems 1-6,...

    can i have a step by step of 1, 3, and 5 please In Problems 1-6, determine whether the given equation is separable, linear, neither or both. dx 1. +xt = ex 2. 2dy +sin x y = 0 dt dx 3. 3t e'+ y ln t 4. (t+ \ dt yt - y dy Srortar dr 5. 3r = de vorla (d) dx TU6. x +Px = sin t dt 03

  • CONVERT THE FOLLOWING MATLAB CODE FROM SOURCE PANEL METHOD TO VORTEX PANEL METHOD: clc;clear all;...

    CONVERT THE FOLLOWING MATLAB CODE FROM SOURCE PANEL METHOD TO VORTEX PANEL METHOD: clc;clear all;close all; Vinf=100; % freestream velocity R=1; % cylinder radius n=4; % number of panels alpha=2; % angle of attack dtheta=2*pi/n; theta=pi+pi/n:-dtheta:-pi+pi/n; X=R*cos(theta); Y=R*sin(theta); for i=1:n % angle of flow with tangent of panel phi(i)=-alpha+atan2((Y(i+1)-Y(i)),(X(i+1)-X(i))); % angle of flow with normal of panel beta(i)=phi(i)+pi/2; x_mid(i)=(X(i+1)+X(i))/2; y_mid(i)=(Y(i+1)+Y(i))/2; S(i)=sqrt((Y(i+1)-Y(i))^2+(X(i+1)-X(i))^2); end % Source Panel Method for j=1:n neighbors(:,j)=[1:j-1 j+1:n]; xi=x_mid(j); yi=y_mid(j); for i=1:n-1 m=neighbors(i,j); Xj=X(m); Yj=Y(m); Xj1=X(m+1); Yj1=Y(m+1); A=-(xi-Xj)*cos(phi(m))-(yi-Yj)*sin(phi(m));...

  • Part B: Launching a Projectile at an Angle (1) Erase the trajectory from part A (yellow...

    Part B: Launching a Projectile at an Angle (1) Erase the trajectory from part A (yellow eraser button). (2) Ensure that the initial height (h = 10 m) and initial speed (1 = 15 m s 1) remain equal to the quantities given in part A. (3) Position the projectile launcher so that it launches at an angle of 20°. (4) Fire the launcher (using the red fire button) and record the x-displacement by positioning the center of the target...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT