Applying logic to Curve Fitter Output

4 次查看(过去 30 天)
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 个评论
Eric
Eric 2024-7-20
Thanks again for the quick response. I shouldnt of asked a question quite yet. I just stumbled accross how the starting point in curve fitter can affect the output data quite a bit. I will dabble into this new venture for awhile.
Thank You again!
Eric
Eric 2024-7-20
移动:Walter Roberson 2024-7-20
Im trying to produce a general trend that point z should be increasing as x and/or y is increasing, with exceptions.
This screenshot is pretty much what I am trying to mimic with my dataset. I can't physically produce the same variety of data points as that example, so I feel that is why I am having difficulty estimating a fit. I have been looking into some sort of a Monte Carlo simulation to help generalize my data, being the TWOSOUSANDTQNM in the raw form is extrememly noisy, and the TWOSOUSANDTQAPC in raw form has known varying +/-5 percent error.
This data was visually cherrypicked moving means to rule out error and noise.
TWOSOUSANDTQAPC
207.500000000000
296.500000000000
367.200000000000
419.300000000000
461.800000000000
506.800000000000
540.600000000000
TWOSOUSANDTQSPK
35.9000000000000
28.5000000000000
22.1000000000000
19.3000000000000
17
14.8000000000000
13.3000000000000
TWOSOUSANDTQNM
95.2000000000000
174.300000000000
227.800000000000
270
301.800000000000
334.600000000000
355.500000000000

请先登录,再进行评论。

采纳的回答

Ayush
Ayush 2024-9-2
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();
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 7 1.764000e+03 0.000e+00 4.600e+02 1 18 5.667642e+02 0.000e+00 2.600e+02 2.449e+00 2 25 3.859390e+00 0.000e+00 1.990e+00 4.094e+00 3 32 1.578186e+00 0.000e+00 1.880e+00 1.661e+00 4 39 6.026830e-05 0.000e+00 1.493e-01 3.046e+00 5 46 2.549846e-03 0.000e+00 6.153e-01 1.147e+00 6 53 2.523078e-05 0.000e+00 7.506e-02 2.652e-01 7 60 6.041650e-04 0.000e+00 2.666e-01 4.997e-01 8 67 2.460235e-03 0.000e+00 5.374e-01 1.012e+00 9 74 4.110286e-03 0.000e+00 6.943e-01 1.781e+00 10 81 3.269262e-03 0.000e+00 6.188e-01 3.716e+00 11 88 8.390159e-04 0.000e+00 3.135e-01 7.957e+00 12 95 3.204689e-04 0.000e+00 1.941e-01 2.197e+01 13 102 3.624003e-03 0.000e+00 6.519e-01 4.046e+01 14 109 3.266266e-03 0.000e+00 6.189e-01 2.274e+01 15 116 5.677525e-04 0.000e+00 2.581e-01 4.691e+01 16 123 6.708764e-07 0.000e+00 8.586e-03 5.115e+01 17 134 6.355372e-09 0.000e+00 7.720e-04 2.869e-04 18 164 3.976033e-09 0.000e+00 9.726e-05 2.977e-04 19 175 2.915154e-09 0.000e+00 8.691e-05 2.977e-04 20 187 9.286582e-10 0.000e+00 2.365e-04 1.489e-04 21 198 5.229195e-10 0.000e+00 7.814e-05 1.489e-04 22 210 4.052741e-10 0.000e+00 2.828e-04 7.443e-05 23 221 2.586847e-12 0.000e+00 8.366e-05 7.443e-05 24 238 1.608539e-12 0.000e+00 7.508e-05 1.163e-06 25 249 4.972323e-13 0.000e+00 7.093e-05 1.163e-06 26 262 4.084079e-13 0.000e+00 6.867e-05 2.908e-07 Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance. Fitted Parameters: 0.0000 -141.2530 151.2530 1.0000 1.0000 1.0000 Objective Function Value: 4.0841e-13
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 个)

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by