How to get a line from two points collected using ginput
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Faisal
el 18 de Abr. de 2023
Comentada: Faisal
el 26 de Abr. de 2023
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 comentarios
Rik
el 18 de Abr. de 2023
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?
Respuesta aceptada
John D'Errico
el 18 de Abr. de 2023
Editada: John D'Errico
el 18 de Abr. de 2023
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)
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]
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!