필터 지우기
필터 지우기

intersction graph with line

조회 수: 1 (최근 30일)
Imo
Imo 2012년 8월 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.

채택된 답변

Star Strider
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
Imo
Imo 2012년 8월 31일
편집: Imo 2012년 8월 31일
I'm sorry if I confused you. Your solution is what I need, thank you very much. Rand function is just to give me any set of data just for quick example here. I actually have points that represent my river profile and I need matlab to give me depths in particular places of that profile based on that interpolation.
Only problem would be here that my profile consist of a large amount of dots and i don't know the values of y bewven my specified point (in example 4.5) that i need for interpolation. I thought matlab will figure it out on its own:)
Star Strider
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 CenterFile Exchange에서 Directed Graphs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by