I have struggle with create a prompts for this can someone please help.
Show older comments
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
Stephen23
on 9 Feb 2024
"I have struggle with create a prompts for this can someone please help."
What have you tried so far?
Hong
on 9 Feb 2024
Edited: Walter Roberson
on 9 Feb 2024
Stephen23
on 9 Feb 2024
"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.
Steven Lord
on 9 Feb 2024
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.)
Hong
on 9 Feb 2024
Steven Lord
on 9 Feb 2024
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.
Walter Roberson
on 9 Feb 2024
Hong
on 9 Feb 2024
Image Analyst
on 10 Feb 2024
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!
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!