Finding the equation of a line passing 2 points

536 ビュー (過去 30 日間)
Soroush Asarzadeh
Soroush Asarzadeh 2016 年 7 月 16 日
コメント済み: Image Analyst 2023 年 1 月 29 日
Hello, I have two points (x1,y1) and (x2,y2). Now I want to find the linear equation of a line passing through these 2 points. The equation must be like f(x)=a*x+b. Is there any function in matlab that accepts coordinates of two points an gives the related linear equation back? If not, I know that a=(y2-y1)/(x2-x1) but what is the short and easy way to find 'b'? Thanks in advance!
  1 件のコメント
Ahmed
Ahmed 2021 年 12 月 27 日
You can select Polynomial of degree 1 from the cftool
this will give you something like this
Linear model Poly1:
f(x) = p1*x + p2
Coefficients:
p1 = xx
p2 = xx
where xx could be any number

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2016 年 7 月 16 日
Try polyfit:
coefficients = polyfit([x1, x2], [y1, y2], 1);
a = coefficients (1);
b = coefficients (2);
  5 件のコメント
Jose
Jose 2022 年 11 月 27 日
When I use polyfit, It tells me that "x1" does not exist.
Image Analyst
Image Analyst 2022 年 11 月 27 日
@Jose then define it. Certainly you must know what points you fit the line through. So, what variable names did you use? Use those instead of x1, etc.

サインインしてコメントする。

その他の回答 (3 件)

Star Strider
Star Strider 2016 年 7 月 16 日
A third (and probably the most efficient) option for this particular problem:
x = [1 2];
y = [5 4];
c = [[1; 1] x(:)]\y(:); % Calculate Parameter Vector
slope_m = c(2)
intercept_b = c(1)
slope_m =
-1
intercept_b =
6
This uses the mldivide,\ operator to do a least-squares fit of the points.
  2 件のコメント
Soroush Asarzadeh
Soroush Asarzadeh 2016 年 7 月 17 日
編集済み: Soroush Asarzadeh 2016 年 7 月 17 日
Thanks!
Star Strider
Star Strider 2016 年 7 月 17 日
My pleasure!

サインインしてコメントする。


Alok Mishra
Alok Mishra 2022 年 9 月 15 日
編集済み: Alok Mishra 2022 年 9 月 15 日
    function [a b c eq] = makeEquationFrom2Points(x1,y1,x2,y2)
        syms x y;
        if x1==x2 && y1==y2
            disp('Need 2 distinct points');
            a=NaN;
            b=NaN;
            c=NaN;
            eq="null";
            return;
        else if x1==x2
            b=0;
            a=1;
            c=x1;
        else
            %for lines m not_equal to inf 
            %y=mx+c
            %m is coeff(1) and coeff(2) is c 
            coefficient=polyfit([x1 x2],[y1 y2],1);
            a=-coefficient(1);
            c=coefficient(2);
            b=1;
        end
        eq=a*x+b*y==c;
    end

on workspace do:

      [a b c eq]=makeEquationFrom2Points(1,2,3,4)

Output: a =

   -1.0000

b =

     1

c =

    1.0000

eq =

y - x == 1

  3 件のコメント
navid seif
navid seif 2023 年 1 月 29 日
Thanks.
May I ask how do it works when x1,x2,y1and y2 are vector?
Image Analyst
Image Analyst 2023 年 1 月 29 日
@navid seif put the function in a loop over x1
for k = 1 : numel(x1)
if x1(k) == x2(k) && y1(k) == y2(k)
% etc.
end
end
Everything should have an index k, like a(k), b(k), etc., so that you store the results for every set of data.

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 16 日
After you found a, You can get b from your equation y=a*x+b,

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by