error using curve fitting tools--Attempted to access x(8); index out of bounds because numel(x)=7

2 views (last 30 days)
Hi I am a newbie in matlab, I want to use curve fitting tool to fit a custom equation, which is a continuous two-peak curves, the model includes 8 parameters.
function y = minimize(x,a,b,c,d,e,f,g,h)
TC = xlsread('M:\G1-L1-D1-1.xls',1,'I3:I258');
R2 = 1000000./ TC;
nrows = size(R2,1);
for i = 1:nrows
if (x(i)<a)
y(i)=0;
else if (a<x(i)<b)
y(i)= h.*d.*(sin(pi.*(x(i)-a)./(b...
-a))).*exp(-(c.*(x(i)-a)))./((b-a).*pi.*(1+exp(-(c.*(b-a...
)))./((c.*(b-a))^2)+pi.*pi));
else if (x(i)>b)
y(i)=0;
else if (b<x(i)<e)
y(i)=0;
else if (e<x(i)<f)
y(i)= h.*(1-d).*(sin(pi.*(x(i)-e)./(f-e))).*exp(-(g.*(x(i)-e)))./((f-e).*pi.*(1+exp...
(-(g.*(f-e))))./((g.*(f-e))^2+pi.*pi))
else if (x(i)>f)
y(i)=0;
end
end
end
end
end
end
i=i+1;
end
And then I call cftool, but got an error like this, I don't understand why and how can I solve this, is it something to do with start points? Thanks! Expression minimize(x,a,b,c,d,e,f,g,h) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated: Error in fittype expression ==> minimize(x,a,b,c,d,e,f,g,h) ??? Attempted to access x(8); index out of bounds because numel(x)=7.
close all
clear all
TC = xlsread('M:\G1-L1-D1-1.xls',1,'I3:I258');
x = 1000000./ TC;
x = x(98:198,1);
QQ = 'M:\ILT-D1';
aa = dir(QQ);
aa = xlsread('M:\G1-L1-D1-1.xls',1,'J3:J258');
y= aa.* TC;
y = y(98:198,1);
cftool
regards Han

Answers (1)

Walter Roberson
Walter Roberson on 12 Oct 2015
Your code
if a<x(i)<b
means
if (a<x(i)) < b
The (a<x(i)) part evaluates to either 0 (false) or 1 (true), so you are then comparing 0 or 1 to b. It is highly unlikely that you want to do that.
Your code reads I3:I258 of the xls file. That is 256 entries. That number, 256, gets used as nrows, which is the maximum index in "for i". You index x(i) . Therefore your input, x, must have 256 entries in the vector, but instead it has 7. I am not certain at the moment why it would have 7 entries considering that you use x = x(98:198,1) which should have 11 entries. Either way, 7 entries or 11 entries is certainly not the 256 that you are looping over.
Notice also that if
if (x(i)>b)
is true whenever b <= x(i) . And then in your next test you check
if (b<x(i)<e)
but if b<x(i) then the previous test would have been true.
I recommend also that you program using "elseif" rather than "else if". And remember that any test that has succeeded earlier does not need to be repeated, so if you tested x <= a then the next test does not have to check a < x because you could not have reached the second "if" in the situation where x <= a
  2 Comments
Han Zhu
Han Zhu on 12 Oct 2015
Thank you for your answer. The function that I want to fit to depends on the range of x. I correct it as this:
function y = minimize(x,a,b,c,d,e,f,g,h)
TC = xlsread('M:\G1-L1-D1-1.xls',1,'I3:I258');
R2 = 1000000./ TC;
nrows = size(R2(98:198,1),1);
for i = 1:nrows
if (x(i)>a && x(i)<b)
y(i)= h.*d.*(sin(pi.*(x(i)-a)./(b...
-a))).*exp(-(c.*(x(i)-a)))./((b-a).*pi.*(1+exp(-(c.*(b-a...
))))./((c.*(b-a))^2+pi.*pi));
elseif (x(i)>e &&x(i)<f)
y(i)= h.*(1-d).*(sin(pi.*(x(i)-e)./(f-e))).*exp(-(g.*(x(i)-e)))./((f-e).*pi.*(1+exp...
(-(g.*(f-e))))./((g.*(f-e))^2+pi.*pi));
else
y(i)=0;
end
end
end
I don't really understand your comment " I am not certain at the moment why it would have 7 entries considering that you use x = x(98:198,1) which should have 11 entries." why is it 11 entries? isn't it 101?
I tried to run it again, but the same error message still appears.. Also I tried the fittype function instead of cftool:
ft = fittype('minimize(x,a,b,c,d,e,f,g,h)');
f = fit( x, y, ft,'StartPoint', [3.410941423,5.321103981,0.609720564,0.015015503,10.41287485,43.70890568,0.078584389,1.04E+09]);
which gives error as Error using fittype>iTestCustomModelEvaluation (line 726) Expression minimize(x,a,b,c,d,e,f,g,h) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated: Error in fittype expression ==> minimize(x,a,b,c,d,e,f,g,h) ??? Undefined function 'minimize' for input arguments of type 'double'.
Error in fittype>iCreateFittype (line 367) iTestCustomModelEvaluation( obj );
Error in fittype (line 324) obj = iCreateFittype( obj, varargin{:} );
Caused by: Error using fittype/evaluate (line 102) Error in fittype expression ==> minimize(x,a,b,c,d,e,f,g,h) ??? Undefined function 'minimize' for input arguments of type 'double'.
Walter Roberson
Walter Roberson on 12 Oct 2015
101,yes,I misread.
In order to use fittype with a string instead of an anonymous function, the function named in the string must have its own .m file and cannot be a function defined in some other .m file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!