Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Non-rectangular 3D plot

Subject: Non-rectangular 3D plot

From: Bookie

Date: 22 Apr, 2008 15:03:57

Message: 1 of 16

Hi,

  I was wondering how I can make a 3D plot using non-rectandular data for x
and y. My x-y domain can be described with limits: a <= x <= b AND 0 <= y <=
m*x + b. Then z, forming the surface of the plot, will be a function of x
and y. So the shadow of the plot will not look like a rectangle but some
other 4-sided polygon. Thanks for you help in advance!

Bookie

Subject: Non-rectangular 3D plot

From: alessandro mura

Date: 22 Apr, 2008 20:25:55

Message: 2 of 16


> I was wondering how I can make a 3D plot using non-rectandular data for x
> and y. My x-y domain can be described with limits: a <= x <= b AND 0 <= y
> <=
> m*x + b. Then z, forming the surface of the plot, will be a function of x
> and y. So the shadow of the plot will not look like a rectangle but some
> other 4-sided polygon. Thanks for you help in advance!
>
> Bookie
>


If triangles are OK, istead of 4-sided polygons, then:


x=randn(1,100)
y=randn(1,100);
z=x.^2-y.^2;
tri=delaunay(x,y);
hold on
for i=1:size(tri,1)
patch([x(tri(i,1)) x(tri(i,2)) x(tri(i,3))], [y(tri(i,1)) y(tri(i,2))
y(tri(i,3))],[z(tri(i,1)) z(tri(i,2)) z(tri(i,3))],'g')
end


--
Alessandro Mura
Istituto Nazionale di Astrofisica - IFSI
http://pptt4.ifsi-roma.inaf.it/~mura/index.html

Subject: Non-rectangular 3D plot

From: Bookie via MathKB.com

Date: 22 Apr, 2008 22:58:27

Message: 3 of 16

alessandro mura wrote:
>> I was wondering how I can make a 3D plot using non-rectandular data for x
>> and y. My x-y domain can be described with limits: a <= x <= b AND 0 <= y
>[quoted text clipped - 4 lines]
>>
>> Bookie
>
>If triangles are OK, istead of 4-sided polygons, then:
>
>x=randn(1,100)
>y=randn(1,100);
>z=x.^2-y.^2;
>tri=delaunay(x,y);
>hold on
>for i=1:size(tri,1)
>patch([x(tri(i,1)) x(tri(i,2)) x(tri(i,3))], [y(tri(i,1)) y(tri(i,2))
>y(tri(i,3))],[z(tri(i,1)) z(tri(i,2)) z(tri(i,3))],'g')
>end
>

Thanks Alessandro,

   So from what I get this code selects 100 random x-y points and calculatees
z for each point. I have read into Delaunay triangulation, and I am not sure
how to use it. I do not know what the code does in the 'patch' function. As
well I don't see where I would enter my specific limits for x and y.

Right now my points in the x-y domain lie within a 4-sided polygon, which I
could break into a 1 rectangle and 1 triangle. If I am using delaunay
triangulation do I need to split my 4-sided polygon into 2 or more triangles
only? Does some part of your code represent the verticies of the triangle?

Also my x and y coordinates are described by limits: a <= x <= b AND 0 <= y
<= m*x+b. I can make my x be a vector: x = a:1:b, but I do not know how to
make y be a vector. I would want: y = 0:1:(m*x+b), which doesn't work of
course. Or I could just say: y = min:1:max, but then I'm not incorporating
the relation between the y values and x values.

Lastly, my z function is calling an m-file function: z = functionname(x,y).
So I can't use the dot operator to calculate the z-matrix values. I then
want to plot z. I could use surf, mesh, triplot, etc. I'm not sure which. I
do not want z-values for my x-y coordinates outside of my polygon region, so
maybe I want holes in my surface plot. I am wondering if I can return 'NaN',
for (x,y) coordinates outside of my region.

I appreciate your help and the help of this forum. Whatever suggestions you
and anyone else has would be awesome!

Regards,
Bookie

--
Message posted via MathKB.com
http://www.mathkb.com/Uwe/Forums.aspx/matlab/200804/1

Subject: Non-rectangular 3D plot

From: Roger Stafford

Date: 23 Apr, 2008 01:29:02

Message: 4 of 16

"Bookie" <u43093@uwe> wrote in message <831044a106ed5@uwe>...
> Hi,
>
> I was wondering how I can make a 3D plot using non-rectandular data for
x
> and y. My x-y domain can be described with limits: a <= x <= b AND 0
<= y <=
> m*x + b. Then z, forming the surface of the plot, will be a function of x
> and y. So the shadow of the plot will not look like a rectangle but some
> other 4-sided polygon. Thanks for you help in advance!
>
> Bookie
----------
  In the 'surf' and 'surfc' functions it isn't necessary that the x-y domain
covered be a rectangle. It is only necessary that there be a two-parameter
representation of the desired surface in which the parameters fill a
rectangular space. In your example, you could use parameters, ix and iy
where

 [ix,iy] = meshgrid(1:p,1:q); % Use a p by q mesh of ix & iy values
 x = a + (ix-1)*(b-a)/(p-1); % a <= x <= b
 y = (iy-1).*(m*x+b)/(q-1); % 0 <= y <= m*x+b
 z = (Whatever your function of x and y is)
 surf(x,y,z)

  There is an example of a sphere generated by 'surf' in the function reference
to matlab. Study it to see how the parameterization works. In particular note
that what you call the "shadow" is a circle in this case.

Roger Stafford

Subject: Non-rectangular 3D plot

From: Scott Burnside

Date: 23 Apr, 2008 04:32:01

Message: 5 of 16

<snip>
> Lastly, my z function is calling an m-file function: z =
functionname(x,y).
> So I can't use the dot operator to calculate the z-matrix
values. I then
> want to plot z. I could use surf, mesh, triplot, etc.
I'm not sure which. I
> do not want z-values for my x-y coordinates outside of my
polygon region, so
> maybe I want holes in my surface plot. I am wondering if
I can return 'NaN',
> for (x,y) coordinates outside of my region.
>
> I appreciate your help and the help of this forum.
Whatever suggestions you
> and anyone else has would be awesome!
>
> Regards,
> Bookie

You can set values outside your region to NaN:

Z = rand(150,150);
surf(Z);
view([35,35])
axis vis3d
shading interp
lighting phong
% omit data outside of radius 25
x = 1:150;
y = 1:150;
[X Y] = meshgrid(x,y);
hold on
roy = zeros(150,150);
for i = 1:(150^2)
   roy(i) = ((X(i)-75).^2) + ((Y(i)-75).^2);
   if roy(i) > (50^2)
       Z(i) = NaN;
   end
end
figure;
surf(Z);
view([35,35])
axis vis3d
shading interp
lighting phong


hth,
Scott

Subject: Non-rectangular 3D plot

From: alessandro mura

Date: 23 Apr, 2008 11:42:36

Message: 6 of 16

Hi, few more info:

patch is just a low-level function to draw surfaces.
In general, a n-sided polygon (n>3) in space is usually not on a plane.
Hence, matlab draws 2 or more triangles instead. For example:
patch([0 0 1 1],[0 1 1 0],[0 1 0 1],'g')

Usually we you use surf you don't appreciate the differnce,
but all that small rectangles or quadrilaterals
are, usually, couples of triangles:

surf(rand(3))

Delaunay gives, from a non-rectangular grid of points
in 2d, a (non unique) set of triangles. See help delaunay.
So I've given you the solution for triangles and not for
quadrilaterals. I don't know if there is a similar method for n-polygons?

Ale
--
Alessandro Mura
Istituto Nazionale di Astrofisica - IFSI
http://pptt4.ifsi-roma.inaf.it/~mura/index.html

Subject: Non-rectangular 3D plot

From: Bookie via MathKB.com

Date: 23 Apr, 2008 12:47:33

Message: 7 of 16

Thanks Roger, Alessandro & Scott! I'm going to try these things out.

Well wishes,
Bookie

Roger Stafford wrote:
>> Hi,
>>
>[quoted text clipped - 5 lines]
>>
>> Bookie
>----------
> In the 'surf' and 'surfc' functions it isn't necessary that the x-y domain
>covered be a rectangle. It is only necessary that there be a two-parameter
>representation of the desired surface in which the parameters fill a
>rectangular space. In your example, you could use parameters, ix and iy
>where
>
> [ix,iy] = meshgrid(1:p,1:q); % Use a p by q mesh of ix & iy values
> x = a + (ix-1)*(b-a)/(p-1); % a <= x <= b
> y = (iy-1).*(m*x+b)/(q-1); % 0 <= y <= m*x+b
> z = (Whatever your function of x and y is)
> surf(x,y,z)
>
> There is an example of a sphere generated by 'surf' in the function reference
>to matlab. Study it to see how the parameterization works. In particular note
>that what you call the "shadow" is a circle in this case.
>
>Roger Stafford

--
Message posted via http://www.mathkb.com

Subject: Non-rectangular 3D plot

From: Bookie via MathKB.com

Date: 23 Apr, 2008 19:36:28

Message: 8 of 16

Hi Roger,

  I'm trying the method you've shown,since it is the most familiar for me.
The tip on parameter representation helped! I've modified your code to work
with my case, please see below. I am wondering if I have applied my 'for
loop' properly to get x and y vectors and the z matrix. My results are not
what I am expecting, although much improved over before! Thanks again!

% Range variables
Xpts = a:1:b;
Ypts = min:1:max;
% Preallocation of matricies
m = numel(Xpts);
n = numel(Ypts);
Z = zeros(4,m,n); %3d matrix, a 4x1 vector calculated over changes in vectors:
x (size m) & y (size n)
x = zeros(m);
y = zeros(n);
[ix,iy] = meshgrid(1:m,1:n); % Use a p by q mesh of ix & iy values
% Loop to create z-matrix
for i = 1:m
    x(i) = min(X4) + (ix(i)-1)*(max(X4)-min(X4))/(m-1); % a <= x <= b
    for j = 1:n
        y(i,j) = (iy(j)-1).*(M*x(i)+b)/(n-1); % 0 <= y <= m*x+b
        Variable1(i,j) = x(i) + y(i,j);
        Variable2(i,j) = x(i) - y(i,j);
        [Z(:,i,j)] = FunctionMfile(Variable1(i,j),Variable2(i,j),x(i),y(i,j))
;
    end
end
[x,y] % check matrix
z(i,j) = Z(1,i,j); % plot of one layer of 3d matrix only, since 'surf' only
accepts z as a 2d matrix
surf(x,y,abs(Z))
colormap hsv
colorbar

Regards,
Bookie

Roger Stafford wrote:
>> Hi,
>>
>[quoted text clipped - 5 lines]
>>
>> Bookie
>----------
> In the 'surf' and 'surfc' functions it isn't necessary that the x-y domain
>covered be a rectangle. It is only necessary that there be a two-parameter
>representation of the desired surface in which the parameters fill a
>rectangular space. In your example, you could use parameters, ix and iy
>where
>
> [ix,iy] = meshgrid(1:p,1:q); % Use a p by q mesh of ix & iy values
> x = a + (ix-1)*(b-a)/(p-1); % a <= x <= b
> y = (iy-1).*(m*x+b)/(q-1); % 0 <= y <= m*x+b
> z = (Whatever your function of x and y is)
> surf(x,y,z)
>
> There is an example of a sphere generated by 'surf' in the function reference
>to matlab. Study it to see how the parameterization works. In particular note
>that what you call the "shadow" is a circle in this case.
>
>Roger Stafford

--
Message posted via MathKB.com
http://www.mathkb.com/Uwe/Forums.aspx/matlab/200804/1

Subject: Non-rectangular 3D plot

From: Bookie via MathKB.com

Date: 23 Apr, 2008 19:48:29

Message: 9 of 16

Hi, sorry here's the code again, some of my variable names weren't consistent.
This is only my simplified version. Thanks!

% Range variables
Xpts = a:1:b;
Ypts = minY:1:maxY;
% Preallocation of matricies
m = numel(Xpts);
n = numel(Ypts);
Z = zeros(4,m,n); %3d matrix, a 4x1 vector calculated over changes in vectors:

x (size m) & y (size n)
x = zeros(m);
y = zeros(n);
[ix,iy] = meshgrid(1:m,1:n); % Use a p by q mesh of ix & iy values
% Loop to create z-matrix
for i = 1:m
   x(i) = a + (ix(i)-1)*(a-b))/(m-1); % a <= x <= b
   for j = 1:n
       y(i,j) = (iy(j)-1)*(M*x(i)+B)/(n-1); % 0 <= y <= m*x+b
       Variable1(i,j) = x(i) + y(i,j);
       Variable2(i,j) = x(i) - y(i,j);
       [Z(:,i,j)] = FunctionMfile(Variable1(i,j),Variable2(i,j),x(i),y(i,j));
   end
end
[x,y] % check matrix
z(i,j) = Z(1,i,j); % plot of one layer of 3d matrix only, since 'surf' only
accepts z as a 2d matrix
surf(x,y,abs(Z))
colormap hsv
colorbar

Bookie wrote:
>Hi Roger,
>
> I'm trying the method you've shown,since it is the most familiar for me.
>The tip on parameter representation helped! I've modified your code to work
>with my case, please see below. I am wondering if I have applied my 'for
>loop' properly to get x and y vectors and the z matrix. My results are not
>what I am expecting, although much improved over before! Thanks again!
>
>% Range variables
>Xpts = a:1:b;
>Ypts = min:1:max;
>% Preallocation of matricies
>m = numel(Xpts);
>n = numel(Ypts);
>Z = zeros(4,m,n); %3d matrix, a 4x1 vector calculated over changes in vectors:
>x (size m) & y (size n)
>x = zeros(m);
>y = zeros(n);
>[ix,iy] = meshgrid(1:m,1:n); % Use a p by q mesh of ix & iy values
>% Loop to create z-matrix
>for i = 1:m
> x(i) = min(X4) + (ix(i)-1)*(max(X4)-min(X4))/(m-1); % a <= x <= b
> for j = 1:n
> y(i,j) = (iy(j)-1).*(M*x(i)+b)/(n-1); % 0 <= y <= m*x+b
> Variable1(i,j) = x(i) + y(i,j);
> Variable2(i,j) = x(i) - y(i,j);
> [Z(:,i,j)] = FunctionMfile(Variable1(i,j),Variable2(i,j),x(i),y(i,j))
>;
> end
>end
>[x,y] % check matrix
>z(i,j) = Z(1,i,j); % plot of one layer of 3d matrix only, since 'surf' only
>accepts z as a 2d matrix
>surf(x,y,abs(Z))
>colormap hsv
>colorbar
>
>Regards,
>Bookie
>
>>> Hi,
>>>
>[quoted text clipped - 19 lines]
>>
>>Roger Stafford

--
Message posted via MathKB.com
http://www.mathkb.com/Uwe/Forums.aspx/matlab/200804/1

Subject: Non-rectangular 3D plot

From: Roger Stafford

Date: 23 Apr, 2008 22:36:02

Message: 10 of 16

"Bookie via MathKB.com" <u43093@uwe> wrote in message
<831f53267a850@uwe>...
> Hi, sorry here's the code again, some of my variable names weren't
consistent.
> This is only my simplified version. Thanks!
>
> % Range variables
> Xpts = a:1:b;
> Ypts = minY:1:maxY;
> % Preallocation of matricies
> m = numel(Xpts);
> n = numel(Ypts);
> Z = zeros(4,m,n); %3d matrix, a 4x1 vector calculated over changes in
vectors:
>
> x (size m) & y (size n)
> x = zeros(m);
> y = zeros(n);
> [ix,iy] = meshgrid(1:m,1:n); % Use a p by q mesh of ix & iy values
> % Loop to create z-matrix
> for i = 1:m
> x(i) = a + (ix(i)-1)*(a-b))/(m-1); % a <= x <= b
> for j = 1:n
> y(i,j) = (iy(j)-1)*(M*x(i)+B)/(n-1); % 0 <= y <= m*x+b
> Variable1(i,j) = x(i) + y(i,j);
> Variable2(i,j) = x(i) - y(i,j);
> [Z(:,i,j)] = FunctionMfile(Variable1(i,j),Variable2(i,j),x(i),y(i,j));
> end
> end
> [x,y] % check matrix
> z(i,j) = Z(1,i,j); % plot of one layer of 3d matrix only, since 'surf' only
> accepts z as a 2d matrix
> surf(x,y,abs(Z))
> colormap hsv
> colorbar
------------
  You have me completely confused by your code. I have all kinds of
questions about it.
'
1. In Xpts = a:1:b and Ypts = minY:1:maxY you are restricting yourself to
steps of 1 in obtaining m and n (which I called p and q.) You ought to be able
to independently decide how many points you want in each direction.

2. In Z = zeros(4,m,n) I have no idea why you are constructing a three-
dimensional array for Z. What purpose does it serve?

3. In x = zeros(m) and y = zeros(n), why are you allocating m x m and n x n
arrays for x and y here? They should each be m x n (p x q). In any case, they
don't need to be pre-allocated. (See 4.)

4. There is no need to generate x and y in the for-loop you have here. The
two lines

 x = a + (ix-1)*(b-a)/(p-1)
 y = (iy-1).*(m*x+b)/(q-1)

do that in a vectorized manner with just the two given lines. Both x and y will
be of p x q size.

5. As far as I can make out, your 'FunctionMfile' function takes four
arguments: x+y,x-y,x,y. Why is that? Having four to express a function of
only two variables would appear to be redundant. However, accepting that,
why can you not just say:

 z = FunctionMfile(x+y,x-y,x,y);

if 'FunctionMfile' is able to accept arrays as arguments. If not, then you could
simply write:

 z = zeros(p,q);
 for ii = 1:p
  for jj = 1:q
   z(ii,jj) = FunctionMfile(x(ii,jj)+y(ii,jj), ...
               x(ii,jj)-y(ii,jj),x(ii,jj),y(ii,jj));
  end
 end

without including x and y calculations in the loop. If 'FunctionMfile' insists on
outputting a 4-element vector, just use the first one, as in t = FunctionMfile
(...); z(ii,jj) = t(1);

6. I'm not sure 'surf' is able to properly handle x as a vector input and y as a
matrix, as you appear to have done here.

  Somehow I don't think you have understood the nature of the x and y
matrices generated by those two lines I gave you in 4. above. They are the
type of input that 'surf' requires. As each of their two parametric indices vary
from 1 to p and 1 to q, their values as coordinate x-y pairs neatly fill up the
contents of the trapezoid shape that constitutes the base (your "shadow") of
the surface you desire.

  You should be able to use these matrices directly to generate a like-sized
matrix z which is the appropriate function of x and y. If necessary, you could
create a modified version of 'FunctionMfile' which can do that. It's cleaner to
do it that way than to fool around with extraneous outputs and inputs in the
function. It will make your code easier to follow and modify later.

Roger Stafford

Subject: Non-rectangular 3D plot

From: Bookie via MathKB.com

Date: 24 Apr, 2008 04:07:53

Message: 11 of 16

Hi Roger,

  Ok I see where my code would get very confusing now. To answer the points
you made:

1. I understand now that my mesh decides how many points I have in each
direction.
2. I need a 3d matrix for z because my function outputs a 4x1 vector for z
and I am evaluating this vector over vector parameters x & y, which add the
m and n dimension to z. Its easier not to program my function to accept
arrays. I originally had it accepting arrays for my inputs, but I had to
apply 3d matricies to a long program verses applying it to a single for loop
when I call the function.
3. I misused the zero function. I had thought zero(m) made an mx1 vector. I
realize its makes an mxm matrix actually.
4. I have shown my new code below without x and y in the for loop. Please
see my query about this below.
5. I see the redundancy, but for now I want to see that this method works
before I modify the variables (that are functions of x & y) in my function m.
file that I am calling.
6. I do not necessarily need y to be a matrix. It can be a vector, but I need
to have y increment over a counter j for a given x value.

My new code:

m=21 % m & n may not always be equal!
n=21
z=zeros(4,m,n); % preallocate z matrix
[ix,iy] = meshgrid(1:m,1:n);
x = a + (ix-1)*(b-a)/(m-1); % a <= x <= b
y = (iy-1).*(M*x+B)/(n-1); % 0 <= y <= M*x+B
for i =1:m
    for j=1:n
         z(:,i,j) = FunctionMfile(x(i)+y(j),x(i)-y(j),x(i),y(j));
    end
end
Z(i,j) = z(1,i,j); % take only 1st layer of 3d matrix
surf(x,y,Z)

My question is that when I enter the j-loop, as y(j) goes from y(1) to y(n),
will iy (in the function for y) increment and x stays constant at x(i)? So
for i=1, in the equation for y: x=x(i) and as j=1 to n, iy(j) =1 to n.
Therefore y goes from (iy(1) - 1)*(M*x(i) +B) to (iy(n) -1)*(M*x(i)+B), where
x(i) does not change within the j-loop. Then the whole process repeats for a
new x(i+1) value.

It seems as if the x in the y-function may be incrementing as well within the
j-loop. Since x (and y) is not in the for loop how does the program know that
x is only a function of i and not j? Hence, y may be incrementing from (iy(1)-
1)*(M*x(1)+B) to (iy(n)-1)*(M*x(n)+B).

Thanks again for helping out a matlab novice!

Kind regards,
Bookie

Roger Stafford wrote:
>"Bookie via MathKB.com" <u43093@uwe> wrote in message
><831f53267a850@uwe>...
>> Hi, sorry here's the code again, some of my variable names weren't consistent.
>> This is only my simplified version. Thanks!
>[quoted text clipped - 27 lines]
>> colormap hsv
>> colorbar
>------------
> You have me completely confused by your code. I have all kinds of
>questions about it.
>'
>1. In Xpts = a:1:b and Ypts = minY:1:maxY you are restricting yourself to
>steps of 1 in obtaining m and n (which I called p and q.) You ought to be able
>to independently decide how many points you want in each direction.
>
>2. In Z = zeros(4,m,n) I have no idea why you are constructing a three-
>dimensional array for Z. What purpose does it serve?
>
>3. In x = zeros(m) and y = zeros(n), why are you allocating m x m and n x n
>arrays for x and y here? They should each be m x n (p x q). In any case, they
>don't need to be pre-allocated. (See 4.)
>
>4. There is no need to generate x and y in the for-loop you have here. The
>two lines
>
> x = a + (ix-1)*(b-a)/(p-1)
> y = (iy-1).*(m*x+b)/(q-1)
>
>do that in a vectorized manner with just the two given lines. Both x and y will
>be of p x q size.
>
>5. As far as I can make out, your 'FunctionMfile' function takes four
>arguments: x+y,x-y,x,y. Why is that? Having four to express a function of
>only two variables would appear to be redundant. However, accepting that,
>why can you not just say:
>
> z = FunctionMfile(x+y,x-y,x,y);
>
>if 'FunctionMfile' is able to accept arrays as arguments. If not, then you could
>simply write:
>
> z = zeros(p,q);
> for ii = 1:p
> for jj = 1:q
> z(ii,jj) = FunctionMfile(x(ii,jj)+y(ii,jj), ...
> x(ii,jj)-y(ii,jj),x(ii,jj),y(ii,jj));
> end
> end
>
>without including x and y calculations in the loop. If 'FunctionMfile' insists on
>outputting a 4-element vector, just use the first one, as in t = FunctionMfile
>(...); z(ii,jj) = t(1);
>
>6. I'm not sure 'surf' is able to properly handle x as a vector input and y as a
>matrix, as you appear to have done here.
>
> Somehow I don't think you have understood the nature of the x and y
>matrices generated by those two lines I gave you in 4. above. They are the
>type of input that 'surf' requires. As each of their two parametric indices vary
>from 1 to p and 1 to q, their values as coordinate x-y pairs neatly fill up the
>contents of the trapezoid shape that constitutes the base (your "shadow") of
>the surface you desire.
>
> You should be able to use these matrices directly to generate a like-sized
>matrix z which is the appropriate function of x and y. If necessary, you could
>create a modified version of 'FunctionMfile' which can do that. It's cleaner to
>do it that way than to fool around with extraneous outputs and inputs in the
>function. It will make your code easier to follow and modify later.
>
>Roger Stafford

--
Message posted via MathKB.com
http://www.mathkb.com/Uwe/Forums.aspx/matlab/200804/1

Subject: Non-rectangular 3D plot

From: Roger Stafford

Date: 24 Apr, 2008 08:22:03

Message: 12 of 16

"Bookie via MathKB.com" <u43093@uwe> wrote in message
<8323af6567d50@uwe>...
> Hi Roger,
>
> Ok I see where my code would get very confusing now. To answer the
points
> you made:
>
> 1. I understand now that my mesh decides how many points I have in each
> direction.
> 2. I need a 3d matrix for z because my function outputs a 4x1 vector for z
> and I am evaluating this vector over vector parameters x & y, which add
the
> m and n dimension to z. Its easier not to program my function to accept
> arrays. I originally had it accepting arrays for my inputs, but I had to
> apply 3d matricies to a long program verses applying it to a single for loop
> when I call the function.
> 3. I misused the zero function. I had thought zero(m) made an mx1 vector.
I
> realize its makes an mxm matrix actually.
> 4. I have shown my new code below without x and y in the for loop. Please
> see my query about this below.
> 5. I see the redundancy, but for now I want to see that this method works
> before I modify the variables (that are functions of x & y) in my function m.
> file that I am calling.
> 6. I do not necessarily need y to be a matrix. It can be a vector, but I need
> to have y increment over a counter j for a given x value.
>
> My new code:
>
> m=21 % m & n may not always be equal!
> n=21
> z=zeros(4,m,n); % preallocate z matrix
> [ix,iy] = meshgrid(1:m,1:n);
> x = a + (ix-1)*(b-a)/(m-1); % a <= x <= b
> y = (iy-1).*(M*x+B)/(n-1); % 0 <= y <= M*x+B
> for i =1:m
> for j=1:n
> z(:,i,j) = FunctionMfile(x(i)+y(j),x(i)-y(j),x(i),y(j));
> end
> end
> Z(i,j) = z(1,i,j); % take only 1st layer of 3d matrix
> surf(x,y,Z)
>
> My question is that when I enter the j-loop, as y(j) goes from y(1) to y(n),
> will iy (in the function for y) increment and x stays constant at x(i)? So
> for i=1, in the equation for y: x=x(i) and as j=1 to n, iy(j) =1 to n.
> Therefore y goes from (iy(1) - 1)*(M*x(i) +B) to (iy(n) -1)*(M*x(i)+B), where
> x(i) does not change within the j-loop. Then the whole process repeats for
a
> new x(i+1) value.
>
> It seems as if the x in the y-function may be incrementing as well within
the
> j-loop. Since x (and y) is not in the for loop how does the program know
that
> x is only a function of i and not j? Hence, y may be incrementing from (iy
(1)-
> 1)*(M*x(1)+B) to (iy(n)-1)*(M*x(n)+B).
>
> Thanks again for helping out a matlab novice!
>
> Kind regards,
> Bookie
------------
  There are still a few things amiss in your new code. When you enter the
nested for-loops, both x and y are n x m matrices, not vectors. It is true that
x(i,j) remains unchanging as i varies because that is the way ix is.
Nevertheless 'surf' expects to receive a matrix input, not a vector, so the
matrix version of input is necessary. Because of its definition, y(i,j) varies
with both i and j, and of course 'surf' would expect a matrix as input for y
also. For those reasons your for-loops ought to read:

for i = 1:n % <-- i varies from 1 to n
  for j = 1:m % <-- j varies from 1 to m
    z(:,i,j) = FunctionMfile(x(i,j)+y(i,j), ...
                  x(i,j)-y(i,j),x(i,j),y(i,j));
  end
end

Your use of x(i) and y(j), as if x and y were only vectors, is incorrect.

  The second thing is that Z should be an n x m matrix also, just as x and y
are, not m x n, so your preallocation should be

z = zeros(4,n,m);

  A third matter is that the line

Z(i,j) = z(1,i,j);

is not going to work by itself. You need

Z = squeeze(z(1,:,:));

  Note that in my previous message to you I confused m and n (and therefore
p and q.) I asserted that x and y were m x n in size and that is wrong. You
have my apology. They are n x m in size, just as Z must be.

  When you write

 [ix,iy] = meshgrid(1:m,1:n);

then both ix and iy will be n x m matrices in size, and hence in our case so
will x and y. This is because 'meshgrid' treats the second/column index as
being in the x-direction and the first/row index in the y-direction for
convenience in graphical matters.

Roger Stafford

Subject: Non-rectangular 3D plot

From: Bookie via MathKB.com

Date: 24 Apr, 2008 16:59:29

Message: 13 of 16

Hi Roger,

    My program is working as I expected now! I appreciate your expertise in
this forum. You've been a great help. Thanks for increasing my knowledge of
Matlab. :)

Cheers,
Bookie
  

Roger Stafford wrote:
>"Bookie via MathKB.com" <u43093@uwe> wrote in message
><8323af6567d50@uwe>...
>> Hi Roger,
>>
>[quoted text clipped - 51 lines]
>> Kind regards,
>> Bookie
>------------
> There are still a few things amiss in your new code. When you enter the
>nested for-loops, both x and y are n x m matrices, not vectors. It is true that
>x(i,j) remains unchanging as i varies because that is the way ix is.
>Nevertheless 'surf' expects to receive a matrix input, not a vector, so the
>matrix version of input is necessary. Because of its definition, y(i,j) varies
>with both i and j, and of course 'surf' would expect a matrix as input for y
>also. For those reasons your for-loops ought to read:
>
>for i = 1:n % <-- i varies from 1 to n
> for j = 1:m % <-- j varies from 1 to m
> z(:,i,j) = FunctionMfile(x(i,j)+y(i,j), ...
> x(i,j)-y(i,j),x(i,j),y(i,j));
> end
>end
>
>Your use of x(i) and y(j), as if x and y were only vectors, is incorrect.
>
> The second thing is that Z should be an n x m matrix also, just as x and y
>are, not m x n, so your preallocation should be
>
>z = zeros(4,n,m);
>
> A third matter is that the line
>
>Z(i,j) = z(1,i,j);
>
>is not going to work by itself. You need
>
>Z = squeeze(z(1,:,:));
>
> Note that in my previous message to you I confused m and n (and therefore
>p and q.) I asserted that x and y were m x n in size and that is wrong. You
>have my apology. They are n x m in size, just as Z must be.
>
> When you write
>
> [ix,iy] = meshgrid(1:m,1:n);
>
>then both ix and iy will be n x m matrices in size, and hence in our case so
>will x and y. This is because 'meshgrid' treats the second/column index as
>being in the x-direction and the first/row index in the y-direction for
>convenience in graphical matters.
>
>Roger Stafford

--
Message posted via http://www.mathkb.com

Subject: Non-rectangular 3D plot

From: CENGIZ

Date: 10 Feb, 2010 11:38:04

Message: 14 of 16

Hello Roger,

Your answer was helpful to me too. I have a very similar problem. I was wondering how i can make a 3d plot using a triangular domain. suppose that i need to plot z values for all (x,y) values within the triangular region (a1,b1), (a2,b2), and (a3,b3). I tried to use the parametrization technique you gave here, but could not figure out how to apply to my situation. Any thoughts would be appreciated.

Best...

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <fum3cu$r2t$1@fred.mathworks.com>...
> "Bookie" <u43093@uwe> wrote in message <831044a106ed5@uwe>...
> > Hi,
> >
> > I was wondering how I can make a 3D plot using non-rectandular data for
> x
> > and y. My x-y domain can be described with limits: a <= x <= b AND 0
> <= y <=
> > m*x + b. Then z, forming the surface of the plot, will be a function of x
> > and y. So the shadow of the plot will not look like a rectangle but some
> > other 4-sided polygon. Thanks for you help in advance!
> >
> > Bookie
> ----------
> In the 'surf' and 'surfc' functions it isn't necessary that the x-y domain
> covered be a rectangle. It is only necessary that there be a two-parameter
> representation of the desired surface in which the parameters fill a
> rectangular space. In your example, you could use parameters, ix and iy
> where
>
> [ix,iy] = meshgrid(1:p,1:q); % Use a p by q mesh of ix & iy values
> x = a + (ix-1)*(b-a)/(p-1); % a <= x <= b
> y = (iy-1).*(m*x+b)/(q-1); % 0 <= y <= m*x+b
> z = (Whatever your function of x and y is)
> surf(x,y,z)
>
> There is an example of a sphere generated by 'surf' in the function reference
> to matlab. Study it to see how the parameterization works. In particular note
> that what you call the "shadow" is a circle in this case.
>
> Roger Stafford
>

Subject: Non-rectangular 3D plot

From: Luigi Giaccari

Date: 11 Feb, 2010 16:28:06

Message: 15 of 16

"Bookie" <u43093@uwe> wrote in message <831044a106ed5@uwe>...
> Hi,
>
> I was wondering how I can make a 3D plot using non-rectandular data for x
> and y. My x-y domain can be described with limits: a <= x <= b AND 0 <= y <=
> m*x + b. Then z, forming the surface of the plot, will be a function of x
> and y. So the shadow of the plot will not look like a rectangle but some
> other 4-sided polygon. Thanks for you help in advance!
>
> Bookie
>

I hope one of these works

http://www.mathworks.com/matlabcentral/newsreader/create_message?reply_id=711429

http://www.advancedmcode.org/how-to-plot-a-coloured-surface-from-3d-scatter.html

http://www.advancedmcode.org/surface-recostruction-from-scattered-points-cloud-mycrustopen.html

http://www.advancedmcode.org/surface-recostruction-from-scattered-points-cloud-mycrust-robust.html



http://www.advancedmcode.org

Subject: Non-rectangular 3D plot

From: Roger Stafford

Date: 27 Feb, 2010 04:07:03

Message: 16 of 16

"CENGIZ " <cengizucbenli@gmail.com> wrote in message <hku5qs$81c$1@fred.mathworks.com>...
> Hello Roger,
>
> Your answer was helpful to me too. I have a very similar problem. I was wondering how i can make a 3d plot using a triangular domain. suppose that i need to plot z values for all (x,y) values within the triangular region (a1,b1), (a2,b2), and (a3,b3). I tried to use the parametrization technique you gave here, but could not figure out how to apply to my situation. Any thoughts would be appreciated.
>
> Best...

  Hello Cengiz. Sorry for the delay. I didn't notice your question when it appeared Feb. 10.

  With a triangle whose vertices are (a1,b1), (a2,b2), and (a3,b3) in the x-y plane, you can generate points falling within the triangle parametrically using two parameters p and q defined by:

 x = p*a1 + (1-p)*(q*a2 + (1-q)*a3)
 y = p*b1 + (1-p)*(q*b2 + (1-q)*b3)

with p and q in the rectangular (actually square) region

 0 <= p <= 1
 0 <= q <= 1

  (This does have the disadvantage that points will be clustered more densely near the (a1,b1) vertex.)

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us