﻿ Euler Math Toolbox - Tutorials

# Non-Linear Fit

We fit a function of the following type to noisy data.

```>function f(x,[a,b,c]) &= a/x^b*sin(c*x);
```

The function

is not easy to fit because of the mix of exponential and periodic terms.

We assume the following real data.

```>vreal = [1.5,0.1,2];
```

This is the following function.

```>plot2d("f(x,vreal)",0.1,5):
```

Now we generate data plus some noise.

```>x=linspace(0.1,5,30); ...
y=f(x,vreal);  ...
seed(0.1); y=y+normal(size(x))*0.2*(max(y)-min(y));
```

For the minimization, we need a function, which computes the error of any given parameter set. Our error norm are the squared sums of the errors.

```>function err(v) := sum((f(x,v)-y)^2);
```

The Nelder or the Powell minimization find the minimal value, if the start point is good enough.

```>vmin=powellmin("err",[1,1,1])
```
```[1.59081,  0.187171,  2.00906]
```

Let us plot the function, the errors and the noisy values in one plot.

```>plot2d(x,y,>points,color=red,style="o");  ...
labelbox(["values","correct","fit"], ...
colors=[red,red,blue],styles=["o","-","-"],points=[1,0,0]):
```

In the following plot, we fix one correct parameter, and vary the others around the correct values.

```>function h(x,y) := err([1,x,y]); ...
plot3d("h",r=1.5,cx=0.1,cy=2,>contour):
```

```>vmin=powellmin("err",[3,1,4])
```
```[0.307071,  0.905458,  3.96991]
```

Let us plot the function, the errors and the noisy values in one plot.

```>plot2d(x,y,>points,color=red,style="o");  ...