How to find x values from y value in "fit" function?

조회 수: 42 (최근 30일)
Kanan Yagublu
Kanan Yagublu 2022년 5월 10일
댓글: Kanan Yagublu 2022년 5월 11일
Hi, I have some data and with this data I create a fit function:
f = fit(x,y,'smoothingspline');
plot(f,x,y);
I need to find X values in two points like in the picture.
I tried to use :
z = x(y==y_i);
But what I get is:
0×1 empty double column vector
How can I solve this problem?
Thanks in advance

채택된 답변

David Hill
David Hill 2022년 5월 10일
yval=-62;
eqn=@(x)feval(f,x)-yval
X1=fzero(eqn,4.8);
X2=fzero(eqn,5.3);
  댓글 수: 2
Kanan Yagublu
Kanan Yagublu 2022년 5월 11일
Thank you for your answer, but how did you get 4.8 and 5.3? Are they approximation from the graph?
Because when I change the data set the code doesn't work
Kanan Yagublu
Kanan Yagublu 2022년 5월 11일
@David Hill and how can I modify it so it will not depend on 4.8 and 5.3 ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 5월 10일
Set up a sample polynomial fit.
x = randn(10, 1);
y = (x-1).*(x+1); % polynomial is y = x^2-1 = (x-1)*(x+1)
p = fit(x, y, 'poly2')
p =
Linear model Poly2: p(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 1 (1, 1) p2 = 6.259e-17 (-2.697e-16, 3.949e-16) p3 = -1 (-1, -1)
Find the points where p takes on the value y = 3.
plusOneSolution = fzero(@(x) p(x)-3, 1)
plusOneSolution = 2
minusOneSolution = fzero(@(x) p(x)-3, -1)
minusOneSolution = -2
Check that evaluating the fit at those two points gives us the value y = 3.
check = p([plusOneSolution, minusOneSolution])
check = 2×1
3.0000 3.0000

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by