Question

In: Computer Science

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!

Solutions

Expert Solution

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


Related Solutions

You measure the distance, l, a projectile travels several times using the same apparatus and find...
You measure the distance, l, a projectile travels several times using the same apparatus and find the following values (cm): 5.11, 4.89, 5.04, 4.94, 4.93, 5.06 and 5.09. Assuming that the errors are random: a) What is the mean value forl? b) What is the standard deviation for this data set? c) What is the uncertainty associated with your best estimate forl? d) How should one report the result of this measurement along with the uncertainty at the 95 %...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
MATLAB Write a user defined function for a projectile motion. If a ball is launched from...
MATLAB Write a user defined function for a projectile motion. If a ball is launched from initial position(0,0) with a velocity v0 at angle θ, determine your horizontal and vertical position. Please plot x vs. t, y vs. t and y vs. x.
Using MATLAB: The velocity, v, and the distance, d, as a function of time, of a...
Using MATLAB: The velocity, v, and the distance, d, as a function of time, of a car that accelerates from rest at constant acceleration, a, are given by = a n d = 12 Determine v and d at every second for the first 10 seconds for a car with acceleration of = 15 ft/s2. Your output must have exactly the same format as the template table below. Note that dots have been added to the table below; you can...
PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance...
PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance = speed * time For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each...
MATLAB QUESTION: I have to write a function that pretty much cleans number. these are the...
MATLAB QUESTION: I have to write a function that pretty much cleans number. these are the instructions Inputs: number1 - a 1d of array of number, size doesn't matter outputs: number2 - all number in number1 that are 0 need to be set to nan and return via number2. data - storing the number of 0's in number1 that pretty much are being set to nans here's the code: function [number2, data] = cleanNumber(number1) %add your code below.. end code...
Write a new MATLAB function as described in the following: 1. The new function will have...
Write a new MATLAB function as described in the following: 1. The new function will have three input parameters: f, W, C. 2. Parameter f will specify the frequency of the square wave. 3. The parameter W will specify the width of the single pulse as a number of sample periods. 4. The parameter C will specify the number of square wave cycles. 5. Calculate a number of samples, N, to run the simulation for both waveforms. 6. Create one...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
I am writing a matlab program where I created a figure that has a few different...
I am writing a matlab program where I created a figure that has a few different elements to it and now I need to move that figure into a specific excel file into a specific set of boxes in the excel file. With numbers and text I have always used the xlswrite function for this in order to put data into specific boxes. How do I do the same with this figure? The figure I have is called like this:...
I'm new in MATLAB and I have to write a code in MATLAB which converts a...
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT