Algebraic Problem in writing the function for LSQNONLIN tool in the Optimization Toolbox
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
I am using lsqnonlin for fitting a non linear equation to the model
z=Ax+(B*log(y))+(C/y)+(D*x*log(1+y))
X matrix is 10x2 double matrix. 1st column contains values for x and 2nd column contains values for y.
x(1) and x(2) represent x and y respectively in the model.
The function definition to be used in lsqnonlin is as follows:
function F=myfun(x,X,Z) k=1:10; F=(x(1)*X(k,1))+(x(2)*log(X(k,2)))+(x(3)/X(k,2))+(x(4)*X(k,1)*log(1+X(k,2)))-Z(k); end
I have all the values for the X and the Z matrix.
The problem is that whenever I am trying to call the myfun function, the error which it shows is:
??? Error using ==> plus Matrix dimensions must agree.
Error in ==> myfun at 3 F=( x(1) * X(k,1) )+( x(2) * log(X(k,2) )+( x(3) / X(k,2) )+( x(4) * X(k,1) * log(1+X(k,2) ) ) - Z(k);
Please clear this up.
Thank you.
채택된 답변
Andrei Bobrov
2012년 5월 11일
use function lsqcurvefit
eg:
A = .5;
B = 4.1;
C = .6;
D = .6;
x = (.01:.01:1)';y = (.01:.01:1)';
z = A*x+(B*log(y))+(C./y)+(D*x.*log(1+y))+.1*rand(size(x));
%solution:
f = @(a,X) a(1) * X(:,1) + a(2) * log(X(:,2)) + a(3) ./ X(:,2) + a(4) * X(:,1) .* log(1+X(:,2) );
abcd = lsqcurvefit(f,[0 0 0 0],[x y],z)
댓글 수: 0
추가 답변 (1개)
Sargondjani
2012년 5월 11일
the message simply tells you that if youo want to add two matrices they have to be of the same size, and apparently that is not the case
(split the equation in parts if you want to find out where it goes wrong)
it might be that your Z is a row vector and not a column vector.
i also see a future error message: if you want to devide a number by a vector you have to use './' instead of '/' (after the x(3))
댓글 수: 3
Sargondjani
2012년 5월 11일
but you also add x(1)*X(k,1) + x(2)*log(X(k,1))...
that's adding two matrices (vectors). andrei's correction might do the trick though...
참고 항목
카테고리
Help Center 및 File Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!