
fit a curve through selected points with own function
52 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (2)
Michael Soskind
am 20 Apr. 2020
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 Kommentar
Michael Soskind
am 20 Apr. 2020
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
am 20 Apr. 2020
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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!