
index out of bound because numel(x) = 1
Posted:
Feb 7, 2014 1:40 PM


Hi, I am trying to do least square fit. I have looked at the examples in the Matlab official web site that may be somewhat related to my problem. I found one that may be useful to me and wrote it exactly as it was shown in the Matlab official web site. I have pasted it below: 
function F = trial(x,xdata) F = x(1)*exp(x(2)*xdata); x =[1 0.2 3 4 5 6 7 8 9 10]; % (This part is not given in the web site. I added it % %after I got the error message. This did not fix the problem. Then I tried giving this % data different name, but it still did not fix the problem.) xdata =[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3]; ydata =[455.2 428.6 124.1 67.3 43.2 28.1 13.1 0.4 1.3 1.5]; x0 = [100; 1] % Starting guess [x,resnorm] = lsqcurvefit(@trial,x0,xdata,ydata);
I got the error message :
Attempted to access x(2); index out of bounds because numel(x)=1.
Error in trial (line 6) F = x(1)*exp(x(2)*xdata);
How do I correct it? I know the reason why the error appears, but I do not know how I can correct it?

