Multiple parameter Non Linear Curve Fit- Error using ^
8 次查看(过去 30 天)
显示 更早的评论
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 !!
0 个评论
采纳的回答
Alan Stevens
2021-7-4
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!