MATLAB: Fourier’s series fitting problem

curve fittingCurve Fitting ToolboxMATLABtrigonometric fitting

Hi,
I have a set of data and I need to calculate the trigonometric curve by using the "curve fitting tool" of Matlab.
The curve is computed regularly and the plot on the tool fit all the point, but the function doesn't fit anyone point of the set, it seems out of scale.
Thanks in advance.
Point set
x1= [
0.072553;
0.773202;
1.378368;
1.788931;
2.450787;
3.010140;
3.447557;
3.837764;
4.187158;
4.460306;
4.635640;
4.712404;
4.719292;
4.719436]
z1 =[
4.854458;
4.770619;
4.706262;
4.620619;
4.270619;
3.770619;
3.270619;
2.770619;
2.270619;
1.770619;
1.270619;
0.770619;
0.268003;
0.000000]
the output of the Fourier's series is:
General model Fourier2:
f(x) = a0 + a1*cos(x*w) + b1*sin(x*w) + a2*cos(2*x*w) + b2*sin(2*x*w)
Coefficients (with 95% confidence bounds):
a0 = -5.229e+07 (-4.405e+12, 4.405e+12)
a1 = 6.971e+07 (-5.872e+12, 5.872e+12)
b1 = 1.143e+06 (-7.223e+10, 7.223e+10)
a2 = -1.742e+07 (-1.468e+12, 1.468e+12)
b2 = -5.716e+05 (-3.611e+10, 3.611e+10)
w = 0.008349 (-175.8, 175.8)
Goodness of fit:
SSE: 0.7653
R-square: 0.9811
Adjusted R-square: 0.9692
RMSE: 0.3093

Best Answer

  • Try this code with lsqcurvefit()
    x1= [
    0.072553;
    0.773202;
    1.378368;
    1.788931;
    2.450787;
    3.010140;
    3.447557;
    3.837764;
    4.187158;
    4.460306;
    4.635640;
    4.712404;
    4.719292;
    4.719436];
    z1 =[
    4.854458;
    4.770619;
    4.706262;
    4.620619;
    4.270619;
    3.770619;
    3.270619;
    2.770619;
    2.270619;
    1.770619;
    1.270619;
    0.770619;
    0.268003;
    0.000000];
    fun = @(a0,a1,a2,b1,b2,w,x) a0 + a1*cos(x*w) + b1*sin(x*w) + a2*cos(2*x*w) + b2*sin(2*x*w);
    opts = optimoptions('lsqcurvefit', 'MaxFunctionEvaluations', 50000, ...
    'MaxIterations', 10000, 'FunctionTolerance', 1e-7);
    p = lsqcurvefit(@(p, x) fun(p(1),p(2),p(3),p(4),p(5),p(6),x),rand(1,6), x1, z1, [], [], opts);
    plot(x1, z1, 'b+')
    hold on
    plot(x1, fun(p(1),p(2),p(3),p(4),p(5),p(6),x1), 'm-');