Plotting a function inside of another function with no outputs

5 visualizaciones (últimos 30 días)
Spaceman
Spaceman el 6 de Mzo. de 2024
Comentada: Spaceman el 21 de Mzo. de 2024
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

Voss
Voss el 6 de Mzo. de 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
  2 comentarios
Spaceman
Spaceman el 7 de Mzo. de 2024
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.

Iniciar sesión para comentar.

Más respuestas (1)

Dyuman Joshi
Dyuman Joshi el 6 de Mzo. de 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."
See the documentation - title (and the last section in the same page)
  5 comentarios
Dyuman Joshi
Dyuman Joshi el 7 de Mzo. de 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
Spaceman
Spaceman el 21 de Mzo. de 2024
Genius. Thank you so much for yout help!

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by