Plotting a function inside of another function with no outputs

3 ビュー (過去 30 日間)
Spaceman
Spaceman 2024 年 3 月 6 日
コメント済み: Spaceman 2024 年 3 月 21 日
Given: Not all functions have to have output arguments. You could have a function that does something, but doesn't return an actual value.
Find: Create a function called plotProj_username that accepts an initial velocity, initial launch angle, and a time vector. This function will call your projAlt_username function and provide it with the same input arguments. It will then plot the altitude as a function of time.
Issue: I have patched up some discrepensies of mine when dealing with creating functions and calling them... However I am now faced with calling a function from within another function with no output, but instead, a plot.
My Solution: This is the original function I am tasked with plotting
function y = projAlt_kweave19(v_0,theta_0,t)
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
% Code used to call function v_0=200; theta_0=45; t=(1:1:100); Y=projAlt_kweave19(v_0,theta_0,t)
Below is the function I have created to do so
function plotProj_kweave19(v_0,theta_0,t)
y_0=0;
g=9.81;
v_y0 = v_0*sin(theta_0);
y = y_0+v_y0.*t-1/2*g.*t.^2;
plot(projAlt_kweave19);
title('Altitude as a function of time')
xlabel('Altitude'); ylabel('Time');
end
% Code used to call function v_0=200; theta_0=45; t=(1:1:100); alt_t=(projAlt_kweave19(v_0,theta_0,t))
it gives me an error: Array indices must be positive integers or logical values.
I am not sure what changed because before I was getting a figure 1 that looked spot on.

採用された回答

Voss
Voss 2024 年 3 月 6 日
You need to pass the inputs to projAlt_kweave19
v_0=200; theta_0=45; t=(1:1:100);
plotProj_kweave19(v_0,theta_0,t) % call plotProj_kweave19 (with inputs)
function y = projAlt_kweave19(v_0,theta_0,t)
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
function plotProj_kweave19(v_0,theta_0,t)
% y_0=0; % no need to calculate
% g=9.81; % this stuff in here.
% v_y0 = v_0*sin(theta_0); % projAlt_kweave19 calculates it
% y = y_0+v_y0.*t-1/2*g.*t.^2; % and returns something you plot next:
plot(projAlt_kweave19(v_0,theta_0,t)); % <-- pass inputs to projAlt_kweave19 here
title('Altitude as a function of time')
xlabel('Altitude'); ylabel('Time');
end
  2 件のコメント
Spaceman
Spaceman 2024 年 3 月 7 日
EUREKA! Man this one was giving me so much grief. I was trying to call the function and plot it sans the inputs. That is exacly what I was looking for. Thank you so much. It was really bumming me out I couldn't figure this out. This stuff really isnt intuitive to me but I try to get as much practice as possible with my limited time.
Voss
Voss 2024 年 3 月 7 日
You're welcome!

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

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2024 年 3 月 6 日
Firstly, do not use built-in functions as names for variables (or scripts for that matter). You are using plot as a variable name -
% vvvv
% Code used to call function v_0 = 200; theta_0 = 45; t = (1:1:100); plot =
% (projAlt_kweave19(v_0,theta_0,t))
You should change the variable name.
Secondly, why not include the call to plot() in the same function?
"Which leads me to believe I am doing something wrong here"
If you don't want to return the handle of the the plot, suppress the output using a semi-colon.
"I am also unsure of how to add labels and a title."
See the documentation - title (and the last section in the same page)
  5 件のコメント
Dyuman Joshi
Dyuman Joshi 2024 年 3 月 7 日
In that case -
%Values
v_0=200; theta_0=45; t=(1:1:100);
%Call the plotting function
plotProj_kweave19(v_0,theta_0,t)
%% function definition
function plotProj_kweave19(v_0,theta_0,t)
%call the (helper) function
y = projAlt_kweave19(v_0, theta_0, t);
%and plot
plot(y)
title('Altitude as a function of time')
xlabel('Altitude'); ylabel('Time');
end
function y = projAlt_kweave19(v_0,theta_0,t)
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
Spaceman
Spaceman 2024 年 3 月 21 日
Genius. Thank you so much for yout help!

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by