RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • coneprog
    • fminbnd
    • fminunc
    • fsolve
    • fzero
    • integral
    • linprog
    • lsqcurvefit
    • lsqnonlin
    • optimoptions
    • optimset
    • quad
    • secondordercone

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
funAnyYes—Model callback evaluated as fun(x,xdata).
x0NumericArrayYes—Initial parameter guess.
xdataAnyYes—Independent data passed to the model callback.
ydataNumericArrayYes—Observed response data.
lbNumericArrayNo[]Lower parameter bounds. Empty means unbounded.
ubNumericArrayNo[]Upper parameter bounds. Empty means unbounded.
optionsAnyNo—Options 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.
  • The documented numeric solver domain is floating point. Native integer x0, xdata, ydata, bounds, numeric options, and callback results are independently gated RunMat extensions.
  • Integer x0, ydata, bounds, floating options, and callback results must be exactly representable at the double solver boundary. Integer xdata remains an exact callback payload and is not eagerly converted.
  • 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?⌄

RunMat currently supports the core curve-fitting forms with optional lower and upper parameter bounds. Linear-constraint, nonlinear-constraint, and problem-structure forms are not yet implemented.

Related Math functions

Optim

coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqnonlin · optimoptions · optimset · quad · secondordercone

Elementwise

abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · typecast · uint16 · uint32 · uint8

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh

Reduction

all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var

Structure

bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm

Signal

blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

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

Ops

cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftn · fftshift · ifft · ifft2 · ifftn · ifftshift

Interpolation

griddedInterpolant · interp1 · interp1q · interp2 · pchip · ppval · spline

Discrete

lcm · primes

Ode

ode15s · ode23 · ode45

Poly

polyder · polyfit · polyint · polyval · roots

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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How lsqcurvefit works
  • Examples
  • Fit a line
  • Fit an exponential decay
  • Fit with bounds and diagnostics
  • Using lsqcurvefit with coding agents
  • FAQ
  • Related Math functions
  • Optim
  • Elementwise
  • Trigonometry
  • Reduction
  • Structure
  • Signal
  • Rounding
  • Factor
  • Solve
  • Ops
  • Symbolic
  • Fft
  • Interpolation
  • Discrete
  • Ode
  • Poly
  • Open-source implementation
  • About RunMat