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


dpb <none@non.net> wrote in message <ld3ct3$f64$1@speranza.aioe.org>... > On 2/7/2014 12:40 PM, Prabodh wrote: > ... > > > I am trying to do least square fit. I have looked at the examples [and] > > found one that may be useful to me ... > > function F = trial(x,xdata) > > F = x(1)*exp(x(2)*xdata); > > x =[1 0.2 3 4 5 6 7 8 9 10]; % (...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? > > Your function should be only > > function F = trial(x,xdata) > F = x(1)*exp(x(2)*xdata); > > where x is the vector of coefficients of the function, _NOT_ to be > confused with the x variable of the function (xdata). > > Save this in the mfile 'trial.m' on the path (appears that's ok, altho > you have some other stuff in the file that shouldn't be I'm guessing). > > Then as you did, define xdata, ydata and the starting guesses for the > coefficients, x as x0 and call and all should be well. > > [x,resnorm] = lsqcurvefit(@trial,x0,xdata,ydata); > > NB that the only place x actually shows up is in the return vector > output from lsqcurvefit. It is defined in length by the length(X0) > internally and those initial guesses get modified automagically and > returned in x(1) and x(2), respectively behind the scenes. > > I still do not understand. I have written exactly as you have told:  function F = trial(x,xdata) F = x(1)*exp(x(2)*xdata); I deleted the line x =[1 0.2 3 4 5 6 7 8 9 10]; and put the other as it was before: function F = trial(x,xdata) F = x(1)*exp(x(2)*xdata); 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] [x,resnorm] = lsqcurvefit(@trial,x0,xdata,ydata); Still the problem is not fixed. It gave the same error again. How do I correct it? I have written this in the editor :  ctrl + shift + N (not in the mupad). Is this the problem? Any suggestion will be helpful to me. Thank you.

