试图在Matlab中求解线性方程组 [英] Trying to solve a system of linear equations in matlab

查看:136
本文介绍了试图在Matlab中求解线性方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,要求我解决一个线性方程组.在问题中指出,我应该建立矩阵A和列向量 b 来解决方程A x = b ,其中 x 是列向量(wxyz).

I've been set a question asking me to solve a system of linear equations. In the question it states I should set up a matrix A and column vector b to solve the equation Ax=b, where x is the column vector (w x y z).

A = [1 1 1 1; 0 1 4 -2; 2 0 -2 1; 1 -2 -1 1]
b = [28;7;22;-4]
A1 = inv(A).*b
sum(A1,2)

这是我到目前为止所做的,但是我知道MATLAB给我的答案是错误的,因为正确的解决方案应该是w = 10.5,x = 9,y = 2.5,z = 6.

This is what I've done so far, however I know the answer that MATLAB gives me is incorrect, as the right solutions should be w=10.5, x=9, y=2.5, z=6.

有人可以向我指出正确的方向/告诉我我要去哪里了吗? (我对MATLAB还是相当陌生,所以对此一无所知). 谢谢.

Can someone point me in the right direction/ show me where I'm going wrong? (I'm fairly new to MATLAB so very unsure about it all). Thanks.

推荐答案

A = [1 1 1 1; 0 1 4 -2; 2 0 -2 1; 1 -2 -1 1];
b = [28;7;22;-4];
A1 = A \ b;
ans = sum(A1,2);

有关\运算符的参考,请阅读: https ://it.mathworks.com/help/matlab/ref/mldivide.html

For a reference concerning the \ operator, please read this: https://it.mathworks.com/help/matlab/ref/mldivide.html

使用您的技术的正确代码是:

The correct code to use your technique would be:

A1 = inv(A) * b;

但是您可能会注意到,Matlab代码分析器会指出:

but as you may notice, the Matlab code analyzer will point out that:

对于求解线性方程组,a的逆 矩阵主要具有理论价值.切勿使用a的倒数 求解线性系统Ax = b且x = inv(A)* b的矩阵,因为它是 缓慢且不准确.

For solving a system of linear equations, the inverse of a matrix is primarily of theoretical value. Never use the inverse of a matrix to solve a linear system Ax=b with x=inv(A)*b, because it is slow and inaccurate.

用A \ b替换inv(A)* b

Replace inv(A)*b with A\b

将b * inv(A)替换为b/A

Replace b*inv(A) with b/A

那:

INV(A)* b可能比A \ b慢

INV(A)*b can be slower than A\b

这篇关于试图在Matlab中求解线性方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆