How to put a fit condition such that the fitted curve is not above the fitted data (Fit<Fitted Data)?

7 views (last 30 days)
Hello Matlab community,
I am a bit stuggling to understand if there is an easy way to implement the condition above! I am using the fittype function:
aFittype = fittype(expression)
and fitting an easy exponential decay. I tried using a weighting factor, to add a constraint on the fit, but it is not enough.
For physical reason, the exponential decay fitted would make more sense if its values would less than the fitted data. I am then subtracting this as a background.
Thank you for the suggestions!

Accepted Answer

Torsten
Torsten on 27 Jan 2023
Moved: Torsten on 27 Jan 2023
In each measurement point, add the constraint
y_measurement >= fun(x_measurement)
where "fun" is your exponential curve.
You will have to use "fmincon" instead of "fit" to incorporate the constraints either in matrix A and vector B or in function "nonlcon".
  4 Comments
Torsten
Torsten on 8 Feb 2023
fun = @(p) sum( ( p(1)*exp(-x/(8.618e-5*p(2))) - y ).^2 )
instead of
fun = @(p) sum((p(1)*exp(-x/(8.618e-5.*p(2))))-y)^2
and
c = difference;
ceq = [];
instead of
c = [];
ceq = sum(difference > 0);

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!