How to assign properly global variables
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (2)
Philippe Girard
el 22 de En. de 2017
Image Analyst
el 22 de En. de 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));
Categorías
Más información sobre Whos en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!