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:
3D plot over a triangle region

Subject: 3D plot over a triangle region

From: Anna Larsen

Date: 20 Jan, 2012 18:00:10

Message: 1 of 6

Hello,

I have the following 3D funktion that I want to plot over a triangular region instead of a rectangular region. How can I solve this problem, and is it possible to z(max)=1

[x,y] = meshgrid(0:0.1:1);
z = x*(2*x.^2+2*y^2+2*(1-x-y).^2+3*y.*(1-x-y)-1);
figure
surf(x,y,z)

Best regards Anna

Subject: 3D plot over a triangle region

From: Steven_Lord

Date: 20 Jan, 2012 19:30:22

Message: 2 of 6



"Anna Larsen" <asla07@student.aau.dk> wrote in message
news:jfca3a$r4q$1@newscl01ah.mathworks.com...
> Hello,
> I have the following 3D funktion that I want to plot over a triangular
> region instead of a rectangular region. How can I solve this problem, and
> is it possible to z(max)=1

Set the values of z corresponding to (x, y) coordinates outside your
triangle to NaN.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: 3D plot over a triangle region

From: Bruno Luong

Date: 22 Jan, 2012 19:36:25

Message: 3 of 6

You could:

1. mesh the triangle in finer resolution (You might take a look at 2D mesh generation in FEX)
2. Compute the function at the knots
3. Call trimesh or trisurf to plot

Bruno

Subject: 3D plot over a triangle region

From: Roger Stafford

Date: 22 Jan, 2012 19:36:26

Message: 4 of 6

"Anna Larsen" wrote in message <jfca3a$r4q$1@newscl01ah.mathworks.com>...
> I have the following 3D funktion that I want to plot over a triangular region instead of a rectangular region. How can I solve this problem, and is it possible to z(max)=1
>
> [x,y] = meshgrid(0:0.1:1);
> z = x*(2*x.^2+2*y^2+2*(1-x-y).^2+3*y.*(1-x-y)-1);
> figure
> surf(x,y,z)
- - - - - - - - -
  You could make a mesh of parameters s and t defined as follows. Let P1 = (x1,y1), P2 = (x2,y2), and P3 = (x3,y3) be the three vertices of your triangle. Then do:

 [s,t] = meshgrid(linspace(0,1,100));
 x = (1-sqrt(s))*x1 + sqrt(s).*((1-t)*x2+t*x3);
 y = (1-sqrt(s))*y1 + sqrt(s).*((1-t)*y2+t*y3);
 z = x.*(2*x.^2+2*y.^2+2*(1-x-y).^2+3*y.*(1-x-y)-1);
 z(z>1) = 1;
 surf(x,y,z)

(Note: The square root avoids having greater density near P1.)

Roger Stafford

Subject: 3D plot over a triangle region

From: Matt J

Date: 22 Jan, 2012 19:36:27

Message: 5 of 6

"Anna Larsen" wrote in message <jfca3a$r4q$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have the following 3D funktion that I want to plot over a triangular region instead of a rectangular region.

You can set the z data outside the triangular region to NaNs. Then they won't show up in the plot.

Subject: 3D plot over a triangle region

From: Matt J

Date: 22 Jan, 2012 19:36:27

Message: 6 of 6

"Anna Larsen" wrote in message <jfca3a$r4q$1@newscl01ah.mathworks.com>...
>
>and is it possible to z(max)=1

Yes, there are many ways to reset the maximum value of the z array to 1.One option would be

z=z-max(z(:))+1

Since this approach and many others may or may not make sense for you, you should say what the purpose of doing this is.

Tags for this Thread

No tags are associated with 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