How to show iterations and values of variables serially in a column in a figure? Help me plz

3 visualizzazioni (ultimi 30 giorni)
I solved linear equations by using Gauss-Seidel method and showed the iterations and values of x1,x2,x3,x4 in columns in the command window.But I can't do it in a figure. i wrote this:
x2=0;x3=0;x4=0;k=0; disp(' n x1 x2 x3 x4') for i=1:10 k=k+0.1; x1=0.3+ 0.2*x2+0.1*x3+0.1*x4; x2=1.5+ 0.2*x1+0.1*x3+ 0.1*x4; x3=2.7+0.1*x1+0.1*x2+0.2*x4; x4=-0.9+0.1*x1+0.1*x2+0.2*x3;
disp([i' x1' x2' x3' x4']) end
figure(1) axis([0 1 0 1]); grid on text(0.15,0.95,'n') text(0.25,0.95,'x1') text(0.35,0.95,'x2') text(0.45,0.95,'x3') text(0.55,0.95,'x4')
How can i show the values of x1,x2,x3 and x4 serially one under one in that figure? Plz help

Risposte (1)

nick
nick il 14 Apr 2025
Hello Shams,
To display the remaining values of x1,x2,x3 and x4 serially in order, 'for' loop can be utilised as shown below:
x2 = 0; x3 = 0; x4 = 0;
iterations = 10;
results = zeros(iterations, 4);
for i = 1:iterations
x1 = 0.3 + 0.2 * x2 + 0.1 * x3 + 0.1 * x4;
x2 = 1.5 + 0.2 * x1 + 0.1 * x3 + 0.1 * x4;
x3 = 2.7 + 0.1 * x1 + 0.1 * x2 + 0.2 * x4;
x4 = -0.9 + 0.1 * x1 + 0.1 * x2 + 0.2 * x3;
results(i, :) = [x1, x2, x3, x4];
end
figure(1);
axis off;
hold on;
% Add column headers
text(0.1, 1, 'n', 'FontWeight', 'bold');
text(0.3, 1, 'x1', 'FontWeight', 'bold');
text(0.5, 1, 'x2', 'FontWeight', 'bold');
text(0.7, 1, 'x3', 'FontWeight', 'bold');
text(0.9, 1, 'x4', 'FontWeight', 'bold');
% Add iteration results
for i = 1:iterations
y_position = 1 - i * 0.05; % Adjust y position for each iteration
text(0.1, y_position, num2str(i));
text(0.3, y_position, num2str(results(i, 1), '%.4f'));
text(0.5, y_position, num2str(results(i, 2), '%.4f'));
text(0.7, y_position, num2str(results(i, 3), '%.4f'));
text(0.9, y_position, num2str(results(i, 4), '%.4f'));
end
hold off;
Kindly refer to the documentation by executing the following command in MATLAB Command Window to know more about 'hold' function :
doc hold

Categorie

Scopri di più su Numerical Integration and Differential Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by