Visualizing functions of 2 variables:
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I wish to perform a 3D graph using surf:
How use the following 8 by 3 matrix as my input values for a and b:
2.12345 2.12345 2.12345 % row 1
2.12345 2.12345 3.12345 % row 2
2.12345 3.12345 2.12345 % row 3
2.12345 3.12345 3.12345 % row 4
3.12345 2.12345 2.12345 % row 5
3.12345 2.12345 3.12345 % row 6
3.12345 3.12345 2.12345 % row 7
3.12345 3.12345 3.12345 % row 8
My function is as follows:
function my_func = z(~,~)
x = (1+sin(5*a)-(1/sqrt(8*(sin(a)))))
y = (1+sin(5*b)+(1/sqrt(8*(sin(b)))))
z = x+y+x
Please help me to add a loop into my function, so that the first loop will accept the first row as input and so on. Also help me to correct the function, my intended input and output are:
Sample input:
2.12345 2.12345 2.12345 % row 1
Sample output based on row 1 input:
x = (1+sin(5*2.12345)-(1/sqrt(8*(sin(2.12345)))))
y = (1+sin(5*2.12345)+(1/sqrt(8*(sin(2.12345)))))
x = (1+sin(5*2.12345)-(1/sqrt(8*(sin(2.12345)))))
z = x+y+x
To plot the 3D graph:
% what should I put here as x and y values here?
surf(x,y,z)
anything else should I add to plot the 3D graph ?
2 Kommentare
Walter Roberson
am 15 Okt. 2012
You have 3 input values per row. How do you want to covert those three values into the two variables "a" and "b" ?
Your function shows you calculating x then y then x+y+x . Is that the same as (2*x)+y ? I ask because your sample output shows three outputs, x then y then x, after which x+y+x becomes suggestive that perhaps you failed to show a line of calculation and also accidentally named two variables "x".
Akzeptierte Antwort
Björn
am 15 Okt. 2012
Bearbeitet: Björn
am 16 Okt. 2012
It is not completely clear to me what you want, but if I interpret your question correctly you want to first x to use the first column of your matrix for a. The y-equation you want to use the second column of the matrix, and the second x-equation has to use the third column.
Nevertheless, in that case, z depends on three variables. What I suggest is to redefine x into
x = 1+sin(5*a1)-(1/sqrt(8*(sin(a1)))) + 1+sin(5*a2)-(1/sqrt(8*(sin(a2))))
where a1 is the first column and a2 the third column.
Now about the function. First of all I am curious how you got the function to work. Normally when creating a function you start the code with:
function z = my_func(a,b)
Anyway, with the new x-formula and taking all the possible combinations of a1, b and a2 values, the function should look something like:
function z = my_func(a,b)
a1 = reshape(a,[],1); % Create column-vector of a
a2 = a;
x1 = 1+sin(5*a1)-(1./sqrt(8*(sin(a1)))); % Create first x-vector
x2 = 1+sin(5*a2)-(1./sqrt(8*(sin(a2)))); % Create second x-vector
x = reshape(bsxfun(@plus,x1,x2),[],1); % Create combined x-vector
y = 1+sin(5*b)-(1./sqrt(8*(sin(b)))); % Create the y-vector
z = bsxfun(@plus,x,y);
[x1,y1]=ndgrid(x,y);
% This makes the array of the z-values of all the combinations of x1, x2, and y
surf(x1,y1,z);
end
This should do the trick. a and b are now vectors only containing all possible values once: a=[2.12345,3.12345], b=[2.12345,3.12345].
Hope this is what you are looking for. Try to be a little more clear in what you want in future questions. It makes it easier for others to answer.
3 Kommentare
Matt J
am 16 Okt. 2012
x1=repmat(x,1,length(y));
y1=repmat(y,length(x),1);
I suspect that the above 2 lines would be more optimally implemented as
[x1,y1] = ndgrid(x,y);
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!