How to assign properly global variables
155 次查看(过去 30 天)
显示 更早的评论
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.
Could you please help me?
By the way I apologize if my sentences might be hard to understand -- I'm not English.
0 个评论
采纳的回答
John Chilleri
2017-1-22
编辑:John Chilleri
2017-1-22
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!
更多回答(2 个)
Image Analyst
2017-1-22
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));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!