How to use a variable only defined in a function in your script?
조회 수: 21 (최근 30일)
이전 댓글 표시
Hi,
I created a function that takes the user's values for the angle and I want to use those values in my script but when I try putting my function in the script and running it, Matlab tells me that variable is undefined.
Would making the variable global fix this or is this only between functions? (If it works please show me how to make a variable global).
THANKS!
댓글 수: 5
madhan ravi
2018년 10월 25일
don't use angle as your function name it may contradict which inbuilt function name which returns phase angle!
답변 (3개)
Image Analyst
2018년 10월 25일
You need to return the value in your function to see in in the calling script. Like this
[var1, var2] = ComputeValue(input1, input2); % Call the function
% Define the function
function [var1, var2] = ComputeValue(input1, input2); % Call the function
% some code..... then
var1 = .... whatever.
var2 = .... whatever.
If it doesn't say the variable names on the function line, and you don't accept those values into the main calling script, then you won't have those values in the main calling script.
댓글 수: 2
Image Analyst
2018년 10월 25일
MATLAB is not call by reference - it's call by value. So you have to return values for them to change in the calling routine, and you don't pass them in as input arguments. And you don't pass back t from angle() since t is never even defined. Do it like this:
function [theta_min, theta_max, theta] = angle()
theta_min=deg2rad(input('Enter the lower value of the angle range (deg):'));
theta_max=deg2rad(input('Enter the upper value of the angle range (deg):'));
theta = linspace(theta_min, theta_max, 10);
end
Then, when you call it, instead of doing this:
angle(theta_min,theta_max,theta) % Wrong way!
you do this:
[theta_min, theta_max, theta] = angle() % Right way.
Stephen23
2018년 10월 25일
Your usage of the function angle has several bugs, in particular:
- You do not call angle with any output arguments, although you need it later in your code.
- You define three input arguments to angle, but ignore all of them inside the function.
- You define one output argument to angle, but do not define it anywhere inside the function itself.
Here is simpler code without a function:
x0 = str2double(input('Enter the initial horizontal position: ','s'));
y0 = str2double(input('Enter the initial vertical position: ','s'));
v0 = str2double(input('Enter the initial velocity: ','s'));
theta_min = str2double(input('Enter the lower value of the angle range (deg): ','s'));
theta_max = str2double(input('Enter the upper value of the angle range (deg): ','s'));
theta = linspace(deg2rad(theta_min),deg2rad(theta_max),10);
for k = 1:numel(theta)
ProjectileMotion(x0,y0,v0,theta(k));
end
If you really want to use a function named angle, then use this:
function t = angle(t_min,t_max)
t = linspace(t_min,t_max,10);
end
and then call it like this:
theta = angle(deg2rad(theta_min),deg2rad(theta_max));
But to be honest, I don't see much point in doing that.
Note that ProjectileMotion also has three output arguments, which you ignore totally when you call the function.
댓글 수: 2
Stephen23
2018년 10월 25일
편집: Stephen23
2018년 10월 25일
"I'm not sure I understand the function you gave me, it doesn't ask the user for the inputs."
Because I moved those input calls to outside of the function. Your function was ambiguously written because you had both input arguments (which got discarded) and input calls (which redefined the input arguments). It is better to use the input arguments, which is why I moved the input calls outside the function. You can easily move them back inside, if you wish:
function t = angle() % no input arguments!
t_min = str2double(input('Enter the lower value of the angle range (deg): ','s'));
t_max = str2double(input('Enter the upper value of the angle range (deg): ','s'));
t = linspace(deg2rad(t_min),deg2rad(t_max),10);
end
And then call it like this:
x0 = str2double(input('Enter the initial horizontal position: ','s'));
y0 = str2double(input('Enter the initial vertical position: ','s'));
v0 = str2double(input('Enter the initial velocity: ','s'));
theta = angle(); % no input arguments!
for k = 1:numel(theta)
ProjectileMotion(x0,y0,v0,theta(k));
end
madhan ravi
2018년 10월 25일
x0=0
theta_min=deg2rad(10);
theta_max=deg2rad(60);
y0=2;
v0=3;
theta=angle1(theta_min,theta_max)
for i=1:length(theta)
ProjectileMotion1(x0,y0,v0,theta(i));
end
function theta=angle1(theta_min,theta_max)
theta=linspace(theta_min,theta_max,10);
end
function [h_max,range,max_d]=ProjectileMotion1(x0,y0,v0,theta)
g=9.81;
a=1/2*g/(v0*cos(theta))^2;
b=-tan(theta);
c=tan(theta)*x0-y0-g/2/(v0*cos(theta))^2*x0^2;
x_max=(-b+sqrt(b^2-4*a*c))/2/a;
t_max=(x_max-x0)/(v0*cos(theta));
t=linspace(0,t_max,1000);
x=x0+v0*cos(theta).*t;
y=y0+v0*sin(theta).*t-1/2*g*t.^2;
h_max=max(y);
max_d=max(x);
range=max_d-x0;
hold on
plot(x,y)
hold off
fprintf('The possible maximum height is %.3f meters\n',h_max)
fprintf('The possible range is %.3f meters\n',range)
end
댓글 수: 2
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!