C#
non-linear regression
data analysis
machine learning
programming

Non-linear regression in C

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Non-linear regression fits data to a model where the parameters do not appear in a simple linear way. In C sharp, the practical approach is usually to use a numerical library rather than implementing an optimizer from scratch. The real work is choosing a model function, preparing good initial guesses, and validating whether the fit is meaningful.

Understand What Makes It Non-Linear

Linear regression is linear in the parameters, even if the input variables are transformed. Non-linear regression means the parameters themselves sit inside a non-linear expression.

Example model:

y = a * exp(b * x)

The parameter b appears inside the exponential, so this is a non-linear fit problem.

Use Math.NET Numerics for Curve Fitting

Math.NET Numerics is a practical choice in C sharp for this kind of work.

Example fit of an exponential curve:

csharp
1using System;
2using MathNet.Numerics;
3
4class Program
5{
6    static void Main()
7    {
8        double[] x = { 0, 1, 2, 3, 4 };
9        double[] y = { 2.0, 2.7, 3.8, 5.5, 8.1 };
10
11        Func<double, double[], double> model = (t, p) => p[0] * Math.Exp(p[1] * t);
12        double[] initialGuess = { 2.0, 0.3 };
13
14        double[] parameters = Fit.Curve(x, y, model, initialGuess);
15
16        Console.WriteLine($"a = {parameters[0]}");
17        Console.WriteLine($"b = {parameters[1]}");
18    }
19}

This gives you an estimated parameter vector based on the model and initial guess.

Initial Guess Matters

Unlike simple linear regression, non-linear fitting can depend strongly on the starting parameter values.

Bad initial guesses can lead to:

  • slow convergence
  • local minima
  • failure to converge at all

That means domain knowledge matters. A rough but realistic starting point is often more important than minor library settings.

Validate the Fit

A fitted model is not automatically a good model. After fitting, compare predictions with data.

csharp
1for (int i = 0; i < x.Length; i++)
2{
3    double predicted = parameters[0] * Math.Exp(parameters[1] * x[i]);
4    Console.WriteLine($"x={x[i]}, actual={y[i]}, predicted={predicted}");
5}

This simple check often reveals whether the chosen model family was appropriate.

Compute Residual Error

Residuals help quantify fit quality.

csharp
1double sse = 0.0;
2for (int i = 0; i < x.Length; i++)
3{
4    double predicted = parameters[0] * Math.Exp(parameters[1] * x[i]);
5    double residual = y[i] - predicted;
6    sse += residual * residual;
7}
8
9Console.WriteLine($"SSE = {sse}");

Looking only at the parameters without checking residual quality is a common mistake.

Choose the Right Model Form

The hard part of non-linear regression is often not the library call, but model selection. Common patterns include:

  • exponential growth or decay
  • logistic curves
  • power laws
  • saturation models

If the model form is wrong, the optimizer can still produce numbers, but those numbers may not mean anything useful.

Watch Numerical Stability

Some models are sensitive to scale. If inputs or outputs vary over large ranges, normalize data or reformulate the model before fitting.

Numerical instability may show up as:

  • huge parameter values
  • overflow in exponentials
  • poor convergence from reasonable guesses

That is usually a data-scaling issue before it is a library issue.

When to Implement Your Own Optimizer

Usually, do not. Writing a correct non-linear optimizer is far more work than most applications need. Only build one yourself if:

  • you have specialized constraints
  • you need a custom objective function outside standard libraries
  • you are doing research or educational work

For ordinary engineering use, use a library and spend your energy on data quality and model selection.

Common Pitfalls

  • Treating non-linear regression as just a different syntax for linear regression.
  • Using unrealistic initial parameter guesses.
  • Accepting fitted parameters without checking residuals or predictions.
  • Choosing a model family that does not match the data shape.
  • Blaming the library when the real issue is scaling or model design.

Summary

  • Non-linear regression fits models where parameters enter non-linearly.
  • In C sharp, a numerical library such as Math.NET Numerics is the practical path.
  • Good initial guesses matter a lot for convergence.
  • Validate predictions and residuals after every fit.
  • Focus on model selection and data conditioning more than reinventing the optimizer.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.