Finding points on a line from user input

1 view (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 15 Jun 2013
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.

More Answers (1)

Brian
Brian on 16 Jun 2013
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.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!