# How to assign properly global variables

77 views (last 30 days)
Philippe Girard on 22 Jan 2017
Answered: Image Analyst on 22 Jan 2017
Hi,
I read a bunch of topics everywhere, and even videos on YouTube, but I still don't understand how to use global variables. My teacher is actually asking me to plot 3 graphics using the function you see below, with 3 different values of "h". I added "." into function because one of the criteria is that the function must accept vectors. I MUST USE global variables for this exercise.
I have this function:
F(x)=1./(((1-x.^2).^2+h.*x.^2).^(1/2));
x=linspace(0.1,2);
Now I need to associate 3 values to "h". In my case those values are:
h=[0.015625, 0.1, 0.5]
I won't put what I did here because this has no sense at all. So I would really appreciate someone who is able to give me an example.
At the end, I plotted the graphic this way:
plot(x,f(x));
Now when I want to use
function F(x)=[h]
global h
h=0.015625
end
This is where everything collapses. I'm getting confused of how to plug values in my function F(x), and I mix up everything.
By the way I apologize if my sentences might be hard to understand -- I'm not English.

John Chilleri on 22 Jan 2017
Edited: John Chilleri on 22 Jan 2017
Hello,
I provide a specific solution at the bottom after the explanation on global variables.
Global variables are a way to have variables accessible across multiple functions!
Try this:
function a = fcn1()
a = 5;
b = 6;
fcn2()
end
function a = fcn2()
a = b;
end
Notice that it gives you an error because "b" is an undefined function or variable.
This is because the variable "b" was a variable that only existed in function fcn1(). It was a local variable, and its scope was only within fcn1(). If you want variables to have a global scope, so that they can be accessed anywhere, you can declare them as global variables.
This is how you can declare a and b as global variables:
global a b
Now try this:
function a = fcn1()
global b
a = 5;
b = 6;
fcn2()
end
function a = fcn2()
global b
a = b;
end
It should run smoothly, and fcn2 will output a = 6. This is because b is a global variable accessible everywhere it has been declared. Further and more detailed information on global variables can be found here.
It's important to note that you must write
global var1 var2 var3 etc
in each function that you wish to have access to these global variables.
With respect to your function, it has improper syntax, it should look something like this:
function outputs = function_name(inputs)
In your case, the following code might provide the solution you were seeking, please read carefully over it to understand how the global allows the variables to be accessed even though they weren't passed into the function as inputs:
function plot_dif_h_values()
global x h
figure(1)
hold on
x = linspace(0.1,2);
hvec = [0.015625, 0.1, 0.5];
for i = 1:3
h = hvec(i);
fx = myF();
plot(x,fx)
end
end
function fx = myF()
global x h
fx = 1./(((1-x.^2).^2+h.*x.^2).^(1/2));
end
I'm not exactly sure what you're looking for, but the above code is how I interpreted your question. Please ask any questions or correct me if I misinterpreted the question!
Hope this helps!
Philippe Girard on 22 Jan 2017
Pretty cool that you can associate many variables to a single one and call that one only each time. If someday I need it I will have to figure out how to do a cluster able to combine string and numbers.
Philippe Girard on 22 Jan 2017
Thank you John. It's more clear I tried to do it without looking at your solution and I was able to do it. As you can see I'm not a programmer ;). I'm still trying to combine the 3 functions into one graph if I'm not able within the next hour I will search for some help at school! Thanks for your time!

Philippe Girard on 22 Jan 2017
All right now I exactly know what I misunderstand: when the MATLAB reads the
fx=myF();
into the fist block what he's doing? myF() itself has no value linked to it so does he search for a myF() further or a big block fx=myF()?
I tried your program and it's mostly the right thing (thank you again) only that they all have to be into the same window and not 3 different window but I will work on that by myself!

Image Analyst on 22 Jan 2017
You will need to have x be the same size as h if you are doing an element-by-element multiplication. So you need to pass in all 3 arguments to linspace(). And you need to define x before F, and don't use parentheses around x when you define F:
global h;
x = linspace(0.1, 2, length(h)); % x has same number of elements as h
Fx = 1 ./ (((1-x.^2).^2 + h.*x.^2) .^ (1/2));

### Categories

Find more on Historical Contests in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!