surf doesnt work when plot 3d non continuous function

9 Ansichten (letzte 30 Tage)
xueqi
xueqi am 14 Jan. 2013
Kommentiert: Walter Roberson am 30 Okt. 2017
Hi fellows,
I have wrote a function cara, as you seen nextly in the code that it is a non-continuous one.
function [ eu ] = cara( lb1,lb2,lb3,c1,c2 )
d1=[1.2,0.8,0.9];
d2=[1.3,1.5,0.4];
tempeu=0;
r=0.08;
w1=c1*d1(1,1)+c2*d2(1,1)
w2=c1*d1(1,2)+c2*d2(1,2)
w3=c1*d1(1,3)+c2*d2(1,3)
if (w1==min([w1;w2;w3]))
tempeu=-(1-lb2-lb3)*exp(r*((d1(1,1)-1)*c1+(d2(1,1)-1)*c2))-...
p2*exp(r*((d1(1,2)-1)*c1+(d2(1,2)-1)*c2))-...
p3*exp(r*((d1(1,3)-1)*c1+(d2(1,3)-1)*c2));
elseif (w2==min([w1;w2;w3]))
tempeu=-lb1*exp(r*((d1(1,1)-1)*c1+(d2(1,1)-1)*c2))-...
(1-lb1-lb3)*exp(r*((d1(1,2)-1)*c1+(d2(1,2)-1)*c2))-...
lb3*exp(r*((d1(1,3)-1)*c1+(d2(1,3)-1)*c2));
elseif (w3==min([w1;w2;w3]))
tempeu=-lb1*exp(r*((d1(1,1)-1)*c1+(d2(1,1)-1)*c2))-...
lb2*exp(r*((d1(1,2)-1)*c1+(d2(1,2)-1)*c2))-...
(1-lb1-lb3)*exp(r*((d1(1,3)-1)*c1+(d2(1,3)-1)*c2));
end
eu=tempeu;
end
And then I want to use surf to plot function and my code is
x1 = linspace(-100,100,RESOLUTION);
y1 = linspace(-200,200,RESOLUTION);
[c1,c2] = meshgrid(x1,y1);
eu = cara(0.3,0.3,0.3,x1,y1);
%eu=arrayfun(@(x)f(x),x(1:end));
y1=surfc(c1,c2,eu);
y1=surfc(c1,c2,eu);
Then there is an error message saying that "The surface Z must contain more than one row or column.". I dont really understand this and also don't know how to solve this problem. Is there anyone could help me with this?
Really appreciated!
  4 Kommentare
Matt J
Matt J am 14 Jan. 2013
but then there is another error saying that Error using == Matrix dimensions must agree.
And so they should!
xueqi
xueqi am 14 Jan. 2013
yes! But how can I solve this problem? It seems for plotting, I have to write c1 and c2 as meshgrid(x1,y1). But then when the function generating the output it can't treat c1 and c2 just as variable but as matrixes.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 14 Jan. 2013
You're calling surfc with data eu that isn't the same size as c1 and c2.
  1 Kommentar
Matt J
Matt J am 14 Jan. 2013
will produce output the same size as c1 and c2 when rewritten as follows. Notice how IF...ELSE is not what you want here. Rather, you need to index appropriately.
function [ eu ] = cara( lb1,lb2,lb3,c1,c2 )
d1=[1.2,0.8,0.9];
d2=[1.3,1.5,0.4];
r=0.08;
w1=c1*d1(1)+c2*d2(1);
w2=c1*d1(2)+c2*d2(2);
w3=c1*d1(3)+c2*d2(3);
idx1=w1<=w2 & w1<=w3;
idx2=w2<=w1 & w2<=w3;
idx3=w3<=w2 & w3<=w1;
d1=d1-1;d2=d2-1;
E1=exp(r* (c1*d1(1)+c2*d2(1)) );
E2=exp(r* (c1*d1(2)+c2*d2(2)) );
E3=exp(r* (c1*d1(3)+c2*d2(3)) );
eu=zeros(size(c1));
eu(idx1)=-(1-lb2-lb3)*E1(idx1)-p2*E2(idx1)-p3*E3(idx1);
eu(idx2)=-lb1*E1(idx2)- (1-lb1-lb3)*E2(idx2) - lb3*E3(idx2);
eu(idx3)=-lb1*E1(idx3) -lb2*E2(idx3) - (1-lb1-lb3)*E3(idx3);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Gagandeep Sharma
Gagandeep Sharma am 30 Okt. 2017
Bearbeitet: Walter Roberson am 30 Okt. 2017
I also got same error in following problem:
clear
[x,y] = meshgrid(-100:100,-100:100);
D = 2;
for i=1:D
f1 = sum(10.^6)*(i-1/D-1)*(x(i).^2);
end
figure
meshc(x,y,f1)
title('High Conditioned Elliptic Function')
Please help in this regard
  1 Kommentar
Walter Roberson
Walter Roberson am 30 Okt. 2017
After
[x,y] = meshgrid(-100:100,-100:100);
then x and y are each 201 x 201.
D = 2;
for i=1:D
okay, you are looping twice
f1 = sum(10.^6)*(i-1/D-1)*(x(i).^2);
end
and whatever result is produced by the right-hand side of the assignment will overwrite all of f1, since f1 on the left is not indexed and is not used on the right. For example we can see that this is not the case where the values are being totaled over all of the iterations: that would require a structure similar to
total = 0;
for ...
total = total + ....
end
where the variable gets updated with new information.
Because all of f1 is being overwritten, then the effect is as-if you had done only the last iteration, when i = D
Now let us look at what is being calculated on that iteration:
sum(10.^6)*(i-1/D-1)*(x(i).^2)
the 10.^6 part is a scalar constant, and sum() of a scalar constant is just that same constant, same as if the sum() was not present. For the (i-1/D-1) part, both i and D are scalars, so the result is going to be a scalar. For the (x(i).^2) part, x(i) is a scalar, so (x(i).^2) is a scalar. So now we have three scalars being multiplied together, which is going to give a scalar as the result. We can see, then that after the "for" loop, that f1 will be a scalar.
After the loop, you have
meshc(x,y,f1)
As examined earlier, x and y are each 201 x 201, so the first two arguments to that are 201 x 201. The third argument, f1, we just determined to be a scalar. It is not valid to try to construct the mesh of a scalar with respect to a 201 x 201 array of locations.
Unfortunately my guesses about what you might have intended by the code are not going well. The best I have come up with is that perhaps you are intending to construct a vector of values separated by 1/D corresponding to the y values, and multiply that by the square of the x values.... but even that does not explain the sum() or the 10^6.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by