lsqcurvefit

lsqcurvefit fits a function to a set of points using the Levenberg–Marquardt algorithm.

[b,info,trace] = lsqcurvefit(f,b0,x,y,o)

Inputs:
  • f: function handle
  • o: optimization options struct
Options Description Values Default values
jac Jacobian matrix []
vect Non-vectorized version of the jacobian calculation logical true
lb Lower bounds vector of doubles []
ub Upper bounds vector of doubles []
inc List of parameters to include for minimization vector of logical true(numel(b0), 1)
lambda Dumping factor double 0.01
factor Increase / decrease factor double 1.3
minvar Sum of squared errors minimum variation (convergence towards local minimum criterion) double 1e-40
minsse Target sum of squared errors double 1e-40
maxit Maximum number of iterations positive integer 500
dx Tweak precision for derivatives calculation double 1e-7
nbig Number of random initial guesses positive integer []
display Information display level 'iter' ''
Outputs:
  • b: fitted parameters
  • info: struct object with the following fields:
    • sse0: the initial sums of squared errors
    • sse: the final sums of squared errors
    • nit: number of iterations
  • trace: cell array storing the above outputs at each iteration

Animation

Call optimview('lsqcurvefit', info), being info the second output of lsqcurvefit. Some animation options can be specified appending them to the struct object info.
Options Description Values Default values
plot Type of plot string ''
animate Plots animation logical false
animfreq Frame frequency positive integer 1
animstart Inicial iteration to start animation positive integer 1

Examples

>> x = [1 2 3 4 5 6 7 8 9 10]
y = [0.4 0.8 1.3 1.6 1.9 2.23 3.45 7 9 15]
f = @(x,b) b(1) + b(2)*x + b(3)*x.^2 + b(4)*x.^3 

% define a cubic trendline

rng(0)

% for tractability

[b info] = lsqcurvefit(f, zeros(4,1), x, y) [x:1x10 double] [y:1x10 double] [b:4x1 double] [info:1x1 struct] >> b#

% solution: -1.12867 + 1.86352*x - 0.488665*x.^2 + 0.0461519*x.^3

-1.12867 1.86352 -0.488665 0.0461519 >> info.sse0# 380.535 >> info.sse# 1.55525 >> info.nit# 25 >>

% plot animation

info.plot = 'fit' info.animate = true info.animfreq = 4

% frame frequency

optimview('lsqcurvefit',info) [ans:1x1 struct] 0.516000s

References

[1] Moré, J. J. "The Levenberg-Marquardt Algorithm: Implementation and Theory", Numerical Analysis, 1977.

See also

fminsearch | nsga2