MATLAB Answers


Derivative constraint in curve fitting

Asked by Ane Følgesvold Reines on 31 Oct 2019
Latest activity Commented on by Ane Følgesvold Reines on 1 Nov 2019
I have a set of data points in 2D that I want to use fit(x,y,'modeltype') function to test the curve fit of different types of functions. I have tried Fourier series, polynomial, two-term exponential and two-term power functions (one on the increasing and one on the decresing interval). I have two constraints that I want to implement but I dont know how. It is the value and the first derivative in one point. I want the following to hold (the data points are somewhat like an U upside down):
f(1)=1, df/dx(1)=0
How do I implement these connstraints? For these to hold (or be as close to 1 and 0 as possible) is more important than the curve to match all the other data points.
Thank you in advance!


on 31 Oct 2019
You can add two points at the beginning. Derivative means df/dx = tan(a) (tangens of an angle)
I dont know if I managed to make my problem clear. I have a lot of data points, one of them being in (1,1). The curve I want to fit (either it is power series, Fourier, polynomial, etc) must go through this point. Also in that point (1,1) i want its first derivative to be 0, i.e. this should be the curve's global maxima. Those two constraints must be fulfilled by the curve I fit.

Sign in to comment.

2 Answers

Answer by Matt J
on 31 Oct 2019

You can do spline fits with those kinds of constraints using this,


Thank you, sounds like something I should look into.
But I have the issue that it is very important (for further modelling purposes) that the resulting curve is given by one or maximum two different expressions on its range, and that it must have continous first derivatives on its entire range (from x=0 to x=1,5). This is why I thought e.g. polynomials, they are easy to find the derivative. Splines are different curves on each interval, am I right?
Matt J
on 31 Oct 2019
A spline of order 2 or higher will have continuous first derivatives and the SLM toolbox provides tools for evaluating them.
But there will still be loads of different polynomials (one for each interval right?)? I dont doubt that I will be able to use them for my purpose with code, but it shall be implemented into some analytical mathematical expressions where I need to express the derivative of the function at an arbitrary point on the range considered. This seems very unpractical if I have many spline polynomials...
But thank you anyways. I will look more into it just in case it might work

Sign in to comment.

Answer by Cyrus Tirband on 31 Oct 2019

If you absolutely have to make sure your constraints are met, you have to change your fitting equation so that all possible solutions satisfy your constraints. Consider the 2nd degree polynomial:
if the constraints are y'(1) = 0, and y(1) = 1; we get
Your fitting equation then becomes
Which will give shitty results since it only has one degree of freedom. But this is just an example, if you start with a 4th degree polynomial, your fitting equation will have three degrees of freedom. The fit function will then take care of the rest and minimize the least squares cost.


Yeah I guess I can do it mathematically, was just hoping somehow Matlab had an inbuildt function for the constraints.
So far it seems that 8th degree polynomial is the best polynomial for my data.
Matlab does not have this functionality.

Sign in to comment.