Curve fitting with custom constraints

Hey :) My basic problem is that I would like to describe a function with a varying number of minimum and maximum values based on only these values. The function describes the variation around a daily average, hence the average should be 1 (100%). I have tried manually constructing a system of equations, defining the integral as well as function values and first derivatives (=0) at the extrema. However, solving for a solution usually gave no result for piecewise cubic polynomials as well as different forms of truncated Fourier Series (I guess no exact solution exists).
Now I thought of using curve fit with a custom fittype of a Fourier Series which assures a mean of 1. Like this, the the constraints don't have to be satisfied exactly but the best solution is found. The problem is,however, that I can not specify the points to be maximum or minimum values of the curve (i.e. derivatives of zero):
clear all
clc
x_ex=[6, 18];
y_ex=[1.4, 0.5];
y_midnight=[0.7];
extrema=length(x_ex);
xval=[0, x_ex , 24]';
yval=[y_midnight, y_ex, y_midnight]';
f=fittype('1 + a1*cos(x*2*pi/24) + b1*sin(x*2*pi/24) + a2*cos(2*x*2*pi/24) + b2*sin(2*x*2*pi/24)','independent','x','dependent','y','coefficients',{'a1','a2','b1','b2'})
fun=fit(xval,yval,f)
plot(fun,xval,yval)
y=feval(fun,0:0.01:24);
avg=mean(y)
Is there a possibility to additionally specify constraints like having a derivative of zero at the specific values of x or specifying a definite integral? So far I've only found the possibility to restrict upper and lower bounds for the coefficients, but my constraints are of different nature.
If anyone knows a better solution I am of course open to ideas.
Thank you very much!

3 comentarios

Torsten
Torsten el 28 de Mzo. de 2018
Yes, by using fmincon which allows constraints to be set.
Simon Schraml
Simon Schraml el 29 de Mzo. de 2018
Editada: Simon Schraml el 29 de Mzo. de 2018
Hi Torsten and thank you for your reply!
I am just looking into fmincon and seems like what i might need. However I am a little confused as to how I get my constraints in there. As I understand you can set the equality and inequality constraints by directly constraining the values coefficients in x(which in my case could be polynomial coefficients or those of fourier series etc.). But how would I be able to specify constraints like
1) The value of the polynomial (or Fourier Series) should never be negative (inequality)
2) The derivative at a certain point should be equal to zero (equality)
3) The definite integral from 0 to 1 should be exactly 1 (equality)
My function to fit can e.g. look sth. like this:
1+x(1,1).*sin(w.*t+x(1,2))+x(2,1).*sin(2.*w.*t+x(2,2))+x(3,1).*sin(3.*w.*t+x(3,2))
where t is the variable time, w (omega) is known and x(1,1) through x(3,2) are my coefficients
Appreciate the tips! :)
Torsten
Torsten el 29 de Mzo. de 2018
Editada: Torsten el 29 de Mzo. de 2018
Use "nonlcon" inside "fmincon". Here you can define every constraint that can be derived from the solution vector x.
Best wishes
Torsten.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mzo. de 2018

Editada:

el 29 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by