'/N Enter Number of Equation:': For For End End For For End For End End For For End End

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Batch- T2

PROGRAM TO SOLVE SIMULTANEOUS EQUATIONS BY


THOMAS ALGORITHM METHOD

n=input('\n Enter number of equation :');


for i=1:n
for j=1:n
a(i,j)=input('\n Enter Matrix Element :');
end
b(i,1)=input('\n Enter Constant:');
end
for i=2:n
fact=a(i,i-1)/a(i-1,i-1);
for j=1:n
a(i-1,j)=a(i-1,j)*fact;
end
b(i-1)=b(i-1)*fact;
for j=1:n
a(i-1,j)=a(i-1,j)*fact;
end
b(i-1)=b(i-1)*fact;
end
for i=1:n
dig=a(i,i);
for j=1:n
a(i,j)=a(i,j)/dig;
end
b(i)=b(i)/dig;
end
res(n)=b(n);
i=n-1:1:1
res(i)=b(i);
for j=i:n-1
res(i)=res(i)-res(j+1)*a(i,j+1);
end
i=1:n
fprintf('\n Value of x%d=%f',i,res(i));

PROGRAM OUTPUT

Enter number of equation :4


Enter matrix element row-wise :1
Enter matrix element row-wise :2
Enter matrix element row-wise :0
Enter matrix element row-wise :0
Enter matrix element row-wise :-1
Enter matrix element row-wise :1
Enter matrix element row-wise :2
Enter matrix element row-wise :0
Enter matrix element row-wise :0
Enter matrix element row-wise :1
Enter matrix element row-wise :3
Enter matrix element row-wise :1
Enter matrix element row-wise :0
Enter matrix element row-wise :0
Enter matrix element row-wise :2
Enter matrix element row-wise :2
Enter the second matrix element:4
Enter the second matrix element:1
Enter the second matrix element:7
Enter the second matrix element:8
Value of x1=0.000000
Value of x2=0.000000
Value of x3=0.000000
Value of x4=6.000000

You might also like