Function with 3 input arguments

22 ビュー (過去 30 日間)
Spaceman
Spaceman 2024 年 3 月 6 日
編集済み: Spaceman 2024 年 3 月 6 日
Given: Write a function to calculate the position of a projectile at any given time, t. The function should have 3 input arguments arguments: the initial velocity v_0, the initial launch angle theta_0, and the time at which you want to know the position. The function should then return the vertical (y) position of the projectile at that time. Assume that we are working in metric units with with acceleration of gravity g=9.81 m/s^2. Call your function projAlt_username. Remember, if we are launching a projectile at an angle, only a portion of that velocity will actually be in the vertical direction! For now, lets assume that the angle will be provided in degrees and the initial launch altitude (y_0) is zero.
v_y0=v_0*sin(theta_0)
y=y_0+v_y0*t-1/2*g*t^2
Find: Once we get this to work for a single value of time we can adjust the function to accept a vector of time values and return a vector of y values. Since we provided the vector of time values as an input argument, we now have everything needed in order to plot the altitude as a function of time.
Issue: I can't even get the code to run, I am just at a loss. Functions really throw me for a loop. (pun intended)
My Solution: I just know I am missing something fundamental here...
function y = projAlt_kweave19(v_0,theta_0,t)
% projAlt_kweave19 will return the vertical (y) position of the projectile at any given time t
% projAlt_kweave19 calculates the position of a projectile at any given
% time t with 3 input arguments: the initial velocity, V_0, the initial
% launch angle, theta_0, and the time at which you want to know the
% position. The function will then return the vertical (y) position of the
% projectile at the thtat time. Assume we are working in metric units with
% the acceleration of gravity g=9.81 m/s^2.
y_0=0
g=9.81
v_y0 = v_0*sin(theta_0);
y = y_0+v_y0*t-1/2*g*t^2;
end
I am using this line to call my function: v_0=100; theta_0=45; t=5; projAlt_kweave19

採用された回答

Stephen23
Stephen23 2024 年 3 月 6 日
編集済み: Stephen23 2024 年 3 月 6 日
"I just know I am missing something fundamental here..."
Exactly the same fundamental concept as your previous questions:
  • define the function with input arguments.
  • call the function with input values.
You defined a function with three input arguments, and then called it with zero inputs. How will that work?
A = 100;
B = 45;
C = 5;
D = projAlt_kweave19(A,B,C)
D = 302.8268
function y = projAlt_kweave19(v_0,theta_0,t)
% projAlt_kweave19 will return the vertical (y) position of the projectile at any given time t
% projAlt_kweave19 calculates the position of a projectile at any given
% time t with 3 input arguments: the initial velocity, V_0, the initial
% launch angle, theta_0, and the time at which you want to know the
% position. The function will then return the vertical (y) position of the
% projectile at the thtat time. Assume we are working in metric units with
% the acceleration of gravity g=9.81 m/s^2.
y_0=0;
g=9.81;
v_y0 = v_0*sin(theta_0);
y = y_0+v_y0*t-1/2*g*t^2;
end
  1 件のコメント
Spaceman
Spaceman 2024 年 3 月 6 日
編集済み: Spaceman 2024 年 3 月 6 日
Eureka! I don't know why it's so hard to grasp that I have to explicitly define the inputs in the function call. Thank you for clearing this up for me again. I am, as another wise user pointed out, a novice user. I guess my line of thinking is, I defined A,B, & C, THEN I called the function which has A,B, & C inside of it, with the expectation that it would extrapolate the information I filled in prior to calling it. I forget that the function call's workspace is completely independent of what's outside the workspace.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by