i'm trying fit data sum of sines function in matlab, however, number of terms of sine function in matlab limited,i.e. 1 ≤ n ≤ 8. however, want more terms in fit functions, i.e. on 50 term. there anyway make matlab fit data sum of sine function on 8 sinusoidal terms? why there such constraint in matlab (is technically or arbitrary)? there toolbox fit sinusoidal function (especially capable of supporting wieghted data)?
>f = fit(x,y, 'sin10') >error using fittype>icreatefromlibrary (line 412) >library function sin10 not found.
it o.k 'sin8' or 'sin9' parameters.
i appreciate answer.
i'v found solution question accidentally, while browsing matlab help. post answer in hope of helping people have same problem.
as first shot solve , tried 'fit' instruction. reasons, customized 'fit' based fitting code below, didn't workout:
fitoptions = fitoptions('method','nonlinearleastsquares', 'algorithm', 'trust-region', 'maxiter'); fittype = fittype('a*sin(1*f) + b*sin(2*f) + c*sin(3*f) + d*sin(4*f) + e*sin(5*f) + g*sin(6*f) + h*sin(7*f) + k*sin(8*f) + l*sin(9*f) + m*sin(10*f) + n*sin(11*f)', 'independent', 'f'); [fittedmodel, gof] = fit(freq, data, fittype) % `in above code, phase parameters not included, might added.
what found using 'lsqcurvefit' instruction optimization toolbox, customized function fitting more feasible , easier 'fit' function. tested fit data sum of 12 (>8) sines in below code:
clear;clc xdata=1:0.1:10; % x or independant data ydata=sin(xdata+0.2)+0.5*sin(0.3*xdata+0.3)+ 2*sin( 0.2*xdata+23 )+... 0.7*sin( 0.34*xdata+12 )+.76*sin( .23*xdata+.3 )+.98*sin(.76 *xdata+.56 )+... +.34*sin( .87*xdata+.123 )+.234*sin(.234 *xdata+23 ); % y or dependant data x0 = randn(36,1); % initial guess fun = @(x,xdata)x(1)*sin(x(2)*xdata+x(3))+... x(4)*sin(x(5)*xdata+x(6))+... x(7)*sin(x(8)*xdata+x(9))+... x(10)*sin(x(11)*xdata+x(12))+... x(13)*sin(x(14)*xdata+x(15))+... x(16)*sin(x(17)*xdata+x(18))+... x(19)*sin(x(20)*xdata+x(21))+... x(22)*sin(x(23)*xdata+x(24))+... x(25)*sin(x(26)*xdata+x(27))+... x(28)*sin(x(29)*xdata+x(30))+... x(31)*sin(x(32)*xdata+x(33))+... x(34)*sin(x(35)*xdata+x(36)); % goal function sum of 12 sines options = optimoptions('lsqcurvefit','algorithm','trust-region-reflective');% options fitting x=lsqcurvefit(fun,x0,xdata,ydata) % main instruction times = linspace(xdata(1),xdata(end)); plot(xdata,ydata,'ko',times,fun(x,times),'r-') legend('data','fitted sum of 12 sines') title('data , fitted curve')
the results satisfactory (till now), shown in below:
Comments
Post a Comment