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));必要があります。s1s2

次に、ベクトル入力に対するベクトルの答えを得るには、p(i)(の i 番目の要素を選択するp) をに変更する必要があります。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)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)))

theta注:いつが0またはpi(またはほぼ)であるか(ほぼ)0で割っているため、非常に大きな数がたくさん得られます。

于 2012-01-05T01:24:19.183 に答える