# How to correctly present results of linear equation with f print f function?

2 views (last 30 days)
Jonas Morgner on 4 May 2022
Answered: Walter Roberson on 4 May 2022
% A
syms x1 x2 x3 % Defining the vairables
eqns = [x1 - 2*x2 + x3 == 0, % First equation
2*x2 - 8*x3 == 8, % Second equation
-4*x1 + 5*x2 + 9*x3 == -9]; % Third equation
[A] = equationsToMatrix(eqns) % Forming the coefficient matrix
A =
% B
syms x1 x2 x3
eqns = [x1 - 2*x2 + x3 == 0,
2*x2 - 8*x3 == 8,
-4*x1 + 5*x2 + 9*x3 == -9];
[A,b] = equationsToMatrix(eqns) % Forming the augumented matrix
A =
b =
% C
A = [1 -2 1;
0 2 -8;
-4 5 9]
A = 3×3
1 -2 1 0 2 -8 -4 5 9
b = [0; 8; -9]
b = 3×1
0 8 -9
[L, U, P] = lu(A) % L = all multipliers, U = upper triangular matrix, P = row interchanges
L = 3×3
1.0000 0 0 0 1.0000 0 -0.2500 -0.3750 1.0000
U = 3×3
-4.0000 5.0000 9.0000 0 2.0000 -8.0000 0 0 0.2500
P = 3×3
0 0 1 0 1 0 1 0 0
y = L\(P*b) % Forward substitution
y = 3×1
-9.0000 8.0000 0.7500
x = U\y % Backward substitution
x = 3×1
29 16 3
How can I present the result (x) correctly with the fprintf function?
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Walter Roberson on 4 May 2022
fprintf('%5g\n', x)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Linear Algebra in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by