Quit[];

(* MATRICI TRIDIAGONALI - Algoritmo di Crout *)

(* ESERCIZIO 6.6.6a *)

n=3;
a={{1,-1,0,0},{-2,4,-2,-1},{0,-1,2,1.5}};
MatrixForm[a]

l[1][1]=a[[1,1]];
u[1][2]=a[[1]][[2]]/l[1][1];
z[1]=a[[1]][[n+1]]/l[1][1]; (* La y delle slides *)

For[i=2,i≤n-1,i++,l[i][i-1]=a[[i]][[i-1]];
l[i][i]=a[[i]][[i]]-l[i][i-1]*u[i-1][i];
u[i][i+1]=a[[i]][[i+1]]/l[i][i];
z[i]=(a[[i]][[n+1]]-l[i][i-1]*z[i-1])/l[i][i]
]
l[n][n-1]=a[[n]][[n-1]];
l[n][n]=a[[n]][[n]]-l[n][n-1]*u[n-1][n];
z[n]=(a[[n]][[n+1]]-l[n][n-1]*z[n-1])/l[n][n];

Clear[x];
x[n]=z[n];

For[i=n-1,i≥1,i--,x[i]=z[i]-u[i][i+1]*x[i+1]]

For[i=1,i≤n,i++,Print[x[i]]]

a1={{1,-1,0},{-2,4,-2},{0,-1,2}};
b={0,-1,1.5};
x={x1,x2,x3};
Solve[Dot[a1,x]==b,x]

(* ESERCIZIO 6.6.6d *)

n=4;
a={{0.5,0.25,0,0,0.35},{0.35,0.8,0.4,0.,0.77},{0,0.25,1.,0.5,-0.5},{0,0,1.,-2.,-2.25}};
MatrixForm[a]

l[1][1]=a[[1]][[1]];
u[1][2]=a[[1]][[2]]/l[1][1];
z[1]=a[[1]][[n+1]]/l[1][1];

For[i=2,i≤n-1,i++,l[i][i-1]=a[[i]][[i-1]];
l[i][i]=a[[i]][[i]]-l[i][i-1]*u[i-1][i];
u[i][i+1]=a[[i]][[i+1]]/l[i][i];
z[i]=(a[[i]][[n+1]]-l[i][i-1]*z[i-1])/l[i][i]
]
l[n][n-1]=a[[n]][[n-1]];
l[n][n]=a[[n]][[n]]-l[n][n-1]*u[n-1][n];
z[n]=(a[[n]][[n+1]]-l[n][n-1]*z[n-1])/l[n][n];

Clear[x];
x[n]=z[n];

For[i=n-1,i≥1,i--,x[i]=z[i]-u[i][i+1]*x[i+1]]

For[i=1,i≤n,i++,Print[x[i]]]

a1=a={{0.5,0.25,0,0},{0.35,0.8,0.4,0.},{0,0.25,1.,0.5},{0,0,1.,-2.}};
b={0.35,0.77,-0.5,-2.25}
x={x1,x2,x3,x4};
Solve[Dot[a1,x]==b,x]

a