# Fitting Data to a Non-Linear Curve

1 view (last 30 days)
Andrew Rowe on 8 Dec 2019
Answered: Alex Sha on 9 Dec 2019
I have matrix data for x and y and am looking to find an equation that best represents this data. We don't believe it is a polynomial line of best fit so polyfit wouldn't be appropriate. Is there a method for finding the equation for logarithmic or exponential through data?
Here's the data for reference:
x=[0 1 2 3 4 5 6 7 8 9 10]
y=[.0242 0.1940 0.2792 0.2358 0.1386 0.0598 0.0238 0.0090 0.0034 0.0013 0.002]
Image Analyst on 8 Dec 2019
Looks like it could either be log-normal, or the sum of two normals. Is there some knowledge you have about the physics of the situation that might lead you to prefer one or the other, or even some other equation? What physical process gave rise to these measurements?
x=[0 1 2 3 4 5 6 7 8 9 10]
y=[.0242 0.1940 0.2792 0.2358 0.1386 0.0598 0.0238 0.0090 0.0034 0.0013 0.002]
plot(x, y, 'bs-');
grid on;
I can fit the data to either equation with fitnlm() though with only 10 data points, it will be difficult to definitively state that one is better than the other because the sum of the residuals is less. You'd need many more data points to be more certain which is better.

Star Strider on 8 Dec 2019
One possibility:
x=[0 1 2 3 4 5 6 7 8 9 10];
y=[.0242 0.1940 0.2792 0.2358 0.1386 0.0598 0.0238 0.0090 0.0034 0.0013 0.002];
objfcn = @(b,x) b(1).*x.*exp(b(2).*x);
B0 = [1; -1];
[B,rsdnrm] = fminsearch(@(b) norm(y - objfcn(b,x)), B0);
xv = linspace(min(x), max(x));
figure
plot(x, y, 'pg')
hold on
plot(xv, objfcn(B,xv), '-r')
hold off
grid
text(4.5, 0.22, sprintf('$y(x) = %.3f \\cdot x \\cdot e^{%+0.3f \\cdot x}$',B), 'Interpreter','latex')
xlabel('x')
ylabel('y(x)')
The most accurate model would be one that models the process that created your data.
This model produces:

Alex Sha on 9 Dec 2019
If don't care the function type, the follow is one:
y = p1*(p2^x)*(x+p3)^p4;
Root of Mean Square Error (RMSE): 0.0032240913289773
Sum of Squared Residual: 0.000114342413873453
Correlation Coef. (R): 0.999488278914727
R-Square: 0.998976819687923
Determination Coef. (DC): 0.998955740196223
Chi-Square: 0.00189307571492163
F-Statistic: 2218.55228360604
Parameter Best Estimate
---------- -------------
p1 0.0382808254972641
p2 0.190470576993248
p3 0.932820554835385
p4 4.94812019020198