Plotting a function inside of another function with no outputs

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.

 Respuesta aceptada

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 comentarios

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.
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

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 comentarios

Spaceman
Spaceman el 6 de Mzo. de 2024
Editada: Spaceman el 6 de Mzo. de 2024
Genius. I forget using built-in functions as variable names is a big no-no. Specifically i and j.
I am now starting to see why coders are regarded in such high fashion. It's an art.
You can not plot a function. You can only plot data.
Go through the documentation page of the function you are using to learn what inputs are accepted, and what syntaxes can be used.
I've merged the code into a single function -
% Code used to call function
v_0=200; theta_0=45; t=(1:1:100);
Y=projAlt_kweave19(v_0,theta_0,t)
Y = 1x100
1.0e+04 * 0.0165 0.0321 0.0466 0.0602 0.0728 0.0845 0.0951 0.1048 0.1134 0.1211 0.1278 0.1336 0.1383 0.1421 0.1449 0.1467 0.1476 0.1474 0.1463 0.1442 0.1411 0.1370 0.1319 0.1259 0.1189 0.1109 0.1019 0.0920 0.0810 0.0691
%% function definition
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;
plot(t,y);
title('Altitude as a function of time')
xlabel('Altitude'); ylabel('Time');
end
Spaceman
Spaceman el 6 de Mzo. de 2024
Editada: Spaceman el 6 de Mzo. de 2024
This works, I think in this particular thought experiment, I am tasked with calling the above function inside of another function, then plotting the DATA inside of the function I called. . . I am still troubleshooting on how to do that. It will end up being two seperate function files. projAlt_username & plotProj_username.
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
Genius. Thank you so much for yout help!

Iniciar sesión para comentar.

Productos

Versión

R2023b

Etiquetas

Preguntada:

el 6 de Mzo. de 2024

Comentada:

el 21 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by