Thomas Algorithm
Problem-10:
function[]=thomas_algo()
clc
a=input('Enter the value a:');
b=input('Enter the value b:');
alp=input('Enter the value alpha:');
bta=input('Enter the value of beta:');
h=input('Enter the value h:');
n=((b-a)/h);
function fn1=p(x)
    fn1=2;
end
function fn2=q(x)
    fn2=-1;
end
function fn3=r(x)
    fn3=x*exp(x)-x;
end
A=zeros(n-1,n-1);
A(1,1)=1;
A(n+1,n+1)=1;
B(1)=alp;
B(n+1)=bta;
for i=2:n
    x=a+(i)*h;
    A(i,i-1)=1+(h*(p(x)))/2;
    A(i,i)=-h*h*q(x)-2;
    A(i,i+1)=1-(h*(p(x)))/2;
    B(i)=h*h*r(x);
end
A
b=B'
M=[A b]
t=size(M);
n=t(1);
for i=1:n-1
    M(i,:)=M(i,:)/M(i,i);
    M(i+1,:)=M(i+1,:)- M(i,:)*M(i+1,i);
end
new_M=M
Y(n)=M(n,n+1);
for i=n-1:-1:1
    Y(i)= M(i,n+1)-Y(i+1)*M(i,i+1);
end
Y
for j=1:n
    m(j)=a+(j)*h;
    fprintf('when x=%.2f ,y=%0.4f \n',m(j),Y(j))
end
plot(m',Y,'r--o')
title('BVP by thomas algo')
xlabel('values of x')
ylabel('values of y')
end
                                          Solution:
Enter the value a:1
Enter the value b:2
Enter the value alpha:0
Enter the value of beta:-4
Enter the value h:0.2
A=
  1.0000      0         0      0      0       0
  1.2000 -1.9600 0.8000        0      0       0
     0     1.2000 -1.9600 0.8000      0       0
     0        0       1.2000 -1.9600 0.8000   0
     0        0         0    1.2000 -1.9600 0.8000
     0        0         0      0      0       1.0000
b=
  0.1711
  0.2530
  0.3636
  0.5111
 -4.0000
M=
 1.0000     0       0        0      0       0        0
 1.2000 -1.9600 0.8000       0      0       0       0.1711
     0    1.2000 -1.9600 0.8000     0       0       0.2530
     0     0      1.2000 -1.9600 0.8000     0       0.3636
     0     0        0      1.2000 -1.9600 0.8000 0.5111
     0     0        0       0        0      1.0000 -4.0000
new_M =
 1.0000     0       0       0       0        0        0
     0    1.0000 -0.4082     0       0       0      -0.0873
     0      0      1.0000 -0.5441    0       0      -0.2433
     0      0       0      1.0000 -0.6121    0      -0.5016
     0      0       0       0       1.0000 -0.6528 -0.9082
     0      0       0       0       0       1.0000 -4.0000
Y=
     0 -0.7764 -1.6884 -2.6557 -3.5194 -4.0000
when x=1.20 ,y=0.0000
when x=1.40 ,y=-0.7764
when x=1.60 ,y=-1.6884
when x=1.80 ,y=-2.6557
when x=2.00 ,y=-3.5194
when x=2.20 ,y=-4.0000
Graph: