fit a curve through selected points with own function
이전 댓글 표시
Hey!
I have a problem.. I don't know why I'm having such a hard time figuring it out.
I have data say
x = [1 2 3 4 5 6 7 8]
y = [3 4 5 6 7 8 9 10]
What I need is to plot it, then choose four points (say x=2, y= 4 and x= 4, y= 6 etc)
and make a fit through only these 4 points. The fit should be of the form x**-n, where n is a real number.
I can then play around with n, to find the best fit, once I can get x,y and this fit with 4 selected points on a same graph.
All help is appreciated, thank you!
답변 (2개)
Michael Soskind
2020년 4월 20일
Hi Steve,
With regards to curve fitting, when you choose the points, then you can fit it to any function using lsqcurvefit, since you know x and y.
Here is an example similar to the one you describe above. I select the values you described, and show the result in a plot:
x = [1 2 3 4 5 6 7 8];
y = [3 4 5 6 7 8 9 10];
range = [2,3,4,7];
xdata = x(range);
ydata = y(range);
fun = @(c, xdata) c(1)*xdata + c(2);
x0 = [1.2,1.8]
coeffs = lsqcurvefit(fun, x0, xdata, ydata);
figure(); hold on;
plot(xdata, ydata, 'o');
plot(xdata, fun(coeffs, xdata))

I hope that helps, and is similar to what you are looking for,
Michael
댓글 수: 1
Michael Soskind
2020년 4월 20일
I forgot to mention, the function definition is important, c is an array of the coefficients, and since I use a linear function, I have a slope term and offset term. In your case, you can scale the polynomial and have the power terms selected by the curve fitting routine.
fun = @(c, xdata) c(1)*xdata.^c(2);
Ameer Hamza
2020년 4월 20일
You can use the curve fitting toolbox to find the fit of the form you described in the question
x = [1 2 3 4 5 6 7 8];
y = [3 4 5 6 7 8 9 10];
ft = fittype('x^-n');
fit_model = fit(x([2 4]).',y([2 4]).',ft);
plot(x, y, '+', x, fit_model(x))
you can check the value of n from 'fit_model'
>> fit_model
fit_model =
General model:
fit_model(x) = x^-n
Coefficients (with 95% confidence bounds):
n = -1.327 (-3.497, 0.8423)
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!