1

我是一个新的 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 必须是矩阵,而不是标量或向量。”

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

提前致谢。拉尔夫。

4

1 回答 1

1

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

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

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

总之:

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)2xnuncertain

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。

于 2012-01-05T01:24:19.183 回答