- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Meshgrid on inhomogeneous surface plot
조회 수: 3 (최근 30일)
이전 댓글 표시
I have simulated data, that behaves inhomogeneous along x and y, resulting in an surface plot as shown below.
Due to the inhomogeneity, the build-in edges of the surf function results in an accumulation of black lines at the origin as well as the end of thy y-axis. An additional challenge is that the plot is in the shape of a triangular, thus not covering the whole x-y plane.
I want to have lines along x and y with equidistant distance on top of the surface plot. Its kind of the background grid on the x-y plane at z = 0, but projected on the surface plot.
Here is a minimal example to generate an inhomogeneous surface plot:
% Create inhomogenous arrays x and y
x = [0 1 2 2.5 2.6 2.7 2.8 2.85 2.9 2.95 2.975];
y = x+5;
% Create an inhomogenous mesh xx and yy
xx = x;
yy = y;
for i=2:length(x)-1
xx = cat(1,xx,x.^(1+i/10));
yy = cat(1,yy,y.^(2+i/10));
end
ZZ = xx + yy;
figure(1)
clf
surf(xx,yy,ZZ)
xlabel('x')
ylabel('y')
colormap sky
xlim([0 9])
ylim([0 500])
zlim([0 500])
댓글 수: 0
답변 (2개)
Hassaan
2024년 1월 16일
편집: Hassaan
2024년 1월 16일
% Create inhomogenous arrays x and y
x = [0 1 2 2.5 2.6 2.7 2.8 2.85 2.9 2.95 2.975];
y = x+5;
% Create an inhomogenous mesh xx and yy
xx = x;
yy = y;
for i=2:length(x)-1
xx = cat(1,xx,x.^(1+i/10));
yy = cat(1,yy,y.^(2+i/10));
end
ZZ = xx + yy;
figure(1)
clf
surf(xx,yy,ZZ)
xlabel('x')
ylabel('y')
colormap sky
xlim([0 9])
ylim([0 500])
zlim([0 500])
% Your existing code to create the surface plot
% ...
% Step 2: Determine Grid Points
% Example: Creating 10 evenly spaced grid lines along x and y
xGrid = linspace(min(x), max(x), 10);
yGrid = linspace(min(y), max(y), 10);
% Step 3 & 4: Calculate Grid Line Coordinates and Plot
hold on; % Keep the current surface plot
for i = 1:length(xGrid)
% Find the closest points in xx to the current grid line
[~, idx] = min(abs(xx(1,:) - xGrid(i)), [], 2);
plot3(xx(:,idx), yy(:,idx), ZZ(:,idx), 'k'); % 'k' for black lines
end
for i = 1:length(yGrid)
% Find the closest points in yy to the current grid line
[~, idx] = min(abs(yy(:,1) - yGrid(i)), [], 2);
plot3(xx(idx,:), yy(idx,:), ZZ(idx,:), 'k'); % 'k' for black lines
end
hold off;
% Step 5: Adjust Aesthetics
% Set the properties of the plot as needed
xlabel('x');
ylabel('y');
colormap sky;
xlim([0 9]);
ylim([0 500]);
zlim([0 500]);
This code creates a set of grid lines at regular intervals along the x and y axes and plots them on the surface plot. The plot3 function is used to plot lines in 3D space. Adjust the number of grid lines by changing the number in linspace. Also, fine-tune the appearance of the grid lines (like color, line width) as needed.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
Feel free to contact me.
댓글 수: 3
Hassaan
2024년 1월 16일
편집: Hassaan
2024년 1월 16일
% Create a more homogeneous mesh
x = linspace(0, 3, 100); % 100 points from 0 to 3
y = x + 5; % Adjust as needed
[xx, yy] = meshgrid(x, y);
ZZ = xx + yy.^2; % Example calculation for ZZ
% Plotting the surface
figure(1);
clf;
surf(xx, yy, ZZ);
xlabel('x');
ylabel('y');
colormap sky;
xlim([0, 9]);
ylim([5, 8]); % Adjust according to your y range
zlim([0, 500]);
% Adding grid lines
hold on;
xGrid = linspace(min(x), max(x), 10); % 10 evenly spaced x-grid lines
yGrid = linspace(min(y), max(y), 10); % 10 evenly spaced y-grid lines
for i = 1:length(xGrid)
plot3([xGrid(i) xGrid(i)], [min(y) max(y)], [500 500], 'k'); % Grid lines along x-axis
end
for i = 1:length(yGrid)
plot3([min(x) max(x)], [yGrid(i) yGrid(i)], [500 500], 'k'); % Grid lines along y-axis
end
hold off;
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
Dyuman Joshi
2024년 1월 16일
To modify the lines on the surface, you will have to modify the underlying data.
You might not be able to get the exact shape, but griddata with a finer grid seems to be the best bet -
% Create inhomogenous arrays x and y
x = [0 1 2 2.5 2.6 2.7 2.8 2.85 2.9 2.95 2.975];
y = x+5;
% Create an inhomogenous mesh xx and yy
xx = x;
yy = y;
for i=2:length(x)-1
xx = cat(1,xx,x.^(1+i/10));
yy = cat(1,yy,y.^(2+i/10));
end
ZZ = xx + yy;
figure()
s = surf(xx,yy,ZZ);
xlim([0 9])
ylim([0 500])
zlim([0 500])
%Generate linearly spaced points in the range of the data
xG = linspace(min(xx,[],'all'), max(xx,[],'all'), 75);
yG = linspace(min(yy,[],'all'), max(yy,[],'all'), 75);
%Make a meshgrid with the linearly spaced points
%to use as input for grid-data
[xq, yq] = meshgrid(xG, yG);
vq = griddata(xx, yy, ZZ, xq, yq);
figure()
surf(xq, yq, vq)
xlim([0 9])
ylim([0 500])
zlim([0 500])
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!