plugging in a function inside fittype, error

Hello. I'm new in matlab. I'm currently trying to use the fittype function to later on use to fit.
I have a truncated exponetial whcih i define in the following matlab function.
function [yf] = Exp_t(x3,a,m)
%x and y data are arrays with size 20x1
%a and m are unknown coeficients to be fitted.
for i=1:20
if x3(i)<0 %truncation
yf(i,1)=0;
elseif (x3(i)>=0) && (x3(i)<=a)
yf(i,1)=(1-exp(-m*x3(i)))/(1-exp(-m*a)); %exponential fucntion
elseif x3(i)>a %truncation
yf(i,1)=1;
end
end
end
I then insaert this function in my main script
%truncated exponential fit.
y3=(table1{:,4}); %data from column 4 table1
x3=(table1{:,3}); %data from column 3 table1
%inserting Exp_t(x3,a,m) into fittype function
ft = fittype( 'Exp_t(x3,a,m)','independent', {'x3'}, 'dependent', {'yf'},'coeff',{'a','m'});
%fit(x3,y3,ft,'Robust','on')
I've spent more than two days trying to fix this, any help is much apreciated.
I receive the following errors:
Error using fittype/testCustomModelEvaluation (line 12)
Expression Exp_t(x3,a,m) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error in fittype expression ==> Exp_t(x3,a,m)
??? Index exceeds the number of array elements (7).
Error in fittype>iCreateFittype (line 373)
testCustomModelEvaluation( obj );
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
Error in Project_2 (line 105)
ft = fittype( 'Exp_t(x3,a,m)','independent', {'x3'}, 'dependent', {'yf'},'coeff',{'a','m'});
Caused by:
Error using fittype/evaluate>I_ERROR_FCN_ (line 179)
Error in fittype expression ==> Exp_t(x3,a,m)
??? Index exceeds the number of array elements (7).

 採用された回答

Gargi Patil
Gargi Patil 2021 年 12 月 21 日

1 投票

Hi,
This error can be resolved by declaring the size of the output array yf beforehand in the function Exp_t as follows:
function [yf] = Exp_t(x3, a, m)
%x and y data are arrays with size 20x1
%a and m are unknown coeficients to be fitted.
yf = zeros(size(x3, 1), 1);
....
end

2 件のコメント

Alvaro Sanchez
Alvaro Sanchez 2021 年 12 月 21 日
thanks, that worked but also had to remove the brackets.
Matt J
Matt J 2021 年 12 月 22 日
@Alvaro Sanchez If it worked, please Accept-click the answer.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 12 月 22 日

0 投票

ft = fittype( 'Exp_t(x3,a,m)','independent', {'x3'}, 'dependent', {'yf'},'coeff',{'a','m'});
Instead of using a quoted character expression, use an anonymous function.

カテゴリ

ヘルプ センター および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 12 月 10 日

回答済み:

2021 年 12 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by