# Multiple parameter Non Linear Curve Fit- Error using ^

5 views (last 30 days)
Adhitya Shreyas on 4 Jul 2021
Commented: Adhitya Shreyas on 5 Jul 2021
Here lies the code I used for Non-linear regression. The fit equation is large and actually the fit works on Mathematica to an extent. Just checking on MATLAB too. The input X is an array of 600 terms, and Y as well.
G = 6.67*(10^-11);
H = 73.8;
% Curve-fit Equation
fun = @(b,X) (100*G*(H^2)*((b(1)^3)/X)*((log(abs(1+(X/b(2))))-((X/b(2))/((X/b(2))+1)))/(log(abs(1+(b(1)/b(2))))-((b(1)/b(2))/((b(1)/b(2))+1))))) + (4*pi*G*b(3)*b(4)*((X/(2*b(4)))^2)*((besseli(0,(X/(2*b(4))))*besselk(0,(X/(2*b(4)))))-(besseli(1,(X/(2*b(4))))*besselk(0,(X/(2*b(4))))))) + (G*22.665*(b(5)^2)*b(6));
% Rough parameter values
b = [10; 10; 10; 10; 10; 10];
X = Rkpc;
Y = Vkms;
% Specify a vector of starting conditions for the solvers
b0 = [75; 300; 1; 8; 10; 10];
% Perform a nonlinear regression
beta = nlinfit(X,Y, fun, b0);
I've recieved the following error:
Error using nlinfit (line 213)
Error evaluating model function
'@(b,X)(100*G*(H^2)*((b(1)^3)/X)*((log(abs(1+(X/b(2))))-((X/b(2))/((X/b(2))+1)))/(log(abs(1+(b(1)/b(2))))-((b(1)/b(2))/((b(1)/b(2))+1)))))+(4*pi*G*b(3)*b(4)*((X/(2*b(4)))^2)*((besseli(0,(X/(2*b(4))))*besselk(0,(X/(2*b(4)))))-(besseli(1,(X/(2*b(4))))*besselk(0,(X/(2*b(4)))))))+(G*22.665*(b(5)^2)*b(6))'.
Error in Curve_fit_darkmatter (line 35)
beta = nlinfit(X,Y, fun, b0);
Caused by:
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform
elementwise matrix powers, use '.^'.
Kindly help me on how to change the matrix if necessary, or what other steps to do. Or also an alternative method to curve fit maybe. This might be challenging, but thanks for the help !!

Alan Stevens on 4 Jul 2021
You probably need to use element by element multiplication and division etc in your definition of fun.
I.e. use a.*b and a./b (notice the dots) rather than a*b and a/b (without the dots) etc.
Adhitya Shreyas on 5 Jul 2021
Thanks a lot Alan !

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by