plugging in a function inside fittype, error

조회 수: 10 (최근 30일)
Alvaro Sanchez
Alvaro Sanchez 2021년 12월 10일
답변: Walter Roberson 2021년 12월 22일
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일
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일
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.

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by