intersction graph with line
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2012년 8월 30일
편집: Star Strider
2012년 8월 30일
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
Star Strider
2012년 8월 31일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!