如何将我的函数绘制到网格中

时间:2012-01-05 00:10:13

标签: matlab plot triangulation uncertainty

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

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

提前致谢。 拉尔夫。

1 个答案:

答案 0 :(得分:1)

首先,我认为您的功能存在错误:您的[theta2, dist2] = cart2pol(p(1)-s1(1), p(2)-s2(2));应该首先s1s2

接下来,要获取矢量输入的向量答案,您必须将p(i)(选择p的第i个元素)更改为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)坐标集,将其展平为2xn的{​​{1}}矩阵,然后展开他们退回到网格绘图。例如:

uncertain

注意:你会得到很多非常大的数字,因为当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))) theta0(或非常接近)时,因为那时你除以(差不多)0。