Finding points on a line from user input

조회 수: 1 (최근 30일)
Brian
Brian 2013년 6월 15일
The question is as follows: 'Make a function file “e82.m” with 5 input parameters. The first four are two pairs of xand y- coordinates (x1, y1, x2, y2 ) and the fifth, a single x-value. The first four are two dots in a y-x plot. Connect these with a line and the fifth value is also a dot on the same line, but you should find out the corresponding y-value and present everything in a plot. Of course the five parameters must be chosen to be arbitrary.'
so far I have this: xvalueone = 'x value between 0 and 10'; xvalue = input(xvalueone); a=xvalue/10
yvalueone = 'y value between 0 and 10'; yvalue = input(yvalueone); b=yvalue/10
xvaluetwo = 'second x value between 0 and 10'; xvalue = input(xvaluetwo); c=xvalue/10
yvaluetwo = 'second y value between 0 and 10'; yvalue = input(yvaluetwo); d=yvalue/10
A=[a,c] B=[b,d]
plot(A,B,'-')
input ('select x value between the previous two values')
BUT I don't know how to get the corresponding value from the line.
Thanks for any help

채택된 답변

Image Analyst
Image Analyst 2013년 6월 15일
Use the equation of a line y = m*x+b. m is the slope and is (y2-y1)/(x2-x1). b is the offset and I'm sure you can figure that out because this is just 8th grade algebra. If you can't, you can just use polyfit to tell you.
coeffs = polyfit([x1, x2], [y1, y2], 1)
So then you know m, and b, and you just plug in the x and you get the y - it's trivial.

추가 답변 (1개)

Brian
Brian 2013년 6월 16일
Thanks...in all these problems it isn't the maths that causes problems I just don't know how to programme what I want to, I never did anything before/other languages.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by