MATLAB 中的非矩形网格

发布于 2025-01-11 20:04:07 字数 1310 浏览 0 评论 0原文

我想在 matlab 中创建一个非矩形网格。

基本上,我有一个多边形可行集,我需要制作一个网格,以便在该集中插入 3D 数据点。插值函数已给出,并且需要有限的 (x, y, z) 输入。其中 x 为 nx1,y 为 1xm,z 为 nxm。现在,我使用 linspace 设置了网格,并在使用我的函数之前将所有 NaN(不可行)值设置为 0,这当然是错误的(第三张图)。

有一个简单的解决方案吗?

我添加了一张图片来说明我当前正在做的事情:第一个图是可行集,第二个图是该集中求解的样本数据点,第三个图是插值(当前仍然使用矩形网格且 NaN = 0)。我需要的是一个看起来像第一个图形(红色多边形)而不是矩形的网格。在第三张图中,您可以看到矩形网格与将 NaN 设置为 0(=不可行值,不包含在红色多边形集中)相结合会导致沿边缘的错误插值,因为它包含不可行区域。

输入图片这里的描述

这是我使用矩形网格的代码:

figure (2) %sample data
plot3(X0(1,:), X0(2,:), U, 'x')
%X0(1,:) and X0(2,:) are vectors corresponding to the Z-Values (blue sample data)
%X0 and U are in the feasible set (red polygon)

xv = linspace(xLb(1), xUb(1), 100);
yv = linspace(xLb(2), xUb(2), 100); %xLb and xUb are upper and lower bounds for the rectangle mesh
[x1,x2] = meshgrid(xv, yv);
Z = griddata(X0(1,:), X0(2,:), U, x1, x2);
%This grid obviously includes values that are not in the feasible set (red polygon) by its rectangular nature

Z(isnan(Z))=0; %set infeasible values to 0, wrong of course
testMPC = someInterpolationFunction([0:length(Z)-1]',[0:length(Z)-1],Z);
testMPC.showInterpolation(20,20) 
%this shows figure 3 in the attached picture

I want to create a non-rectangular meshgrid in matlab.

Basically I have a polygon shaped feasible set I need to make a grid of in order to interpolate 3D data points in this set. The function for interpolation is given and requires finite (x, y, z) inputs. Where x is nx1, y is 1xm and z is nxm. Right now I have the mesh set up with linspace and set all NaN (infeasible) values to 0 before using my function, which is wrong of course (third figure).

Is there a simple solution for this?

I added a picture illustrating what I'm currently doing: First plot is the feasible set, second plot are solved sample data points in this set and third plot is the interpolation (currently still with rectangular meshgrid and NaN = 0). What I need is a meshgrid looking like the first figure (red polygon) instead of a rectangular one. In the third plot you can see that the rectangular meshgrid in combination with setting NaN to 0 (=infeasible values, not included in the red polygon set) results in a wrong interpolation along the edges, because it includes infeasible regions.

enter image description here

Here is my code using a rectangular meshgrid:

figure (2) %sample data
plot3(X0(1,:), X0(2,:), U, 'x')
%X0(1,:) and X0(2,:) are vectors corresponding to the Z-Values (blue sample data)
%X0 and U are in the feasible set (red polygon)

xv = linspace(xLb(1), xUb(1), 100);
yv = linspace(xLb(2), xUb(2), 100); %xLb and xUb are upper and lower bounds for the rectangle mesh
[x1,x2] = meshgrid(xv, yv);
Z = griddata(X0(1,:), X0(2,:), U, x1, x2);
%This grid obviously includes values that are not in the feasible set (red polygon) by its rectangular nature

Z(isnan(Z))=0; %set infeasible values to 0, wrong of course
testMPC = someInterpolationFunction([0:length(Z)-1]',[0:length(Z)-1],Z);
testMPC.showInterpolation(20,20) 
%this shows figure 3 in the attached picture

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2025-01-18 20:04:07

尝试这样的事情:

nRows = 100;
nCols = 200;

x1 = @(x) max(0, x-50);
x2 = @(x) min(nCols, nCols - 50 + x);

RR = zeros(nRows, nCols);
CC = zeros(nRows, nCols);

for iRow = 1:nRows
  c1 = x1(iRow);
  c2 = x2(iRow);
  
  colVec = linspace(c1, c2, nCols);
  
  RR(iRow, :) = iRow;
  CC(iRow, :) = colVec;
end

mesh(RR, CC, zeros(size(RR)))

在此处输入图像描述

您需要重新定义 x1x2 或课程以及缩放的函数,但这应该给你一个想法如何开始。

Try something like this:

nRows = 100;
nCols = 200;

x1 = @(x) max(0, x-50);
x2 = @(x) min(nCols, nCols - 50 + x);

RR = zeros(nRows, nCols);
CC = zeros(nRows, nCols);

for iRow = 1:nRows
  c1 = x1(iRow);
  c2 = x2(iRow);
  
  colVec = linspace(c1, c2, nCols);
  
  RR(iRow, :) = iRow;
  CC(iRow, :) = colVec;
end

mesh(RR, CC, zeros(size(RR)))

enter image description here

You'd need to redefine the functions for x1 and x2 or course as well as the scaling, but this should give you an idea of how to get started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文