# plugging in a function inside fittype, error

7 visualizzazioni (ultimi 30 giorni)
Alvaro Sanchez il 10 Dic 2021
Risposto: Walter Roberson il 22 Dic 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.
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Gargi Patil il 21 Dic 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 CommentiMostra NessunoNascondi Nessuno
Alvaro Sanchez il 21 Dic 2021
thanks, that worked but also had to remove the brackets.
Matt J il 22 Dic 2021

Accedi per commentare.

### Più risposte (1)

Walter Roberson il 22 Dic 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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by