如何将函数绘制到网格上

发布于 2024-12-25 05:12:54 字数 757 浏览 2 评论 0原文

我是 MATLAB 新用户,我正在尝试绘制一个函数:

function [ uncertainty ] = uncertain(s1, s2, p)
%UNCERTAIN calculates the measurement uncertainty of a triangulation
% provide two coordinates of known stations and a target coordinate 
% of another point, then you get the uncertainty 
 [theta1, dist1] = cart2pol(p(1)-s1(1), p(2)-s1(2));
 [theta2, dist2] = cart2pol(p(1)-s1(1), p(2)-s2(2));
 theta=abs(pi-theta2-theta1);
 uncertainty = dist1*dist2/abs(sin(theta));
end

调用方式:

uncertain([0 0],[8 0],[4 4])

我得到一个结果。 但我想要一个整个表面并调用:

x=-2:.1:10;
y=-2:.1:10;
z = uncertain([0 0],[8 0],[x y]);
mesh(x,y,z)

我收到错误:“Z 必须是矩阵,而不是标量或向量。”

如何修改我的代码以便我的函数绘制表面?

提前致谢。 拉尔夫.

I am a new MATLAB user and I am trying to plot a function:

function [ uncertainty ] = uncertain(s1, s2, p)
%UNCERTAIN calculates the measurement uncertainty of a triangulation
% provide two coordinates of known stations and a target coordinate 
% of another point, then you get the uncertainty 
 [theta1, dist1] = cart2pol(p(1)-s1(1), p(2)-s1(2));
 [theta2, dist2] = cart2pol(p(1)-s1(1), p(2)-s2(2));
 theta=abs(pi-theta2-theta1);
 uncertainty = dist1*dist2/abs(sin(theta));
end

called with:

uncertain([0 0],[8 0],[4 4])

I get a single result.
But i want a whole surface and called:

x=-2:.1:10;
y=-2:.1:10;
z = uncertain([0 0],[8 0],[x y]);
mesh(x,y,z)

I get the error: "Z must be a matrix, not a scalar or vector."

How can I modify my code so that my function draws a surface?

Thanks in advance.
Ralf.

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

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

发布评论

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

评论(1

喜爱纠缠 2025-01-01 05:12:55

首先,我认为您的函数有一个错误:您的 [theta2, dist2] = cart2pol(p(1)-s1(1), p(2)-s2(2)); 应该有 th第一个 s1s2

接下来,要获得矢量输入的矢量答案,您必须将 p(i) (选择 p 的第 i 个元素)更改为 p (i,:),它将选择 p 的第 i

之后,将乘法 (*) 更改为按元素乘法 (.*)。

总之:

function [ uncertainty ] = uncertain(s1, s2, p)
%UNCERTAIN calculates the measurement uncertainty of a triangulation
% provide two coordinates of known stations and a target coordinate 
% of another point, then you get the uncertainty
% target coordinates p are 2xn
% output uncertainty is 1xn
 [theta1, dist1] = cart2pol(p(1,:)-s1(1), p(2,:)-s1(2));
 [theta2, dist2] = cart2pol(p(1,:)-s2(1), p(2,:)-s2(2));
 theta=abs(pi-theta2-theta1);
 uncertainty = dist1.*dist2./abs(sin(theta));
end

唯一的变化是 p(i) -> p(i,:)*->.*/->./.

要获得表面,您可以使用 meshgrid 获取网格中的所有 (x,y) 坐标集,并将它们展平为 2xn 矩阵对于不确定,然后将它们展开回网格以进行绘制。示例:

x=-2:.1:10;  % 121 elements
y=-2:.1:10;  % 121 elements
[xs,ys]=meshgrid(x,y); % xs and ys are each 121 x 121
zs = uncertain([0 0],[8 0],[xs(:) ys(:)]'); %get zs, being 1x(121*121) ie 1x14641
% Reshape zs to be 121x121 in order to plot with mesh
mesh(xs,ys,reshape(zs,size(xs)))

注意:您会得到很多非常大的数字,因为当 theta0pi (或非常接近)时,因为那时您'再除以(几乎)0。

First I think there's a mistake in your function: your [theta2, dist2] = cart2pol(p(1)-s1(1), p(2)-s2(2)); should have th first s1 being a s2.

Next, to get a vector answer out for your vector inputs, you have to change your p(i) (which selects the ith element of p) to p(i,:), which will select the first ith row of p.

After that, you change multiplication (*) to element-wise multiplication (.*).

In summary:

function [ uncertainty ] = uncertain(s1, s2, p)
%UNCERTAIN calculates the measurement uncertainty of a triangulation
% provide two coordinates of known stations and a target coordinate 
% of another point, then you get the uncertainty
% target coordinates p are 2xn
% output uncertainty is 1xn
 [theta1, dist1] = cart2pol(p(1,:)-s1(1), p(2,:)-s1(2));
 [theta2, dist2] = cart2pol(p(1,:)-s2(1), p(2,:)-s2(2));
 theta=abs(pi-theta2-theta1);
 uncertainty = dist1.*dist2./abs(sin(theta));
end

The only changes are p(i) -> p(i,:), and *->.* and /->./.

To get a surface, you use meshgrid to get all sets of (x,y) coordinates in a grid, flatten them into a 2xn matrix for uncertain, and then expand them back out to the grid to plot. Example:

x=-2:.1:10;  % 121 elements
y=-2:.1:10;  % 121 elements
[xs,ys]=meshgrid(x,y); % xs and ys are each 121 x 121
zs = uncertain([0 0],[8 0],[xs(:) ys(:)]'); %get zs, being 1x(121*121) ie 1x14641
% Reshape zs to be 121x121 in order to plot with mesh
mesh(xs,ys,reshape(zs,size(xs)))

Note: you'll get lots of really big numbers because when theta is 0 or pi (or very nearly) because then you're dividing by (almost) 0.

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