【问题标题】:How do I plot my function to a mesh如何将我的函数绘制到网格上
【发布时间】:2012-01-05 00:10:13
【问题描述】:

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

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

提前致谢。 拉尔夫。

【问题讨论】:

    标签: matlab plot triangulation uncertainty


    【解决方案1】:

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

    接下来,要获得向量输入的向量答案,您必须将p(i)(选择p 的第i 个元素)更改为p(i,:),这将选择第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) 坐标集,将它们展平为uncertain 的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)))
    

    注意:你会得到很多非常大的数字,因为当 theta 是 0 或 pi(或非常接近)时,你会被(几乎)0 除。

    【讨论】:

    • 感谢您的回答。我试着理解你的修改。但结果出乎意料,因为角度应该接近 90°,因此 sin(angle) 应该接近 1。我有一个丑陋的 java 代码生成这样的图像:pastehtml.com/view/bjowk6rbg.html
    • 但是我的 matlab 代码仍然缺少 100 的限制,例如:不确定性 = max (uncertain,100) 坐标范围应该缩小。
    • 我在代码中发现了另一个错误:三个角必须是 180°,因为它们应该是三角形的三个内角 (s1,s2,p)。
    • 但是theta2 = cart2pol...返回站2的外角,所以theta一定是theta2-theta1 ?
    • 代码是合理的,因为它矢量化了您在问题中编写的版本 - 我想,意外的结果与您的数学问题有关(我无法帮助您)。为什么不尝试手动计算单个p,验证结果是否符合您的预期,然后使用您的uncertain 函数与它一致?
    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 2021-10-01
    • 2012-01-02
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多