Hi,
I'm trying to combine 3 graphics into a single one . First, here is my program
function graphique()
global x h f_vec i
h_vec=[0.015625,0.1,0.5];
x=linspace(0.1,2);
for i=1:3
h=h_vec(i);
f = F();
end
plot(x,f_vec)
end
function f=F()
global h x f_vec i
f= 1./(((1-x.^2).^2+h.*x.^2).^(1/2));
f_vec(i)=f;
end
I was expecting that matlab will starts with the first iteration with i=1, associate a value h=0.015625 and call the function f=F(). He calculates f into the second block with the big equation and associate all the values of f to f_vec(1) then come back to the loop "for" with i=2... After doing all 3 iterations, he would get out of the loop and plot a vector f_vec in function of x. But I get an error with f=F() because it seems like having this variable into another function his not for him consider as using it.
Anyway the goal is to get a single window combining 3 graphs. I need help this program was mostly done by a user here and I'm still unable to finish it. Is there a way to use
f_vec
as a storage for all values of the 3 iterations.

 채택된 답변

Image Analyst
Image Analyst 2017년 1월 22일

1 개 추천

Why can't you just take the two lines from your function and put it into the loop.
function graphique()
global x h f_vec i
h_vec=[0.015625,0.1,0.5];
x=linspace(0.1, 2, length(j));
for i=1:3
h=h_vec(i);
f= 1./(((1-x.^2).^2+h.*x.^2).^(1/2));
f_vec(i)=f;
end
plot(x,f_vec)
end
Get rid of the F() function definition. Then you'll have just a single function that does everything your two functions used to do. Be sure to fix linspace() like I did so that it has the same number of elements as h.

댓글 수: 4

Philippe Girard
Philippe Girard 2017년 1월 22일
편집: Philippe Girard 2017년 1월 22일
Because it's a school exercise and we must use global variables. This is not part of a program this is the whole program, the way you wrote it I don't even need to use global variables. So there is 2 criteria; I linked the question, because it's in french here is what it means in english
Here is the function (fonction) where lambda is a real parameter a) for a given lambda write a matlab function which as for only input argument x and that will calculates f(x). Make sure that your function is able to receive vectors as input argument. Use "global" command of matlab to transfert the value of lambda parameter to your function
b) By using the previous function developed at a), trace for each value of lambda= 0,015625 (...), the 3 graph of the function f(x) on the interval [0,1,2]. Identify clearly the 3 curves.
The final result must contains: the MATLAB program develop at the question a) and the graph produced by this program at b)
You don't need all those variables as globals. In fact you have too many confusing variables. It looks like the program is asking you to plot 3 values of x for 3 different values of lambda. So that would simply be this:
function test3
global lambda
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
lambda = [0.015625,0.1,0.5]; % Define our lambda
x = 0 : 1 : 2; % Define our x
graphique(x); % Plot f(x) for all lambdas.
end
function graphique(x)
global lambda
for k = 1 : length(lambda)
f = 1 ./ (((1-x.^2).^2 + lambda(k).*x.^2) .^ (1/2));
plot(x, f, '-d', 'LineWidth', 2, 'MarkerSize', 14);
hold on;
end
grid on;
end
The code will actually work for any length of lambda array.
This is exactly the result that I needed:
function test7
lambda=[0.015625,0.1,0.5];
x=linspace(0.1,2);
equation(x,lambda);
end
function equation(x,lambda)
for k=1:3
f = 1 ./ (((1-x.^2).^2 + lambda(k).*x.^2) .^ (1/2));
plot(x,f);
hold on
end
grid on
end
I didn't use global in this case because it was just to verify that I correctly understand how function works.
So this block
lambda=[0.015625,0.1,0.5];
x=linspace(0.1,2);
equation(x,lambda);
is sending information for further in the code. I thought it was the opposite that he asked for informations.
Now I only need to add labels and all that stuff. Your code learned me a ton of things thank you!
Image Analyst
Image Analyst 2017년 1월 22일
But you said using global was a required part of the solution. Like they wanted/required you to use it. Anyway, thanks for accepting.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2017년 1월 22일

댓글:

2017년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by