I have struggle with create a prompts for this can someone please help.

The maximum horizontal distance, d, traveled by a projectile fired is calculated using the following formula 𝑑 = 𝑣 ^2 /2𝑔 *( 1 + √1 + 2𝑔 𝑦0 /𝑣 2 sin2 𝜃 )𝑠𝑖𝑛2𝜃
Where • d is the total horizontal distance travelled by the projectile.
v is the velocity at which the projectile is launched
g is the gravitational acceleration: 9.81 𝑚/𝑠 2
θ is the angle at which the projectile is launched
𝑦0 is the initial height of the projectile
Write a MATLAB program that prompts the user to input values for 𝑣 and 𝑦0, and computes and outputs the distance 𝑑 for 𝜃 = 10°, 15°, 20°, … , 40°. Note that when 𝑦0 = 0, the formula becomes 𝑑 = 𝑣 2 𝑔 𝑠𝑖𝑛2𝜃. Use this fact to test your implementation for the general case.

9 Comments

"I have struggle with create a prompts for this can someone please help."
What have you tried so far?
Hi,
function d =distance(v,y0,degrees)
%input the velocity at which the projectile is launched
v=input('Enter the velocity at which the projectile is launched :')
%input the initial height of the projectile
y0=input('Enter the initial height of the projectile :')
%the gravitational acceleration
g= 9.81
degrees=10:5:40;
d = (v.*v/ (2*g)) * (1 + sqrt(1 + (2*g*y0) / (power(v,2) *power(sin(degress) ,
2)))))*sin(20);
end
I got this but i dont know i cannot get the function right, i keep getting error
"I got this but i dont know i cannot get the function right, i keep getting error"
Your code has a number of bugs and features:
  • you defined a variable named DEGREES but then in your calculation you try to access a non-existent variable named DEGRESS.
  • you used matrix operations throughout the calculation, but most likely you need to use array operations: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html Simple rule-of-thumb: if you are not doing linear algebra then you should probably use array operations.
  • you defined three function inputs, but then immediately reassigned those variable names to some other variables from INPUT calls. If you attempt to call the function with some provided values, it will ignore them.
  • while POWER is the correct function, it is more common to use the .^ operator.
Try fixing those and then come back to us.
What does "keep getting error" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
One thing I note from your code almost immediately, based on your variable name, is that you're working with angles in degrees. The sin and cos functions built into MATLAB compute the sine and cosine respectively of angles in radians. To compute the sine and cosine of angles in degrees use sind and cosd respectively.
I would also remove your input calls. You're asking the user who calls your function to pass in values for v and y0 and then promptly throwing them in the garbage and asking them to enter new values for those variables. Just use the ones that the caller of your function passed into the function. [Imagine if you called sind(45) and sind promptly asked you "Enter the angle whose sine you want to compute" -- you'd probably get at least a little annoyed with having to type 45 twice.]
Your function also either should use the value of the degrees input argument (and not overwrite that variable) or not require its caller to provide a value for degrees (and use the values you define in the function.)
function d=distance(v,y0,angle)
%input the velocity at which the projectile is launched
v=input('Enter the velocity at which the projectile is launched : ');
%input the initial height of the projectile
y0=input('Enter the initial height of the projectile : ' );
%g is the gravitational acceleartion
g=9.81;
%d is the total horizontal distance travelled by the projectile
angle=10:5:40;
theta=(1+sqrt(1+(2.*g.*y0)./(v.^2.*sind(angle).^2)));
angel=10:5:40;
d=(v.^2/(2.*g)).*theta*sin(20);
%display the output
fprintf('The total horizontal distance is %f \n',d)
end
Hi, i fixed it but i dont know if i get it right or not
You still have the input statements.
You are still throwing away the input arguments with which your function's user is calling your function.
You're computing sin(20) which probably ought to be sind(20).
Now you have variables named angle and angel -- that would be likely to cause confusion were it not for the fact that you don't use angel anywhere in the code after you defined it. I'd get rid of it.
Regarding determining if you've written your code correctly, check it! If this is a homework assignment (it definitely sounds like it is) look to see if there are examples of the type of problem you've written your code to solve in your textbook. If there are, run your code with the data from those examples and compare your results to the example results in the book. If there aren't, make up a problem and compare the results you get solving them with MATLAB to the results you get solving them by hand.
This doesn't guarantee that you've done it right if the answers match, but if they do match either you got the right answer or you got the same wrong answer in two different ways. Hopefully the former is more likely.
That code should have
function d = distance()
as you ignore the input parameters v, y0, angle.
Hi since the it just said sin20 in the textbook so i assume it should be radiant that's why i just put sin(20)
@Hong 𝑠𝑖𝑛2𝜃 means "sine of two times theta (the angle)", not "sine of 20 radians" or "sin(20)".

Sign in to comment.

Answers (1)

Hi Hong
I understand that you are facing issues in creating prompts to get input from the user.
You can consider using the "input" function to receive input from the user via MATLAB's command window. You can refer to the code snippet below:
% MATLAB program to compute projectile distance for various angles
% Clear variables and console
clear;
clc;
% Constants
g = 9.81; % Gravitational acceleration (m/s^2)
% Prompt user for input values
v = input('Enter the launch velocity v (m/s): ');
y0 = input('Enter the initial height y0 (m): ');
% Display the header for the output
fprintf('Angle (°)\tDistance (m)\n');
fprintf('-------------------------\n');
% Calculate and output the distance for angles 10° to 40°
for theta_deg = 10:5:40
% Convert angle from degrees to radians for computation
% Calculate the distance
% Output the distance for the current angle
end
You can refer to the MathWorks documentation below to learn more about "input" function:
Hope this helps!

1 Comment

Hi,
Thanks for helping but i also have the problem on try to put the formula into matlab bc i keep getting the message that it can comput

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Asked:

on 9 Feb 2024

Commented:

on 10 Feb 2024

Community Treasure Hunt

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

Start Hunting!