Applying logic to Curve Fitter Output
3 views (last 30 days)
Show older comments
I am trying to use Curve Fitter and generating the code to help teach myself how to process my data without Curve Fitter.
Is there a way to apply a logic that a certain data point should be lower/higher than another certain data point, and have the coefficients adjust accordingly?
%CREATEFIT(TWOSOUSANDTQSPK,TWOSOUSANDTQAPC,TWOSOUSANDTQNM)
% Create a fit.
%
% Data for '2000 copy 1' fit:
% X Input: twosousandTQSPK
% Y Input: twosousandTQAPC
% Z Output: twosousandTQnm
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 18-Jul-2024 20:43:50
%% Fit: '2000 copy 1'.
[xData, yData, zData] = prepareSurfaceData( twosousandTQSPK, twosousandTQAPC, twosousandTQnm );
% Set up fittype and options.
ft = fittype( 'A+(x*B)+(C*x*x)+(y*x*D)+(y*x*x*E)+(y*F)', 'independent', {'x', 'y'}, 'dependent', 'z' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.MaxFunEvals = 60000;
opts.MaxIter = 40000;
opts.StartPoint = [0.0526769976807926 0.737858095516997 0.269119426398556 0.422835615008808 0.547870901214845 0.942736984276934];
opts.TolFun = 0.1;
opts.TolX = 0.1;
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
figure( 'Name', '2000 copy 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, '2000 copy 1', 'twosousandTQnm vs. twosousandTQSPK, twosousandTQAPC', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'twosousandTQSPK', 'Interpreter', 'none' );
ylabel( 'twosousandTQAPC', 'Interpreter', 'none' );
zlabel( 'twosousandTQnm', 'Interpreter', 'none' );
grid off
view( -2.9, 15.3 );
I am looking to apply that the output data should follow the 'twosousandTQSPK' axis relative. I reserached the weighting function, but I I don't know how I could accurately match the data inputs I have to weight it.
This is an example of what I am expecting with a different set of variables, but the results above in question should resemble this outcome to some degree.
%CREATEFIT(SIXTEENTQSPK,SIXTEENTQAPC,SIXTEENTQNM)
% Create a fit.
%
% Data for '1600 copy 2' fit:
% X Input: sixteenTQSPK
% Y Input: sixteenTQAPC
% Z Output: sixteenTQnm
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 18-Jul-2024 20:55:15
%% Fit: '1600 copy 2'.
[xData, yData, zData] = prepareSurfaceData( sixteenTQSPK, sixteenTQAPC, sixteenTQnm );
% Set up fittype and options.
ft = fittype( '(y*A)+B+(x*C)+(D*x*x)+(y*x*E)+(y*x*x*F)', 'independent', {'x', 'y'}, 'dependent', 'z' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.MaxFunEvals = 60000;
opts.MaxIter = 40000;
opts.StartPoint = [0.0430238016578078 0.168990029462704 0.649115474956452 0.73172238565867 0.647745963136307 0.450923706430945];
opts.TolFun = 0.1;
opts.TolX = 0.1;
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
figure( 'Name', '1600 copy 2' );
h = plot( fitresult, [xData, yData], zData );
legend( h, '1600 copy 2', 'sixteenTQnm vs. sixteenTQSPK, sixteenTQAPC', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'sixteenTQSPK', 'Interpreter', 'none' );
ylabel( 'sixteenTQAPC', 'Interpreter', 'none' );
zlabel( 'sixteenTQnm', 'Interpreter', 'none' );
grid off
view( -0.5, -13.0 );
3 Comments
Accepted Answer
Ayush
on 2 Sep 2024
Hi Eric,
I am assming that you are trying to fit a surface to your data using MATLAB and want the fitted model to respect specific logical constraints, such as ensuring one data point is always greater or less than another based on one of the input axes. You're exploring how to achieve this, possibly through weighting or other methods.
Here is an example of how you might achieve that:
constrainedFit();
function constrainedFit()
% Example data
xData = [1, 2, 3, 4];
yData = [1, 2, 3, 4];
zData = [10, 20, 30, 40]; % Example outputs
% Initial guess for parameters
initialParams = [1, 1, 1, 1, 1, 1];
% Define the objective function
objective = @(params) fitError(params, xData, yData, zData);
% Perform constrained optimization
options = optimoptions('fmincon', 'Display', 'iter');
[fittedParams, fval] = fmincon(objective, initialParams, [], [], [], [], [], [], @nonlinearConstraints, options);
% Display results
disp('Fitted Parameters:');
disp(fittedParams);
disp('Objective Function Value:');
disp(fval);
end
function error = fitError(params, xData, yData, zData)
% Define the Model: custom model function that describes the relationship you want to fit
% Example model: z = A + B*x + C*y
A = params(1);
B = params(2);
C = params(3);
predictedZ = A + B*xData + C*yData;
% Calculate error
error = sum((predictedZ - zData).^2);
end
function [c, ceq] = nonlinearConstraints(params)
% Define nonlinear inequality constraints
% Example: ensure z1 > z2 (Add a term like penalty = max(0, z2 - z1)
% to your objective function)
z1 = params(1); % Example output for first data point
z2 = params(2); % Example output for second data point
c = z2 - z1; % Constraint: z1 > z2 implies c < 0
ceq = []; % No equality constraints
end
Hope this was helpful!
0 Comments
More Answers (0)
See Also
Categories
Find more on Interpolation 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!