Gauss_seidel

  • Uploaded by: Ali Javed
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Gauss_seidel as PDF for free.

More details

  • Words: 242
  • Pages: 2
% % % %

THIS IS THE CODE FOR SOLVING SYSTEM OF LINEAR EQUATIONS WITH GAUSS-SEIDAL APPROACH. FOR THE CODE TO WORK, THE INPUT MATRIX 'a' SHOULD SATISFY FOLLOWING CONDITIONS:

% % %

1) THE MATRIX SHOULD BE DIAGONALLY DOMINANT. (THE ABSOLUTE VALUE OF DIAGONAL ELEMENT SHOULD BE GREATER THAN THE SUM OF ABSOLUTE VALUES OF REST OF THE ELEMENTS OF THAT ROW)

%

2) MATRIX A SHOUDL BE POSITIVE DEFNITE

clc clear %-------------------------------------------------------------------------% INPUTS %a=input('Enter Matrix starting from 1st row to 1st column') % -------------------------INPUTS ----------------------------------------a = [3.5 0.5 1; 2 -7 -1; -2 5 1] n = length(a); b = [1;2;1]; stopcrit=0.00001; % STOPPING CRITERION divcrit=10000; % STOPPING IN CASE OF DIVERGENCE %-------------------------------------------------------------------------format long count = 1; maxerr=10;

% LONG FORMAT FOR GREATER PRECISION % FOR CALCULATING NUMBER OF ITERATIONS % JUST ANY VALUE FOR INITIATION

%--------VARIFYING CONVERGENCE CRITERION-1 (DIAGONALLY DOMINANT MATRIX A) fail=0; exception=1; for i=1:n crit(i)=0; for j=1:n if j~=i crit(i)=crit(i)+abs(a(i,j)); end end if abs(a(i,i)) stopcrit)&& (maxerr < 1000) count = 1 + count;

for i=1:n xpp = b(i); for j=1:n if j~=i xpp = xpp-a(i,j).*xp(j); end end xn(i)=xpp/a(i,i); err(i)=abs(xp(i)-xn(i)); xp(i)=xn(i); end maxerr = max(err); end count maxerr xn end

More Documents from "Ali Javed"