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.

댓글을 달려면 로그인하십시오.

추가 답변 (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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by