error using curve fitting tools--Attempted to access x(8); index out of bounds because numel(x)=7
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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
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.
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!