Error using fittype for model evaluation
6 次查看(过去 30 天)
显示 更早的评论
I want to find k1-k7 from this equation logKap= log(k1[Fe2+] + k2[FeOH+] + k3[Fe(OH)2] + k4[Fe(OH)3-] + k5[ FeCl+] + k6[ FeSO4] + k7[FeHSO4+]
I have data for [Fe species] and logKap from 40 experiments.
here are my code
clc;
clear;
close all;
% Import data
data = xlsread('Rateconstant.xlsx');
logKap = data(:,2);
deltalogKap = data(:,3);
Fe2_plus = data(:,4);
FeOH_plus = data(:,5);
FeOH2 = data(:,6);
FeOH3_minus = data(:,7);
FeCl_plus = data(:,8);
FeSO4 = data(:,9);
FeHSO4_plus = data(:,10);
n = 10;
% Create arrays to store the fitted parameters
k1 = zeros(1, n);
k2 = zeros(1, n);
k3 = zeros(1, n);
k4 = zeros(1, n);
k5 = zeros(1, n);
k6 = zeros(1, n);
k7 = zeros(1, n);
% Create arrays to store generated data
logKap2 = zeros(size(logKap, 1), n);
for i = 1:n
% Generate new random data within the range of Kap +- deltaKap
logKap2(:,i) = logKap + deltalogKap.*(2*rand(size(logKap)) - 1);
% Define the function to be fitted
Model1= fittype('log10(k1*(Fe2_plus) + k2*(FeOH_plus) + k3*(FeOH2) + k4*(FeOH3_minus) + k5*(FeCl_plus) + k6*(FeSO4) + k7*(FeHSO4_plus))', 'independent', {'Fe2_plus', 'FeOH_plus','FeOH2','FeOH3_minus','FeCl_plus','FeSO4','FeHSO4_plus'}, 'dependent', 'logKap2');
fitted1 = fit([Fe2_plus(:,i), FeOH_plus(:,i), FeOH2(:,i), FeOH3_minus(:,i), FeCl_plus(:,i), FeSO4(:,i), FeHSO4_plus(:,i)], logKap2(:,i), Model1, 'Lower', [0, 0, 0,0,0,0,0], 'Upper', [Inf, Inf, Inf,Inf,Inf,Inf,Inf]);
% Record the fitted parameters
k1(i) = fitted1.k1;
k2(i) = fitted1.k2;
k3(i) = fitted1.k3;
k4(i) = fitted1.k4;
k5(i) = fitted1.k5;
k6(i) = fitted1.k6;
k7(i) = fitted1.k7;
end
But I have some error from the fitting process and don't know how to fix it.
Error using fittype/testCustomModelEvaluation
Expression log10(k1*(Fe2_plus) + k2*(FeOH_plus) + k3*(FeOH2) + k4*(FeOH3_minus) + k5*(FeCl_plus) +
k6*(FeSO4) + k7*(FeHSO4_plus)) is not a valid MATLAB expression, has non-scalar coefficients, or
cannot be evaluated:
Not enough inputs to FITTYPE function.
Error in fittype>iCreateFittype (line 373)
testCustomModelEvaluation( obj );
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
Error in Rateconstant (line 37)
Model1= fittype('log10(k1*(Fe2_plus) + k2*(FeOH_plus) + k3*(FeOH2) + k4*(FeOH3_minus) + k5*(FeCl_plus) + k6*(FeSO4) + k7*(FeHSO4_plus))', 'independent', {'Fe2_plus', 'FeOH_plus','FeOH2','FeOH3_minus','FeCl_plus','FeSO4','FeHSO4_plus'}, 'dependent', 'logKap2');
Caused by:
Error using fittype/evaluate>I_ERROR_FCN_
Not enough inputs to FITTYPE function.
2 个评论
the cyclist
2024-3-19
I'm very confused about why you are looping over your data 10 times, and why you are generating "new random data". This makes no sense to me.
Perhaps you could share the data, and also describe (or provide a reference to) the method you want to use to fit your data.
回答(1 个)
Walter Roberson
2024-3-19
You cannot use nonscalar coefficients in the fit model. Your Fe2_plus and so on are nonscalar.
The computing model is that the fitting process can pass in variable-length of the independent variables, and that the result has to be the same size as the input. Normally all of the independent data is passed in, but potentially different size of data is passed in during the start-up phase when the expression is being probed for suitability.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!