直接解法
Ax=l,求x。利用逆矩阵、矩阵除法、x=inv(A'A)*A'l
lind.m文件如下,
%求解线性方程组
% x1 + 2x2 + 3x3 + 9x4 = 5
%2x1 + 2x2 + 5x3 + 4x4 = 2
%3x1 + 5x2 + x3 + 5x4 = 3
%7x1 + 4x2 + 2x3 -10x4 = 8
A=[1 2 3 9;2 2 5 4;3 5 1 5;7 4 2 -10];
b=[5 2 3 8]';
x=A\b
c=inv(A)*b
d=inv(A'*A)*A'*b
输出
>> lind.m
x =
4.9227
-3.2373
-1.1680
1.1173
c =
4.9227
-3.2373
-1.1680
1.1173
d =
4.9227
-3.2373
-1.1680
1.1173
Warning: Direct access of structure fields returned by a function call (e.g.,
call to lind) is not allowed. See MATLAB 7.10 Release Notes, "Subscripting Into Function Return