如何在 MATLAB 中使用最小二乘近似?

发布于 2024-08-13 08:26:04 字数 375 浏览 9 评论 0原文

对于线性代数的家庭作业,我使用 MATLAB 的 \ 运算符(这是推荐的方法)求解了以下方程:

A = [0.2 0.25; 0.4 0.5; 0.4 0.25];
y = [0.9 1.7 1.2]';
x = A \ y

这产生了以下答案:

x =
1.7000
2.0800

对于作业的下一部分,我我应该使用最小二乘近似来求解相同的方程(然后将其与先前的值进行比较,看看近似值有多准确)。

我怎样才能在 MATLAB 中找到实现这一点的方法?

之前的工作:我找到了函数lsqlin,它似乎能够求解上述类型的方程,但我不明白要提供哪些参数,也不知道以什么顺序提供。

For a homework assignment in linear algebra, I have solved the following equation using MATLAB's \ operator (which is the recommended way of doing it):

A = [0.2 0.25; 0.4 0.5; 0.4 0.25];
y = [0.9 1.7 1.2]';
x = A \ y

which produces the following answer:

x =
1.7000
2.0800

For the next part of assignment, I'm supposed to solve the same equation using the least squares approximation (and then compare it against the prior value to see how accurate the approximation is).

How can I find a way of doing that in MATLAB?

Prior work: I have found the function lsqlin, which seems to be able to solve equations of the above type, but I don't understand which arguments to supply it nor in what order.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2024-08-20 08:26:04

mldivide, ("\") 实际上也是这样做的。根据文档

如果 A 是 m × n 矩阵,其中 m ~= n,B 是具有 m 个分量的列向量,或具有多个此类列的矩阵,则 X = A\B 是最小二乘意义上的解到欠定或超定方程组 AX = B。换句话说,X 最小化范数(A*X - B),即向量 AX - B 的长度。A 的秩 k 由列的 QR 分解确定旋转(有关详细信息,请参阅算法)。计算出的解 X 每列最多有 k 个非零元素。如果 k < n,这通常与 x = pinv(A)*B 的解不同,后者返回最小二乘解。

事实上,你在第一个作业中所做的就是使用 LSE 求解方程。

mldivide, ("\") actually does that too. According to the documentation:

If A is an m-by-n matrix with m ~= n and B is a column vector with m components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B. In other words, X minimizes norm(A*X - B), the length of the vector AX - B. The rank k of A is determined from the QR decomposition with column pivoting (see Algorithm for details). The computed solution X has at most k nonzero elements per column. If k < n, this is usually not the same solution as x = pinv(A)*B, which returns a least squares solution.

So really, what you did in the first assignment was to solve the equation using LSE.

情绪少女 2024-08-20 08:26:04

您的作业是否涉及显式编码最小二乘近似,或者仅使用 MATLAB 中提供的其他函数?如果您可以使用其他函数,一个选项是 LSQR

x = lsqr(A,y);

Does your assignment involve explicitly coding up a least-squares approximation, or just using another function available in MATLAB? If you can use another function, one option is LSQR:

x = lsqr(A,y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文