Uploaded by people.balinese

matlab

advertisement
SOLUSI 1
n=input ('number of equation: ')
printf ("ordo matrix: ");
scanf ("%d",&n);
printf ("input matrix A : \n");
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
printf ("Matrix A[%d][%d] : ",i+1,j+1);
scanf ("%lg", &A[i][j]);
}
}
printf ("input matrix B : \n");
for (i=0;i<n;i++) {
printf ("Matrix B[%d] : ",i+1);
scanf ("%lg", &B[i]);
A[i][j]=B[i];
}
printf ("Augmented matrix [AB] : \n");
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
printf ("%4lg", A[i][j]);
}
printf ("\n");
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
C =A [j][i]/A[i][i];
for (k=0;k<=n+1;k++)
{
A[j][k] = A[j][k] - C*A[i][k];
}
}
}
for (i=0;i<n;i++)
{
b[i] = A[i][n];
}
x[n-1]=b[n-1]/A[n-1][n-1];
for (k=n-2; k>=0; k--)
{
sigma = 0;
for (j=k+1; j<n; j++)
{
sigma =sigma+A[k][j]*x[j];
}
x[k] =(b[k]-sigma)/ A[k][k];
}
printf ("Matrix: \n");
for (i=0;i<n;i++){
for (j=0;j<n+1;j++){
printf ("%4lg", A[i][j]);
}
printf ("\n");
}
printf ("solution: \n");
for (i=0;i<n;i++){
printf ("x-%d=%lg\n", i+1,x[i]);
}
SOLUSI 2
A=rand(15,15)
printf ("input matrix b : \n");
function lufact(A,b)
n=length(b);
y=zeros(n,1);
x=zeros(n,1);
fprintf('\n');
for i=1:n
U(i,i)=1;
end
L(1,1)=A(1,1)/U(1,1);
for j=2:n
L(j,1)=A(j,1)/U(1,1);
U(1,j)=A(1,j)/L(1,1);
end
for i=2:n-1
S=0;
for k=1:i-1
S=S+U(k,i)*L(i,k);
end
L(i,i)=(A(i,i)-S)/U(i,i);
for j=i+1:n
S=0;
for k=1:i-1
S=S+U(k,i)*L(j,k);
end
L(j,i)=(A(j,i)-S)/U(i,i);
S=0;
for k=1:i-1
S=S+U(k,j)*L(i,k);
end
end
S=0;
for k=1:n-1
S=S+U(k,n)*L(n,k);
end
L(n,n)=(A(n,n)-S)/U(n,n);
y(1)=b(1)/L(1,1);
for i=2:n
S=b(i);
for j=1:i-1
S=S-L(i,j)*y(j);
end
y(i)=S/L(i,i);
end
x(n)=y(n)/U(n,n);
for i=n-1:-1:1
S=y(i);
for j=i+1:n
S=S-U(i,j)*x(j);
end
x(i)=S/U(i,i);
end
L
disp(' ')
y
U
disp('solution=')
x
Download
Random flashcards
sport and healty

2 Cards Nova Aulia Rahman

Create flashcards