如何在Matlab中为多个变量的超定二次系统找到argmin/最佳拟合/优化 [英] How to find argmin/best fit/optimize for an overdetermined quadratic system for multiple variables in Matlab
问题描述
我有100个方程式和5个变量. Matlab中有一个函数可以用来找到这些方程的最佳解吗?
I have 100 equations with 5 variables. Is there a function in Matlab which I can use to find the optimal solution of these equations?
我的问题是找到argmin ||(a-ic)^ 2 +(b-jd)^ 2 + e-h(i,j)||在所有i,j的范围从-10到10.即
My problem is to find argmin ||(a-ic)^2 + (b-jd)^2 + e - h(i,j)|| over all i, j from -10 to 10. ie.
%% Note: not Matlab code. Just showing the Math.
for i = -10:10
for j = -10:10
(a-ic)^2 + (b-jd)^2 + e = h(i,j)
已知:h(i,j)
是10*10
矩阵,而i,j
是索引
known: h(i,j)
is a 10*10
matrix,and i,j
are indexes
预期:a,b,c,d,e
推荐答案
You can try using lsqnonlin
as follows.
%% define a helper function in your .m file
function f = fun(x)
a=x(1); b=x(2); c=x(3); d=x(4); e=x(5); % Using variable names from your question. In other situations, be careful when overwriting e.
f=zeros(21*21,0); % size(f) is taken from your question. You should make this a variable for good practice.
for i = -10:10
for j = -10:10
f(10*(i+10+1)+(j+10+1)) = (a-i*c)^2 + (b-j*d)^2 + e - h(i,j); % 10 is taken from your question.
end
end
end
(此外,为什么您的h(i,j)取负指数?)
在主要功能中,您只需编写即可
In your main function you can simply write
function out=myproblem(x0)
out=lsqnonlin(@fun,x0);
end
在您的cmd中,您可以使用特定的初始尝试进行呼叫,例如
In your cmd, you can call with specific initial try such as
myproblem([0,0,0,0,0])
助手功能优于匿名功能,因为根据我的经验,助手会被JIT加速,而匿名则不会.我还选择在循环中重塑形状,而不是在实际之后调用reshape
,因为我希望reshape
花费大量的额外时间.请记住,fun
中的O(1)不是lsqnonlin
中的O(1).
Helper function over anonymous because in my experience helpers get sped up by JIT while anonymous do not. I also opted to reshape in the loops as an opposed to actually call reshape
after because I expect reshape
to cost significant extra time. Remember that O(1) in fun
is not O(1) in lsqnonlin
.
(一如既往,不能保证解决非线性问题.)
(As always, a solution to a nonlinear problem is not guaranteed.)
这篇关于如何在Matlab中为多个变量的超定二次系统找到argmin/最佳拟合/优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!