MATLAB: How to solve a system of equations in the matlab

matrix

Conservation of cars at the four intersections A, B, C and D imply,
x2 + 520 = x3 + 480
x1 + 450 = x2 + 610
x4 + 640 = x1 + 310
x3 + 390 = x4 + 600
respectively. The augmented matrix for this system is
0 1 1 0|-40
1 1 0 0|160
1 0 0 1|-330
0 0 1 1| 210

Best Answer

when det(A)=0, cond(A) is >>1
A=[0 1 -1 0;1 -1 0 0;-1 0 0 1;0 0 1 -1];b=[-40;160;-330;210]
det(A)
then instead of A\b or linsolve(A,b) try different values of tol and maxit in the following matrix preconditioning functions until A*x-b is small enough for x to be considered a solution to the problem.
To do so, try the following:
format long
tol=0.01;maxit=20
[x,flag,relres,iter]=bicgstab(A,B,tol,maxit)
152.50
-7.50
32.50
-177.50
flag =
0
relres =
0.00
iter =
2.50
or
[x,flag,relres,iter]=bicgstab(A,b,tol,maxit)
x =
1.0e+02 *
1.525000000000000
-0.075000000000000
0.325000000000000
-1.775000000000000
flag =
0
relres =
1.673835638739069e-17
iter =
2.500000000000000
or
[x2,flag2,relres2,iter2]=lsqr(A,b,tol,maxit)
x2 =
1.0e+02 *
1.525000000000000
-0.075000000000000
0.325000000000000
-1.775000000000000
flag2 =
0
relres2 =
8.861080375867882e-17
iter2 =
2
or
[x3,flag3,relres3,iter3]=bicg(A,b,tol,maxit)
x3 =
1.0e+02 *
1.525000000000000
-0.075000000000000
0.325000000000000
-1.775000000000000
flag3 =
0
relres3 =
6.695342554956277e-17
iter3 =
3
or
lsqnonneg(A,b)
=
1.0e+02 *
3.299999999999999
1.700000000000000
2.100000000000001
check
A*lsqnonneg(A,b)-b
=
1.0e-13 *
-0.568434188608080
-0.568434188608080
0.568434188608080
0.568434188608080
Mathworks Recommended reading on Matrix Preconditioning:
retail price new > £100.- but there are used ones around for half price.
Regards
John BG
Related Question