2011-12-07 24 views
0

ax + +の2つの線方程式の3つの入力(係数のベクトル[abc]とxの値からなる2つのベクトル)を持つ関数を書くにはどうすればよいですか? = cは、交点のxとyの値を与えるベクトルを出力します。Matlab:交点のxとyの値を与える出力ベクトル

例:solveSystem([1 -1 -1]、[3 1 9]、 - 5:5)これまで

[2 3]を生成しなければならない:

function coeff=fitPoly(mat) 

% this function takes as an input an nx2 matrix consisting of the 
% coordinates of n points in the xy-plane and give as an output the unique 
% polynomial (of degree <= n-1) passing through those points. 


[n,m]=size(mat); % n=the number of rows=the number of points 
% build the matrix C 
if m~=2 
    fprintf('Error: incorrect input'); 
    coeff=0; 
    else 
    C=mat(:,2); % c is the vector of y-coordinates which is the 2nd column of mat 

    % build the matrix A 
    for i=1:n 
    for j=1:n 
     A(i,j)=(mat(i,1))^(n-j); 
    end 
end 
coeff=inv(A)*C; 
end 
+2

ようになっています。あなたはax + bu = cを意味しましたか? – Kavka

+0

申し訳ありません...意味ax + by = c –

+0

あなたの質問とあなたが投稿したコードは一貫していません。線の交点または多項式の近似を探していますか?あなたはあなたの質問を明確にすべきです。 – Kavka

答えて

2

あなた

function xy = solve(c1,c2) 
    A = [c1(1:2); c2(1:2)]; 
    b = [c1(3); c2(3)]; 
    xy = A\b; 
end 

のために計算します:2線の交点を解くためにベクトルxを必要としません
xy = solve([1 -1 -1],[3 1 9]) 

行列

A = [1 -1; 
    3 1] 
b = [-1 
    9] 

AX + BX = cが線の方程式はない

xy = A^-1 b = [2 
       3] 
+0

余分な+1は '\'演算子の使用になります。 – Nzbuu

関連する問題