필터 지우기
필터 지우기

Error using fittype for model evaluation

조회 수: 11 (최근 30일)
Sarawud Saleesongsom
Sarawud Saleesongsom 2024년 3월 19일
댓글: Sarawud Saleesongsom 2024년 3월 19일
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
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.
Sarawud Saleesongsom
Sarawud Saleesongsom 2024년 3월 19일
I'm trying to do Monte Carlo simulation here. I want to take into account my error (deltalogKap) into logKap.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by