regarding iteration plot of gauss-seidel method
4 次查看(过去 30 天)
显示 更早的评论
I have a Matlab code to find the values of iteratives x and the iterations (k). The code is showing correct answers for the given set of equations and initial values. However I wanted to plot the iteration values on the x-axis (k-1) and iterative solutions on the y-axis (x(i)). However the figure shows blank. Attached is the working matlab code. I needed help in plotting the values and iterations simultaneously.
% source: https://matlabcastor.blogspot.com/search?q=gauss
clc
clear all
close all
no_eqn=3;
tolerance=1e-3;
max_itr=10;
% tol = input('Enter the tolerance, tol: ');
% m = input('Enter maximum number of iterations, m: ');
% n=input('Enter number of equations, n: ');
A=[6 -3 1 3;1 2 -1 2; 2 5 -7 -9];
x1=[0 0 0];
k = 1;
figure
hold on
while k < max_itr
err = 0;
for i = 1 : no_eqn
s = 0;
for j = 1 : no_eqn
s = s-A(i,j)*x1(j);
end
s = (s+A(i,no_eqn+1))/A(i,i);
if abs(s) > err
err = abs(s);
end
x1(i) = x1(i) + s
end
if err <= tolerance
break;
else
k = k+1;
plot (x(i)),k-1) %%%%% the plot does not show any data in the figure
end
end
fprintf('Solution vector after %d iterations is :\n', k-1)
for i = 1 : no_eqn
fprintf(' %11.8f \n', x1(i))
end
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!