How do I sort these 2D-Points to get a proper surf plot of this fun... (2024)

33 views (last 30 days)

Show older comments

Max on 10 Jun 2024 at 19:06

  • Link

    Direct link to this question

    https://ch.mathworks.com/matlabcentral/answers/2127221-how-do-i-sort-these-2d-points-to-get-a-proper-surf-plot-of-this-function

  • Link

    Direct link to this question

    https://ch.mathworks.com/matlabcentral/answers/2127221-how-do-i-sort-these-2d-points-to-get-a-proper-surf-plot-of-this-function

Edited: Max on 10 Jun 2024 at 19:18

Accepted Answer: Torsten

Open in MATLAB Online

Hello! I am currently trying to plot the function

How do I sort these 2D-Points to get a proper surf plot of this fun... (2)

on the unit disk How do I sort these 2D-Points to get a proper surf plot of this fun... (3). I wrote a function to get some evaluationpoints inside and on the boundary of the disk but when I plot the function on it I get very weird results. I already discussed a similar problem in a previous question and a nice person explained to me that the problem was the way my points where sorted in the matrix and therefore the reshaping didn't work as desired. Since I get the same problem in the surf plot again, I am quite sure that this is the same case here.

In the function that I wrote to generate points I first started to lay a grid on a square. After that I sort out every point that is outside of the circle or that is on the boundary of the circle. Lastly I put points on the boundary of the circle. The function takes parameters How do I sort these 2D-Points to get a proper surf plot of this fun... (4) and N. First two parameters are the bounds for the square How do I sort these 2D-Points to get a proper surf plot of this fun... (5). The parameter Nis going to be the squareroot of the number of Points we will end up with. The function gives back Matrices xint, xbdy and x. xint holds the interior points and xbdy the points on the boundary of the circle. x should have both points together but there is still the problem on how I sort these... The rows of these matrices are points in How do I sort these 2D-Points to get a proper surf plot of this fun... (6) that means e.g. x is gonna be a How do I sort these 2D-Points to get a proper surf plot of this fun... (7) Matrix.

[~,~,~] = GetCircleGrid(-1,1,30); %Example

How do I sort these 2D-Points to get a proper surf plot of this fun... (8)

function [xint,xbdy,x] = GetCircleGrid(a,b,N)

r = (b-a)/2;

% Centerpoint of Circle

x0 = a+r;

y0 = a+r;

% First get a Grid on the whole square

[xsqr1,xsqr2] = meshgrid(linspace(a,b,N),linspace(a,b,N));

xsqr = [xsqr1(:),xsqr2(:)];

% Now get rid of Points outside of Circle

k=1; % counter

for j=1:N^2

if sqrt((xsqr(j,1)-x0)^2+(xsqr(j,2)-y0)^2) < r % If distance from centerpoint is < r

xint(k,:) = xsqr(j,:);

k=k+1;

end

end

NB = N^2-(k-1); % Number of points for boundary so we end up with N^2 points in total

% Get the Boundarypoints

theta = linspace(0,2*pi,NB);

xbdy = zeros(NB,2);

xbdy(:,1) = x0 + r*cos(theta)';

xbdy(:,2) = y0 + r*sin(theta)';

x = sortrows([xint; xbdy]); % How to sort these...? :(

scatter(xint(:,1),xint(:,2),'ob');

axis equal;

hold on;

scatter(xbdy(:,1),xbdy(:,2),'or');

hold off;

end

The picture show, this is exactly how I want it too look for later usage... Now I wanted to plot the above function (for later usage too...) on the unit disc:

% The function

u = @(x) (5/4)*(1-x(:,1).^2-x(:,2).^2).*sin(pi*x(:,1))+1;

% Get some evaluation points ...

N = 30;

[xint,xbdy,x]=GetCircleGrid(-1,1,N); % ... in the unit disk

How do I sort these 2D-Points to get a proper surf plot of this fun... (9)

% Reshape evaluation points

X = reshape(x(:,1),N,N); % not working well...

Y = reshape(x(:,2),N,N); % not working well...

U = reshape(u(x),N,N); % evaluate the function

% Plot the function as well as the evaluationpoints

figure(1)

surf(X,Y,U,'FaceAlpha',0.5,'EdgeColor','interp');

colormap cool; camlight; lighting gouraud; material dull;

title('f(x,y)')

hold on;

scatter3(X,Y,zeros(N,N),'og');

hold off;

How do I sort these 2D-Points to get a proper surf plot of this fun... (10)

As you can see the plot has some weird lines that are not wanted. How should I sort the points in x to get a proper surf plot? In addition to that I wanted to plot the inner points in a different collor than the boundary points but since the number of points inside and the number of points on the boundary of the disc are not square numbers I don't know how to use reshape for xint and xboundary...

Thank you very much for your help! Kind regard, Max.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Torsten on 10 Jun 2024 at 19:16

  • Link

    Direct link to this answer

    https://ch.mathworks.com/matlabcentral/answers/2127221-how-do-i-sort-these-2d-points-to-get-a-proper-surf-plot-of-this-function#answer_1469746

  • Link

    Direct link to this answer

    https://ch.mathworks.com/matlabcentral/answers/2127221-how-do-i-sort-these-2d-points-to-get-a-proper-surf-plot-of-this-function#answer_1469746

Edited: Torsten on 10 Jun 2024 at 19:17

Open in MATLAB Online

x = linspace(-1,1,1000);

y = linspace(-1,1,1000);

[X,Y] = meshgrid(x,y);

Z = nan(size(X));

idx = X.^2+Y.^2 <= 1;

Z(idx) = 1.25*(1-X(idx).^2-Y(idx).^2).*sin(pi*X(idx))+1;

h = surf(X,Y,Z);

set(h,'edgecolor','none')

colorbar

How do I sort these 2D-Points to get a proper surf plot of this fun... (12)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Tags

  • surf
  • plotting
  • surface
  • matrix
  • reshape

Products

  • MATLAB

Release

R2024a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How do I sort these 2D-Points to get a proper surf plot of this fun... (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How do I sort these 2D-Points to get a proper surf plot of this fun... (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5648

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.