How do I correctly use the fprinf function with the forward/backward elimination

1 view (last 30 days)
How can I present may solution matrix I just calculated using the fprinf function?
My code so far:
%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
% 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
% C
A = [1 -2 1;
0 2 -8;
-4 5 9]
b = [0; 8; -9]
[L, U, P] = lu(A) % L = all multipliers, U = upper triangular matrix, P = row interchanges
y = L\(P*b) % Forward substitution
x = U\y % Backward substitution

Answers (1)

Hari
Hari on 6 Oct 2023
Edited: Hari on 6 Oct 2023
Hi Jonas,
I understand that you want to display your solution matrix using “fprintf” function in MATLAB. Here is a sample code to do that:
fprintf('Solution matrix:\n');
fprintf('%.2f %.2f %.2f\n', x.');
This code displays the solution matrix with the values of ‘x1’, ‘x2’, and ‘x3’ formatted to two decimal places. To use “fprintf” function, you must explicitly mention the format specifier for the data you want to print. You can use a different format specifier for data for representation. This will type cast the values to the respective formats and prints to the console.
Here is the output observed:
Solution matrix:
29.00 16.00 3.00
Refer to the documentation of “fprintf” to know more about the usage, customisation.
Hope this helps!

Categories

Find more on Mathematics 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!