Azzera filtri
Azzera filtri

Visualizing functions of 2 variables:

1 visualizzazione (ultimi 30 giorni)
Enfa White
Enfa White il 15 Ott 2012
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 Commenti
Walter Roberson
Walter Roberson il 15 Ott 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".
Enfa White
Enfa White il 15 Ott 2012
Modificato: Enfa White il 15 Ott 2012
How to covert 3 values into the two variables "a" and "b" is my question, I need the help to modify my function such as to include a loop so that each row will be used once and produce 8 different values for z. x+y+x can not be simplified as (2*x)+y), because all the 8 rows will produce 8 different results for z. Except for x+y+x+y when the input is 2.12345 3.12345 2.12345 3.12345 because 2.12345 and 3.12345 are used twice each, which is the same as 2*2.12345 and 2*2.12345.
Earlier example showed 3 outputs, just to clearly present how I substitute value "a" and "b" from row 1 into equation "x" and "y" and then add up x+y+x for "z". Formula for z is to simply add up the sum of equation "x" and "y".

Accedi per commentare.

Risposta accettata

Björn
Björn il 15 Ott 2012
Modificato: Björn il 16 Ott 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 Commenti
Matt J
Matt J il 16 Ott 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);
Björn
Björn il 16 Ott 2012
Modificato: Björn il 16 Ott 2012
You are right Matt J. I did not know that function yet. Thanks. I changed it in the original answer.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by