How to use a variable only defined in a function in your script?

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 Comments

first upload your code to debug
Here is a part of it:
y0=input('Enter the initial vertical position:');
v0=input('Enter the initial velocity:');
angle(theta_min,theta_max,theta)
for i=1:length(theta)
ProjectileMotion(x0,y0,v0,theta(i));
end
The first function is:
function t=angle(theta_min,theta_max,theta)
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
and projectileMotion is:
function [h_max,range,max_d]=ProjectileMotion(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
"Would making the variable global fix this..."
No, global variables do not "fix" anything, they invariably make things worse:
Alright thank you. Do you know what's wrong with my code?
don't use angle as your function name it may contradict which inbuilt function name which returns phase angle!

Sign in to comment.

Answers (3)

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 Comments

I am not sure that I understand what you mean.
Here is a part of my code:
y0=input('Enter the initial vertical position:');
v0=input('Enter the initial velocity:');
angle(theta_min,theta_max,theta)
for i=1:length(theta)
ProjectileMotion(x0,y0,v0,theta(i));
end
and here is the "angle" function:
function t=angle(theta_min,theta_max,theta)
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
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.

Sign in to comment.

Your usage of the function angle has several bugs, in particular:
  1. You do not call angle with any output arguments, although you need it later in your code.
  2. You define three input arguments to angle, but ignore all of them inside the function.
  3. 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 Comments

I know that I don't need to create a function for the angle but I use it multiple times through out my code and I felt like a function would make it look better. I also have other variables that I use multiple times therefore I wanted to create a function for each one.
As for ProjectileMotion I did not ignore the output arguments, I did not upload it because it works. I only had a problem with other functions.
I'm not sure I understand the function you gave me, it doesn't ask the user for the inputs.
"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

Sign in to comment.

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 Comments

I assigned values of input to save time but you can change it to input

Sign in to comment.

Categories

Asked:

on 25 Oct 2018

Commented:

on 25 Oct 2018

Community Treasure Hunt

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

Start Hunting!