what is the problem with this code gauss siedel . Only processing one solution but it should be twenty solutions

조회 수: 1 (최근 30일)
clc
clear all
area=0.01; % Cross sectional area (m^2)
l=1; % Length of the rod (m)
k=5; % Thermal conductivity (W/mK)
g=20000; % Heat generation (W/m^3)
T1=100; % Temperature at one end (At x=0)
T2=500; % Temperature at the other end (At x=1m)
delta_x=0.05; % Cell spacing
n=20; % Number of cells
s= (k*area)/delta_x;
% Formulate the matrix form Ax=B
% Define matrix A
A=zeros(n,n);
A(1,1)= 3*((k*area)/delta_x);
A(20,20)=A(1,1);
A(2,1)= -((k*area)/delta_x);
A(20,19)=A(2,1);
% Define matrix B
B(1)=g*area*delta_x+2*s*T1;
B(2:19)=g*area*delta_x;
B(20)=g*area*delta_x+2*s*T2;
% For solution matrix x
x(1:n)=150; % Initial guess is taken as 150 at all nodes
normVal=Inf;
tol=1e-3; GaussItr=0;
plotGauss=[];
while normVal>tol
x_old=x;
for i=1:n
sigma=0;
for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end
for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end
x(i)=(1/A(i,i))*(B(i)-sigma);
end
GaussItr=GaussItr+1;
normVal=norm(x_old-x);
plotGauss=[plotGauss;normVal];
end
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f\n%f in %d iterations',x,GaussItr);

답변 (0개)

카테고리

Help CenterFile Exchange에서 Special Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by