lsqcurvefit — Fit nonlinear parametric curves in the least-squares sense.
x = lsqcurvefit(fun, x0, xdata, ydata) estimates parameters x so that fun(x, xdata) matches ydata in the least-squares sense. RunMat preserves the shape of x0, supports scalar and vector parameters, accepts lower and upper parameter bounds, and can return solver diagnostics through MATLAB-compatible multi-output forms.
Syntax
x = lsqcurvefit(fun, x0, xdata, ydata)
x = lsqcurvefit(fun, x0, xdata, ydata, lb, ub)
x = lsqcurvefit(fun, x0, xdata, ydata, lb, ub, options)
[x, resnorm] = lsqcurvefit(___)
[x, resnorm, residual] = lsqcurvefit(___)
[x, resnorm, residual, exitflag] = lsqcurvefit(___)All supported lsqcurvefit forms
x = lsqcurvefit(fun, x0, xdata, ydata)
x = lsqcurvefit(fun, x0, xdata, ydata, lb, ub)
x = lsqcurvefit(fun, x0, xdata, ydata, lb, ub, options)
[x, resnorm] = lsqcurvefit(___)
[x, resnorm, residual] = lsqcurvefit(___)
[x, resnorm, residual, exitflag] = lsqcurvefit(___)
[x, resnorm, residual, exitflag, output] = lsqcurvefit(___)
[x, resnorm, residual, exitflag, output, lambda] = lsqcurvefit(___)
[x, resnorm, residual, exitflag, output, lambda, jacobian] = lsqcurvefit(___)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fun | Any | Yes | — | Model callback evaluated as fun(x,xdata). |
x0 | NumericArray | Yes | — | Initial parameter guess. |
xdata | Any | Yes | — | Independent data passed to the model callback. |
ydata | NumericArray | Yes | — | Observed response data. |
lb | NumericArray | No | [] | Lower parameter bounds. Empty means unbounded. |
ub | NumericArray | No | [] | Upper parameter bounds. Empty means unbounded. |
options | Any | No | — | Options struct from optimset or optimoptions. |
Returns
| Name | Type | Description |
|---|---|---|
x | NumericArray | Estimated fit parameters with the same shape as x0. |
resnorm | NumericScalar | Squared 2-norm of the final residual. |
residual | NumericArray | Final model residual fun(x,xdata)-ydata. |
exitflag | NumericScalar | Solver exit condition. |
output | Any | Iteration and convergence metadata struct. |
lambda | Any | Approximate bound multiplier struct with lower and upper fields. |
jacobian | NumericArray | Finite-difference Jacobian of fun at the solution. |
Returned values from lsqcurvefit depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:lsqcurvefit:InvalidArgument | Argument grammar, bounds, options, or output arity are invalid. | lsqcurvefit: invalid argument |
RunMat:lsqcurvefit:InvalidInput | Initial guess, model callback, data shape, or solver semantics are invalid. | lsqcurvefit: invalid input |
RunMat:lsqcurvefit:TooManyOutputs | `lsqcurvefit` is called with more than seven requested outputs. | lsqcurvefit: too many output arguments |
How lsqcurvefit works
- The model callback is evaluated as
fun(x, xdata)and must return a finite real scalar or array with the same shape asydata. - Unbounded problems use finite-difference Levenberg-Marquardt iterations.
- Bounded problems project trial points into
[lb, ub]and estimate finite-difference Jacobians without stepping outside finite bounds. lbandubmay be empty, scalar, or arrays with the same number of elements asx0; scalar bounds are broadcast to every parameter.optimsetandoptimoptionsoption structs are accepted.TolX,TolFun,MaxIter,MaxFunEvals,Display, andAlgorithmare recognized.Algorithmacceptslevenberg-marquardtandtrust-region-reflective; both currently use RunMat's projected Levenberg-Marquardt implementation.- With requested outputs, RunMat supports
[x, resnorm, residual, exitflag, output, lambda, jacobian].
Examples
Fit a line
xdata = [0 1 2 3];
ydata = 2.*xdata + 1;
p = lsqcurvefit(@(p,x) p(1).*x + p(2), [0; 0], xdata, ydata)Expected output:
p =
2.0000
1.0000Fit an exponential decay
xdata = 0:0.2:2;
ydata = 2.5.*exp(-0.7.*xdata);
p = lsqcurvefit(@(p,x) p(1).*exp(-p(2).*x), [1; 0.1], xdata, ydata)Expected output:
p =
2.5000
0.7000Fit with bounds and diagnostics
opts = optimoptions("lsqcurvefit", "TolX", 1e-8, "Display", "off");
[p, resnorm, residual, exitflag, output] = lsqcurvefit(@(p,x) p(1).*x, 0.5, [1 2 3], [2 4 6], 0, 1, opts)Expected output:
p =
1.0000Using lsqcurvefit with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how lsqcurvefit changes the result.
Run a small lsqcurvefit example, explain the result, then change one input and compare the output.
FAQ
Does lsqcurvefit run on the GPU?⌄
The solver itself is host-side because it repeatedly invokes user callbacks and adapts finite-difference steps. The callback can still use GPU-aware builtins, but the residual vector is gathered before each solver step.
What is returned in lambda?⌄
lambda.lower and lambda.upper are approximate bound multipliers computed from the final finite-difference Jacobian and residual.
Are nonlinear constraints supported?⌄
No. The MATLAB-compatible curve-fitting forms with optional lower and upper parameter bounds are supported.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lsqcurvefit is executed, line by line, in Rust.
- View the source for lsqcurvefit in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.