Curve fitting with custom constraints
Show older comments
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 Comments
Torsten
on 28 Mar 2018
Yes, by using fmincon which allows constraints to be set.
Simon Schraml
on 29 Mar 2018
Edited: Simon Schraml
on 29 Mar 2018
Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!