creating a function to find horizontal distance given initial velocity and initial angle.

2 ビュー (過去 30 日間)
Cailin Grant
Cailin Grant 2018 年 8 月 6 日
回答済み: Adam Danz 2018 年 8 月 7 日
Im trying to create a function that will allow for an input variable of the initial velocity, v and the initial angle, a; that comes up with the output of the horizontal distance once the object gets back to the starting y position.
so far I have
v = input('enter cannon ball exit velocity(m/s):'); %input of the exit velocity of the cannon in m/s a = input('enter cannon ball exit angle from horizon(degrees):'); %input of the exit angle from the horizon of the cannon ball in degrees
function [x]=cannon(v,a) g = -9.8; %gravitaitional constant in m/s^2 t = (2*(v*sin(a)))/g; %equation for the time until cannon hits the ground at the inpact location x = (v*cos(a))*t %equation for the horizontal distance where the cannon hits the ground at the impact location end
  2 件のコメント
Dimitris Kalogiros
Dimitris Kalogiros 2018 年 8 月 6 日
編集済み: Dimitris Kalogiros 2018 年 8 月 6 日
1) what is the quetion ?
2) use the button with {} Code on it , when you want to insert code into your question
Adam Danz
Adam Danz 2018 年 8 月 7 日
Is this a coding question or a conceptual question?

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2018 年 8 月 7 日
It's not clear whether your question is related to code or concept. Concerning the code, you pretty much already have it written. I filled in some lines and reformatted it so that your function is called after the inputs are collected and it produces an output printed in the command window.
v = input('enter cannon ball exit velocity(m/s):'); %input of the exit velocity of the cannon in m/s
a = input('enter cannon ball exit angle from horizon(degrees):'); %input of the exit angle from the horizon of the cannon ball in degrees
x = cannon(v,a);
fprintf('The horizontal displacement is %.2f.\n', x)
function x = cannon(v,a)
g = -9.8; %gravitaitional constant in m/s^2
t = (2*(v*sin(a)))/g; %equation for the time until cannon hits the ground at the inpact location
x = (v*cos(a))*t %equation for the horizontal distance where the cannon hits the ground at the impact location end
end
I haven't looked at the code conceptually.

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by