[go: up one dir, main page]

0% found this document useful (0 votes)
9 views18 pages

MM25B049 Matlab Assignment 2

The document contains MATLAB code and outputs for solving systems of linear equations using Gaussian elimination. It includes examples of augmented matrices, transformed matrices, and back substitution results. Additionally, it features functions for time measurement and Gaussian elimination with partial pivoting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

MM25B049 Matlab Assignment 2

The document contains MATLAB code and outputs for solving systems of linear equations using Gaussian elimination. It includes examples of augmented matrices, transformed matrices, and back substitution results. Additionally, it features functions for time measurement and Gaussian elimination with partial pivoting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

MATLAB ASSIGNMENT - 2

Surya.B

MM25B049
>> ngaussel(i,c)

The augmented matrix is =​

augm =​

1.0000 0 0 0 0 0 0.8147​
0 1.0000 0 0 0 0 0.9058​
0 0 1.0000 0 0 0 0.1270​
0 0 0 1.0000 0 0 0.9134​
0 0 0 0 1.0000 0 0.6324​
0 0 0 0 0 1.0000 0.0975​


The transformed upper triangular augmented matrix C is =​


C =​

1.0000 0 0 0 0 0 0.8147​
0 1.0000 0 0 0 0 0.9058​
0 0 1.0000 0 0 0 0.1270​
0 0 0 1.0000 0 0 0.9134​
0 0 0 0 1.0000 0 0.6324​
0 0 0 0 0 1.0000 0.0975​


Back substitution gives the vector solution​

x =​

0.8147​
0.9058​
0.1270​
0.9134​
0.6324​
0.0975
>> c

c =​

0.8147​
0.9058​
0.1270​
0.9134​
0.6324​
0.0975
>> i

i =​

1 0 0 0 0 0​
0 1 0 0 0 0​
0 0 1 0 0 0​
0 0 0 1 0 0​
0 0 0 0 1 0​
000001
>> A = [1 1 1 1; 2 3 1 5; -1 1 -5 3; 3 1 7 -2];b = [10 31 -2 18]'; ngaussel(A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
2 3 1 5 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 1 -1 3 11​
0 0 -2 -2 -14​
0 0 0 -1 -4​


Back substitution gives the vector solution​

x =​

1​
2​
3​
4
>> A(2,:) = [1 1 1 1];ngaussel (A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
1 1 1 1 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 0 0 0 21​
0 0 NaN NaN -Inf​
0 0 0 NaN NaN​


Back substitution gives the vector solution​

x =​

NaN​
NaN​
NaN​
NaN
>> A(2,3) = 2;A(2,3) = 2

A =​

1 1 1 1​
1 1 2 1​
-1 1 -5 3​
3 1 7 -2
>> ngaussel (A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
1 1 2 1 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 0 1 0 21​
0 0 -Inf NaN -Inf​
0 0 0 NaN NaN​


Back substitution gives the vector solution​

x =​

NaN​
NaN​
NaN​
NaN
>> A(2,3) = 2;ngaussel (A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
1 1 2 1 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 0 1 0 21​
0 0 -Inf NaN -Inf​
0 0 0 NaN NaN​


Back substitution gives the vector solution​

x =​

NaN​
NaN​
NaN​
NaN
>> A(2,3)

ans =​

2
>> ngaussel (A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
1 1 2 1 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 0 1 0 21​
0 0 -Inf NaN -Inf​
0 0 0 NaN NaN​


Back substitution gives the vector solution​

x =​

NaN​
NaN​
NaN​
NaN

A =​

1 1 1 1​
1 1 2 1​
-1 1 -5 3​
3 1 7 -2
>> b

b =​

10​
31​
-2​
18
>> ngaussel (A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
1 1 2 1 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 0 1 0 21​
0 0 -Inf NaN -Inf​
0 0 0 NaN NaN​


Back substitution gives the vector solution​

x =​

NaN​
NaN​
NaN​
NaN
>> A\b

ans =​

-53.0000​
38.0000​
21.0000​
4.0000
>> A*[-53 38 21 4]'

ans =​

10​
31​
-2​
18

y =​

-53.0000 -26.5000​
38.0000 19.0000​
21.0000 10.5000​
4.0000 2.0000
>> help\
help\​
>> help \
\ Solve systems of linear equations Ax = B for x​
This MATLAB function solves the system of linear equations A*x = B.​

Syntax​
x = A\B​
x = mldivide(A,B)​

Input Arguments​
A - Operands​
vectors | full matrices | sparse matrices​
B - Operands​
vectors | full matrices | sparse matrices​

Output Arguments​

x - Solution​
vector | full matrix | sparse matrix​

Examples​
System of Equations​
Linear System with Singular Matrix​
Least-Squares Solution of Underdetermined System​
Linear System with Sparse Matrix​

See also mrdivide, ldivide, rdivide, inv, pinv, chol, lu, qr, ldl,​
linsolve, lsqminnorm, spparms, decomposition​

Introduced in MATLAB before R2006a​
Documentation for \​
Other uses of \

>> y=A\c

y =​

-53.0000 -26.5000​
38.0000 19.0000​
21.0000 10.5000​
4.0000 2.0000
>> B = inv(A)

B =​

5.5000 -3.0000 -1.5000 -1.0000​
-5.5000 2.0000 2.5000 2.0000​
-1.0000 1.0000 0 0​
2.0000 -0.0000 -1.0000 -1.0000
>> D = eye(4);C = A \ D

C =​

5.5000 -3.0000 -1.5000 -1.0000​
-5.5000 2.0000 2.5000 2.0000​
-1.0000 1.0000 0 0​
2.0000 -0.0000 -1.0000 -1.0000
>>
>>
>>
>>
>>
>> A*B

ans =​

1.0000 -0.0000 -0.0000 -0.0000​
0.0000 1.0000 -0.0000 -0.0000​
-0.0000 -0.0000 1.0000 0.0000​
0.0000 -0.0000 0.0000 1.0000
>> z=B*b

z =​

-53.0000​
38.0000​
21.0000​
4.0000
>> tngaussel(10)

ans =​

0.0026
>> tngaussel(122)

ans =​

0.0106

>> TimeIt(10,5)
Dimension​

n =​

10​

Matrix was full.​
Solution time (secs)​

t =​

0.0040
>> Timelt(11,2)
Dimension​

n =​

11​

Half bandwidth​

p =​

2​

Solution time (secs)​

t =​

0.0021

>> tngaussel(11)

ans =​

1.8700e-04
>> Timelt(11,4)
Dimension​

n =​

11​

Half bandwidth​

p =​

4​

Solution time (secs)​

t =​

0.0012
>> tngaussel(200)

ans =​

0.0081
>> Timelt(200,55)
Dimension​

n =​

200​

Half bandwidth​

p =​

55​

Solution time (secs)​

t =​

0.0084
>> tngaussel(1800)

ans =​

16.4507
>> Timelt(1800,200)

Dimension​

n =​

1800​

Half bandwidth​

p =​

200​

Solution time (secs)​

t =​

0.2318
>> n=[10 20 30 40];
>> gaussianElimination(A,b)

ans =​

-53.0000​
38.0000​
21.0000​
4.0000
>> A

A =​

1 1 1 1​
1 1 2 1​
-1 1 -5 3​
3 1 7 -2
>> ngaussel(A,b)

The augmented matrix is =​

augm =​

1 1 1 1 10​
1 1 2 1 31​
-1 1 -5 3 -2​
3 1 7 -2 18​


The transformed upper triangular augmented matrix C is =​


C =​

1 1 1 1 10​
0 0 1 0 21​
0 0 -Inf NaN -Inf​
0 0 0 NaN NaN​


Back substitution gives the vector solution​

x =​

NaN​
NaN​
NaN​
NaN
>> gaussianElimination(A,b)

ans =​

-53.0000​
38.0000​
21.0000​
4.0000
>> t=[0 0 0 0];
>> for i=1:4;t(i)=tngaussel(n(i));
end
>> plot(n,t,'-*')


function x = gauss_with_pivoting(A, b)

% GAUSS_WITH_PIVOTING Solves the system of linear equations Ax = b using

% Gaussian elimination with partial pivoting.

% Inputs:

% A - Coefficient matrix

% b - Right-hand side vector

% Output:

% x - Solution vector

n = size(A, 1);

% Augment the matrix A with the vector b

Ab = [A b];

% Forward elimination with partial pivoting


for k = 1:n-1

% Find the index of the maximum element in the current column

[~, maxIndex] = max(abs(Ab(k:n, k)));

maxIndex = maxIndex + k - 1;

% Swap the current row with the row of the maximum element

if maxIndex ~= k

Ab([k, maxIndex], :) = Ab([maxIndex, k], :);

end

% Perform elimination

for i = k+1:n

factor = Ab(i, k) / Ab(k, k);

Ab(i, k:n+1) = Ab(i, k:n+1) - factor * Ab(k, k:n+1);

end

end

% Back substitution

x = zeros(n, 1);

for i = n:-1:1

x(i) = (Ab(i, end) - Ab(i, i+1:n) * x(i+1:n)) / Ab(i, i);

end

end

You might also like