Plotting a function inside of another function with no outputs
5 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Voss
on 6 Mar 2024
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
More Answers (1)
Dyuman Joshi
on 6 Mar 2024
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."
5 Comments
Dyuman Joshi
on 7 Mar 2024
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
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!