intersction graph with line
1 view (last 30 days)
Show older comments
Hi everyone, can u help me out im still pretty green. This is a simple one but I cant figure it out. I tried to look for similar problem but did not find satisfy answer.
I'm having some data set which I plot with basic plot command. My question is, can matlab give me right away answer for the y value at my distance of say 4.5 like in the simplify example having in mind that my graph is just a line connecting y values
x=1:10;
y=-5*rand(1,10);
plot(x,y)
hold on
plot([4.5 4.5],[0 -5],'r')
thanks in advance.
0 Comments
Accepted Answer
Star Strider
on 30 Aug 2012
Edited: Star Strider
on 30 Aug 2012
I am not exactly certain what you want, but my guess would be:
p = polyfit(x,y,1);
y1 = polyval(p, 4.5);
The value returned for y1 would then be the value that might be expected to be at an x value of 4.5 on that particular iteration based on a linear fit. Since your data are changing between iterations (they are random), y1 will change with them.
If you want to know what the value is on a line between connecting points, to calculate the value for 4.5, the easiest way is probably:
p = polyfit([4 5], y([4 5]), 1);
y1 = polyval(p, 4.5);
That is how I would do it.
(And thank you Walter for your editing.)
2 Comments
Star Strider
on 31 Aug 2012
It is my pleasure to help!
I do not know what your data look like so I cannot suggest a specific solution that might work for you in all situations. However, I suggest you also look at MATLAB's various interpolation functions, for example interp1, and more generally, the functions in Interpolation and Computational Geometry.
Thank you for accepting my answer.
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!