How to force Matlab to use a positive coefficient rather than negative for a custom fit equation

I am working on a script that fits 2 different custom equations to 2 sets of data, it has one coefficient in each one to be determined by Matlab.
The problem is that for one of the fits Matlab has given me the correct magnitude for the coefficient it is working out but the wrong sign. This results in the fit being inverted vertically compared to the data. The shape of the fit is correct but it's just the wrong way round.
I have tried to 'trick' Matlab in to returning a positive coefficient by inverting the data but every time the fit gets close to matching the data Matlab alters the sign of the coefficient and inverts it again.
I have tried to set the bounds of the coefficient to be from 0 to 1 but this just returns a straight line for both fits by making each coefficient 0. The coefficients for each fit are on the order of 10^-12 and 10^-18 so they are very small. The code used to make the fits are shown below with FitOP's coefficient being BETA and FitCl's being n2.
%Fit types
FitOp = fittype( '1-(BETA*(20*10.^-6)*0.001/(pi*1000*(100*(10.^-15))*2*(2^0.5)*((26*(10.^-6))^2)*(1+(((1.55*(10.^-6))*x/(pi*((26*(10.^-6))^2)))^2))))', 'independent', 'x', 'dependent', 'y' );
FitCl = fittype( '1.0739+((4*((1.55*(10.^-6))*x/(pi*((26*(10.^-6))^2)))*(2*pi*0.001*n2*(20*10.^-6)/(pi*1000*(100*(10.^-15))*((26*(10.^-6))^2)*(1.55*(10.^-6))*(2^0.5)))*((1-0.5)^(0.25)))/((1+(((1.55*(10.^-6))*x/(pi*((26*(10.^-6))^2)))^2))*(9+(((1.55*(10.^-6))*x/(pi*((26*(10.^-6))^2)))^2))))', 'independent', 'x', 'dependent', 'y' );
%Fit Options
opts = fitoptions( 'Method', 'nonlinearLeastSquares');
opts.Display = 'Off';
opts.StartPoint = 0;
opts.TolFun = 1e-20;
opts.TolX = 1e-20;
%Fit Creations
[TOpFit, gofOp] = fit( x, oi, FitOp, opts );
[TClFit, gofCl] = fit( x, cioi, FitCl, opts);
This fit works and is almost ready to use for my data.
This fit is not working, it returns the correct value of BETA but it is negative rather than positive. I have tried to make the data it fits the equation to negative but Matlab just forces the BETA coefficient to be positive, making the fit vertically opposite to the data again.
I have tried to limit the coefficient for BETA from 0 to 1 by using the 'lower' and 'upper' bounds for the fit options. This results in both n2 and BETA being set to 0. The value Matlab gives for BETA is -1.2382e-12 (correct value but should be positive) and the value it gives for n2 is 2.5075e-18 (correct value and sign).
I am looking for a way to force Matlab to use the correct signage for BETA or even a way to get the goodness of fit for the FitOp equation with the BETA value inserted directly in so that I can compare an equation with no unknowns to the data. I know that the BETA value it returns is correct, just the sign of the value is wrong and I cannot see a way to force Matlab to use the correct sign.

Answers (0)

Categories

Asked:

on 25 Jan 2018

Edited:

on 25 Jan 2018

Community Treasure Hunt

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

Start Hunting!