plugging in a function inside fittype, error
4 views (last 30 days)
Show older comments
Alvaro Sanchez
on 10 Dec 2021
Answered: Walter Roberson
on 22 Dec 2021
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).
0 Comments
Accepted Answer
Gargi Patil
on 21 Dec 2021
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 Comments
More Answers (1)
Walter Roberson
on 22 Dec 2021
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.
0 Comments
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!