如何将函数绘制到网格上
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为您的函数有一个错误:您的
[theta2, dist2] = cart2pol(p(1)-s1(1), p(2)-s2(2));
应该有 th第一个s1
是s2
。接下来,要获得矢量输入的矢量答案,您必须将
p(i)
(选择p
的第 i 个元素)更改为p (i,:)
,它将选择p
的第 i 行。之后,将乘法 (
*
) 更改为按元素乘法 (.*
)。总之:
唯一的变化是
p(i)
->p(i,:)
和*
->.*
和/
->./
.要获得表面,您可以使用
meshgrid
获取网格中的所有(x,y)
坐标集,并将它们展平为2xn
矩阵对于不确定
,然后将它们展开回网格以进行绘制。示例:注意:您会得到很多非常大的数字,因为当
theta
为0
或pi
(或非常接近)时,因为那时您'再除以(几乎)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 firsts1
being as2
.Next, to get a vector answer out for your vector inputs, you have to change your
p(i)
(which selects the ith element ofp
) top(i,:)
, which will select the first ith row ofp
.After that, you change multiplication (
*
) to element-wise multiplication (.*
).In summary:
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 a2xn
matrix foruncertain
, and then expand them back out to the grid to plot. Example:Note: you'll get lots of really big numbers because when
theta
is0
orpi
(or very nearly) because then you're dividing by (almost) 0.