Defining my objective function using vector of variables for lsqnonlin
이전 댓글 표시
I wanna use lsqnonlin for estimating vector of variables of length 6
function F = myfun()
global X % regression matrix of (nx6)
global Y % output vector (nx1)
F = Y - ( w(1)*X(:,1) + w(2)*X(:,2) + w(3)*X(:,3) + w(4)*X(:,4) + w(5)*X(:,5) + w(6)*X(:,6) );
end
How can I dynamically define my function to adapt with any change in the number of variables (m) when the regression matrix dimesions (nxm) is changed. as follow
F = Y - ( w(1)*X(:,1) + w(2)*X(:,2) + w(3)*X(:,3) + .. + w(m)*X(:,m) );
답변 (1개)
To make the objective function adaptable to any number of variables (based on the number of columns in the regression matrix), the computation can be written using matrix operations instead of hardcoding each term.
Assuming "X" is an "n × m" regression matrix and "w" is a variable vector of length "m", the function can be written like this:
function F = myfun(w)
global X Y
F = Y - X * w;
end
This way, the function works for any number of variables, as long as the number of columns in "X" matches the length of "w". The key is using matrix multiplication, which automatically handles the summation over all terms.
Hope this helps!
카테고리
도움말 센터 및 File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!