fitting an equation to the curve
2 次查看(过去 30 天)
显示 更早的评论
I have set of data X,Y and i would like to fit an equation of the form

I want to find for which n, the curev fits the best.
Any help!
9 个评论
Rik
2018-11-15
You really shouldn't shadow the error function with a variable. Once you rename it, this code should work. I tried implementing fminsearch, but it is difficult to find a good way to select only discrete results.
回答(3 个)
KSSV
2018-11-15
Read about polyfit
N = 100 ; n = 2 ;
x = linspace(0,2*pi,N) ;
y = sin(x).^n ;
%
p = polyfit(x,y,7); % you can change 7 here...
x1 = linspace(0,2*pi);
y1 = polyval(p,x1);
figure
plot(x,y,'o')
hold on
plot(x1,y1)
hold off
madhan ravi
2018-11-15
编辑:madhan ravi
2018-11-15
x = linspace(-2*pi,2*pi,100) ;
xx=linspace(x(1),x(end),1000);
y = sin(x).^7 ;
yy=interp1(x,y,x,'spline')
figure
plot(x,y,'o',x,yy,'r')
Image Analyst
2018-11-15
My favorite fitting function is fitnlm() in the Statistics and Machine Learning toolbox. It could easily find this n. Just attach your data in a .mat file and I can do it for you. You have a non-linear equation in variable n. You could turn it into a linear equation in n by taking the log of the data and then using polyfit, but do you know how that affects the accuracy? Why not use fitnlm() to fit a non-linear model to n right from the start?
4 个评论
Rik
2018-11-15
Ah, yes, you are right. But as I already mentioned, it doesn't matter. The exponent should be an integer, so this will be a real value for any valid n.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!