How to use a variable only defined in a function in your script?
23 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
madhan ravi
le 25 Oct 2018
don't use angle as your function name it may contradict which inbuilt function name which returns phase angle!
Réponses (3)
Image Analyst
le 25 Oct 2018
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 commentaires
Image Analyst
le 25 Oct 2018
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
le 25 Oct 2018
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 commentaires
Stephen23
le 25 Oct 2018
Modifié(e) : Stephen23
le 25 Oct 2018
"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
le 25 Oct 2018
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 commentaires
Voir également
Catégories
En savoir plus sur Build MATLAB Interface to C++ Library dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!