MATLAB: Nonlinear regression

chris

I have to do a nonlinear regression and I need a little help, should I use nlinfit or nlintool, whats the difference?
Ive runned the following but I get an error.
function chris= ps(beta, X) X = [0.53 0.53 0.53 0.52 0.52 0.52 0.51 0.49 0.49 0.48 0.45 0.38 0.37 0.36 0.34 0.34 0.27 0.19 0.03 0.02]'; Y = [0.002 0.003 0.003 0.006 0.011 0.022 0.051 0.098 0.125 0.16 0.22 0.38 0.41 0.45 0.49 0.55 0.67 0.69 0.69 0.717]';
beta=[1.4 0.537 1.1] b1=beta(1); b2=beta(2); b3=beta(3); chris = b1-b1*(X/b2)^b3;
??? Error using ==> mpower Matrix must be square.
Error in ==> chris at 12 chris = b1-b1*(X/b2)^b3;
Help needed!!! Thanks in advance!

Best Answer

From the doc: "nlintool(X,y,fun,beta0) is a graphical user interface to the nlinfit function, and uses the same input arguments"
IOW, nlintool is just a user-friendly front-end to the nlinfit function.
The error is a standard MATLAB error for array operations versus matrix operations. Multiply, divide, and exponentiation are all interpreted in a matrix sense; to use an array operation (ie element-by-element), precede the operator with a dot:
chris = b1-b1*(X/b2).^b3;
Beyond that, you're also going to run into other problems, so let me make a preemptive strike...
Don't define your data in this function!
X and beta are passed in to the function as arguments. Y isn't needed at all here. The function simply defines the model you're trying to fit. Given a value of X and parameters beta, it should return the value of the model you're fitting (ie yhat = beta1 - beta*(X/beta2)^beta3).
It is nlinfit that needs the measured X and Y data values. It ( nlinfit ) uses these to compare the model's predictions for the given X values to the given Y values. It then adjusts the parameters to get the best fit.
So: remove all data from the function. Move them into a separate script. Then, in that script, call nlinfit with an initial guess for beta ( beta=[1.4 0.537 1.1] ).