# Fitting a nonlinear curve to a small dataset

1 view (last 30 days)
Imran on 15 Sep 2020
Commented: Imran on 17 Sep 2020
My data is
Data = ...
[2.5 -14.741408
3.0 -14.765364
4.0 -15.854609
5.0 -16.058246
6.0 -16.103032
7.0 -16.595257];
and looks like this I want to fit a single curve to this and get the equation of that curve. How may I do this?

Image Analyst on 15 Sep 2020
Edited: Image Analyst on 15 Sep 2020
Any idea of what curve you want to fit it to? Like a polynomial, or an exponential decay (demo attached), or something else?
That said, the formula you get, whatever it is, will be virtually worthless in it's predicting ability of points not in your training set. I mean with so few and so noisy data, whatever parameters you come up with could be vastly different with a different training set. You need to get a lot more points. For example if I put in 3.5, I could get almost anything between -15 and -15.6 depending on the formula. In other words, you train with that set and you might get -15, but then you take some more measurements that are nominally the same but since there's a high amount of noise you'd get a different formula and now you might get -15.3 or -15.6. You couldn't really trust the prediction. Again, get more points!
Test4.m is the attached demo with your data plugged in, and it gives this: Imran on 16 Sep 2020
I didn't try the rate equation and the Gaussian demo. I tried others but the results were not that good. I thought to knock you around 3.00 am GMT.
Till now, to my knowledge, there is no theory developed relating the x values and y values which are from my work on a biosensor. I guessed that the data might follow tanh curve after visualizing them in the plot. Also I have some test data, from which I thought a tanh curve might work well for the range 2.5-6.0.
Is it possible to have a downward move of the curve at 6.0? That is from 6.0 to 7.0, it will have a behavior similar in the range 3.0-4.0
Image Analyst on 16 Sep 2020
The red curve is the best fit possible. If you lower it, which you can do by just subtracting some value from the coefficients(4) value (this is the vertical offset), then it will no longer be the best possible fit.
Of course you can lower the blue curve if you want by subtracting a value from all of your training points' y values, but I don't know the point of that. In fact I don't know why you want to lower either curve. Your measurements are what they are - why change them? And the best fit is just that - the best fit given the training data you provided. Why change that? If you want you can change your model from tanh to something else. You could even use splines for an empirical (numeric) fit (no equation but you just plug your x value into spline() instead of some formula from fitnlm() so it's pretty much the same from an end user perspective.
Imran on 17 Sep 2020
Hmm, I understand that I neither can change my training set nor the best fit curve. You have helped a lot. Thank you.

esat gulhan on 15 Sep 2020
x=[2.5 3.0 4.0 5.0 6.0 7.0];
y=[-14.741408 -14.765364 -15.854609 -16.058246 -16.103032 -16.595257];
s=pchip(x,y) %you can use pchip or cape instead of pchip
xx=linspace(2.5,7,100);yy=ppval(s,xx)
plot(xx,yy,'LineWidth',1.5);grid on;hold on;plot(x,y,'o')
ppval(s,3) %if you want to know y when x 3 this code works, if you want y when x 5 you should enter 5
ppval(s,5) %if you want y when x 5 you should enter 5 like that

Imran on 15 Sep 2020
How can I get the function that fits the data?
esat gulhan on 15 Sep 2020
You can get a function from Cfit data tool
a0 = -15.79
a1 = -0.6927
b1 = 0.07262
a2 = 0.2647
b2 = -0.3676
w = 1.016
y= a0 + a1*cos(x*w) + b1*sin(x*w) + a2*cos(2*x*w) + b2*sin(2*x*w)
You can get a fourier function like that. Pchip is better to use
esat gulhan on 15 Sep 2020

R2015a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!