フィルターのクリア

How to get a line from two points collected using ginput

4 ビュー (過去 30 日間)
Faisal
Faisal 2023 年 4 月 18 日
コメント済み: Faisal 2023 年 4 月 26 日
OBJECTIVE. I have a curve, I want to select two points on the curve, fit a line between two points (overlaying on the main curve) and then find the intercept and slope of the fitted line.
MY METHODOLOGY. I can get two points using ginput function, but after, I am having trouble to find the points between the points using "find" function. also, I am unable to fit or plot the line of points from find or ginput function.
  2 件のコメント
Rik
Rik 2023 年 4 月 18 日
Why do you need find? You can calculate the slope and intercept from the two points directly.
Also, what exactly have you tried when you wanted to plot the second line? Did you forget to use the hold function?
Faisal
Faisal 2023 年 4 月 18 日
I tried polyfit to fit a line after ginput.
how to find the intercept from two points?

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

採用された回答

John D'Errico
John D'Errico 2023 年 4 月 18 日
編集済み: John D'Errico 2023 年 4 月 18 日
You aparently know how to use polyfit. But it seems you do not understand what it returns.
x = 1:5;
y = [2 3 5 7 11];
P1 = polyfit(x,y,1)
P1 = 1×2
2.2000 -1.0000
The result is a vector. The first element is the slope of the vector. The second element is the y-intercept.
plot(x,y,'o')
hold on
xpl = [0,5]
xpl = 1×2
0 5
ypl = polyval(P1,xpl);
plot(xpl,ypl,'r-')
As you can see, I extended the line down to x==0, where you should see it crosses the y-axis at y==-1, the y-intercept. So polyfit gives you exactly what you need. There is no need to "find" anything. You already have it.
  1 件のコメント
Faisal
Faisal 2023 年 4 月 26 日
@John D'Errico Thank you!
I succeeded to use the similar script and got what I was looking for.
Thanks again!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFit Postprocessing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by