RunMat
GitHub

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

NameTypeRequiredDefaultDescription
funAnyYesModel callback evaluated as fun(x,xdata).
x0NumericArrayYesInitial parameter guess.
xdataAnyYesIndependent data passed to the model callback.
ydataNumericArrayYesObserved response data.
lbNumericArrayNo[]Lower parameter bounds. Empty means unbounded.
ubNumericArrayNo[]Upper parameter bounds. Empty means unbounded.
optionsAnyNoOptions struct from optimset or optimoptions.

Returns

NameTypeDescription
xNumericArrayEstimated fit parameters with the same shape as x0.
resnormNumericScalarSquared 2-norm of the final residual.
residualNumericArrayFinal model residual fun(x,xdata)-ydata.
exitflagNumericScalarSolver exit condition.
outputAnyIteration and convergence metadata struct.
lambdaAnyApproximate bound multiplier struct with lower and upper fields.
jacobianNumericArrayFinite-difference Jacobian of fun at the solution.

Returned values from lsqcurvefit depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:lsqcurvefit:InvalidArgumentArgument grammar, bounds, options, or output arity are invalid.lsqcurvefit: invalid argument
RunMat:lsqcurvefit:InvalidInputInitial 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 as ydata.
  • 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.
  • lb and ub may be empty, scalar, or arrays with the same number of elements as x0; scalar bounds are broadcast to every parameter.
  • optimset and optimoptions option structs are accepted. TolX, TolFun, MaxIter, MaxFunEvals, Display, and Algorithm are recognized.
  • Algorithm accepts levenberg-marquardt and trust-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.0000

Fit 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.7000

Fit 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.0000

Using 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.

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Symbolic

digits · int · limit · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lsqcurvefit is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.