How to perform double summation using lsqnonlin
1 次查看(过去 30 天)
显示 更早的评论
Akella Kartik
2022-12-14
I understand that lsqnonlin does the sum of squares of a vector defined. BUt how exactly does it perform a double summation?
I am trying to obtain the min for two 3D vectors. the vector defined isn't getting optmized as I would expect.
14 个评论
Askic V
2022-12-14
You could start by providing code with example and expected result. There is a better chance to get help that way.
Akella Kartik
2022-12-14
编辑:Akella Kartik
2022-12-14
There is no proper code as such. This is more of a generic query so that I can code further.
There are two 3D vectors, say of sizes (m,2,n). And I want to compute the norm using the above equation- for each 2 dim vector at each position in (m,2) arrays across the n dimensions.\
I am trying to
X0 = some initialiazation;
[x,resnorm] = lsqnonlin(@ComputeError,X0);
The logic in the function ComputeError would be as below:
i = 1:n
error = norm(A(:,:,i) - B(:,:,i) ); %A and B are some vectors computed using the X0
But the above code is squaring the sum of norms across (m,2) vectors for n dimensions. WHile, what I ideally want is the sum of squares of the norms, summed over the (m,2) across n.
Askic V
2022-12-14
Ok, so you need to minimize a cost function defined in the following way:
function cost = cost_func(x)
m = 5;
n = 3;
A(:,:,1:n)= rand(m, 2, n);
B(:,:,1:n) = rand(m, 2, n);
% some processing with x, A,B
% x is a vector 2x1
cost = 0;
for i = 1:n
for j = 1:m
cost = cost + norm(A(j,:,i)-B(j,:,i))^2;
end
end
end
Akella Kartik
2022-12-14
But when calling the lsqnonlin, it should do the sum of squares, isn't it?
In your code, the squaring and summation is done explicitly. This works for me though, but I wonder what happens if I use call the lsqnonlin.
Thanks in advance!
Torsten
2022-12-14
For lsqnonlin:
function f = cost_func(x)
for i = 1:n
for j = 1:m
f((i-1)*m + j) = A(j,:,i)-B(j,:,i);
end
end
end
Torsten
2022-12-14
编辑:Torsten
2022-12-14
lsqnonlin minimizes sum_i f_i^2 where the f_i are (nonlinear) functions of a parameter vector x to be optimized. Since A(j,:,i) and B(j,:,i) are vectors, my previous answer will let MATLAB throw an error.
But instead, simply do
function f = cost_func(x)
A = ...;
B = ...;
f = A(:)-B(:);
end
Akella Kartik
2022-12-14
f = A(:)-B(:) is ideally computing the differences of the vectors and the lsqnonlin would min the squared sums. But, going by the equation I have posted, shouldn't the norm of these two be explicitly defined?
Also, f((i-1)*m + j) = A(j,:,i)-B(j,:,i) seemed logical with the indices stated for f.
If we were to take the norm, would this is be okay?
f((i-1)*m + j) = norm(A(j,:,i)-B(j,:,i))
This is not throwing up any errors, but has been running for more than an hour now. Values though are being updated quite well.
Torsten
2022-12-14
编辑:Torsten
2022-12-14
Both settings
f = A(:)-B(:)
and
f((i-1)*m + j) = norm(A(j,:,i)-B(j,:,i))
come out the same for lsqnonlin.
lsqnonlin squares norm(A(j,:,i)-B(j,:,i)) for all i and j and sums them.
But
sum_ij (norm(A(j,:,i)-B(j,:,i)))^2 = sum_ijk (A(j,k,i)-B(j,k,i))^2 = sum (A(:)-B(:)).^2
Of course, if your "norm" is not the usual Euclidian norm, the results will be different.
Torsten
2022-12-14
Then both formulations are equivalent - choose the one you like best.
I suggest to take
f = A(:)-B(:)
because there are no square roots in it when the solver takes derivatives with respect to the parameters.
Akella Kartik
2022-12-14
Got it! Thanks a lot.
Apart from the above suggestion, is any other thing that I need to take care of, that can actually provide me with better results and a faster optmization?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)