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

Matlab Class 09

The document covers various methods for solving linear systems, including Cramer's Rule, Gaussian Elimination, LU Decomposition, and Gauss-Jordan elimination. It provides examples and MATLAB code snippets for implementing these methods, demonstrating how to solve practical problems such as calculating sales from a concession stand and hours worked at different jobs. The content is aimed at students in a computer programming and applications course within a mechanical engineering context.

Uploaded by

Jose Aguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views58 pages

Matlab Class 09

The document covers various methods for solving linear systems, including Cramer's Rule, Gaussian Elimination, LU Decomposition, and Gauss-Jordan elimination. It provides examples and MATLAB code snippets for implementing these methods, demonstrating how to solve practical problems such as calculating sales from a concession stand and hours worked at different jobs. The content is aimed at students in a computer programming and applications course within a mechanical engineering context.

Uploaded by

Jose Aguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

ME 202- Computer Programming and Applications

Class 09
Class 07

1. Direct Method
2. Indirect Methods

Louay Alroomi, Ph.D.


Assistant Professor
Department of Mechanical Engineering
San Diego State University
Email: lalroomi@sdsu.edu
• Cramer’s Rule:
function [A, b] = CramerRule1(issquare,
sol_x)
A = [10 3 1;3 10 2;1 2 10] ;
A = [10 3 1;3 10 2;1 2 10];
b = [19 29 35]';
b=[19 29 35]';
[m,n] = size(A) ;
issquare = (size(A,1) == size(A,2));
x = zeros(m,1) ;
for i = 1:size(b,1)
for i = 1:m
if det(A)~=0 && issquare == (size(A,1)
T = A ;
== size(A,2))
T(:,i) = b ;
A_i=A;
x(i) = det(T)/det(A) ;
A_i(:,i)=b(:);
end
sol_x=[det(A_i)/det(A)]
% Simply use \
else
sol = A\b ;
sol_x=[]
[x sol]
end
end
• Gaussian Elimination Method:

• Gaussian elimination transforms a linear system Ax = b to an equivalent


linear system Ux = c, where U is the row-echelon matrix.
• Gaussian elimination uses only elementary row operations in the
transformation.

[ ][ ] [ ] [ ][ ] [ ]
1 2 3 𝑥 9 1 2 3 𝑥 9
2 −1 1 𝑦 = 8 0 −5 5 𝑦 = − 10
3 0 −1 𝑧 3 0 0 −4 𝑧 − 12
Elementary row operations
• Three elementary row operations can be performed on a set
of linear equations without changing the solution
1. Interchanging rows
2. Scaling a row by a constant
3. Replacing a row with a linear combination of the original row
and a multiple of any other row
Gaussian Elimination Method:
You are running a concession stand at a basketball game.
Ex: You are selling hot dogs, chips, and sodas. Each hot dog
costs $1.50, chip costs $1, and soda costs $0.50. At the
end of the night, you made a total of $98.5. You sold a
total of 107 hot dogs, chips, and sodas combined. The
number of sodas sold is twice that of hot dogs. You must
report the number of each sold. Solve this problem using
•Gaussian elimination.
number of hot dogs sold: x
• number of chips sold: y
• number of sodas sold: z

x + y + z = 107
1.5x + y + 0.5z = 98.5
z = 2x -> 2x – z = 0
Matrix form:  Ax = b
Ex: Make a function that solves a linear system with three
linear equations using Gaussian Elimination. The
function’s head should be: Example run for the following equations:
• x + y + z = 107
• 1.5x + y + 0.5z = 98.5

function x = myGE3(A, b) • z = 2x -> 2x – z = 0

[ |]
𝑎 11 𝑎 12 𝑎 13 𝑏1 >> A = [1 1 1; 1.5 1 0.5; 2 0 -1];
>> B = [107; 98.5; 0];
𝑎 21 𝑎 22 𝑎 23 𝑏2 >> myGE3(A,B)
𝑎 31 𝑎 32 𝑎 33 𝑏3 ans =
17

Recommended
56

steps:
34
function x = myGE3(A,b)

AG = [A b];
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) * AG(1,:);
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) * AG(1,:);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) * AG(2,:);
RE = AG;

x(3,1) = RE(3,4) / RE(3,3);


x(2,1) = (RE(2,4) - RE(2,3)*x(3)) / RE(2,2);
x(1,1) = (RE(1,4) - RE(1,3)*x(3) - RE(1,2)*x(2)) / RE(1,1);
end
function x = myGE3(A, b)
For 3
Linear AG = [A b];
AG(2,:) = AG(2,:) - AG(2,1) / AG(1,1) * AG(1,:);
Equations AG(3,:) = AG(3,:) - AG(3,1) / AG(1,1) * AG(1,:);
AG(3,:) = AG(3,:) - AG(3,2) / AG(2,2) * AG(2,:);
RE = AG;

x(3,1) = RE(3,4) / RE(3,3);


x(2,1) = (RE(2,4) - RE(2,3)*x(3)) / RE(2,2);
x(1,1) = (RE(1,4) - RE(1,3)*x(3)
- RE(1,2)*x(2)) / RE(1,1);
end

For N function x = myGE(A, b)


Linear [row, col] = size(A); % Assume A is a square matrix
AG = [A b]; % Assume b is a column vector
Equations for r = 2:row
for c = 1:r-1
S = AG(r, c) / AG(c, c) * AG(c, :);
AG(r, :) = AG(r, :) - S;
end
end
RE = AG;
for r = row:-1:1
S = 0;
for c = col:-1:r+1
S = S + RE(r, c) * x(c);
end
x(r,1) = (RE(r, col+1) - S) / RE(r, r);
end
end
a = [1 -1 2 -1 -8
2 -2 3 -3 -20
1 1 1 0 -2
1 -1 4 3 4];
%Gauss elimination method [m¡n)=size(a);
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
Info = [6 3 2;6 4 3;20 15 12]; t=a(j,:);a(j,:)=a(z,:);
b = [6;0;0]; a(z,:)=t;
A = [Info b]; end
for i =1:size(A,1) end
for j = i+1:size(A,1) for i=j+1:m
key1 = A(j,i)./A(i,i); a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
A(j,:) = A(j,:)-key1.*A(i,:); end
end end
end x=zeros(1,m);
x = zeros(1,size(Info,2)); for s=m:-1:1
for i = size(A,1):-1:1 c=0;
hg = sum(A(i,i+1:end-1).*x(i+1:end)); for k=2:m
x(i) = (A(i,end)-hg)./A(i,i); c=c+a(s,k)*x(k);
end end
disp(x) x(s)=(a(s,n)-c)/a(s,s);
end
• LU Decomposition Method:
b = [2 1 5]';
A = [2 -1 1;-1 2 -1;1 1 1];
N = length(A);
b = [2 1 5]';
L = zeros(N,N);
s=length(A);
U = zeros(N,N);
U=A;
for a = 1:N
L=zeros(s,s);
L(a,a) = 1; % Compute the main diagonal
PV=(0:s-1)';
of matrix L
for j=1:s,
End
% Pivot Voting (Max value in this
U(1,:) = A(1,:); % Compute first row of
column first)
Matrix U
[~,ind]=max(abs(U(j:s,j)));
L(:,1) = A(:,1)/U(1,1); % Compute first
ind=ind+(j-1);
column of Matrix L
t=PV(j); PV(j)=PV(ind); PV(ind)=t;
for i = 2:N
t=L(j,1:j-1); L(j,1:j-1)=L(ind,1:j-1);
for j = i:N
L(ind,1:j-1)=t;
U(i,j) = A(i,j)-L(i,1:i-1)*U(1:i-
t=U(j,j:end); U(j,j:end)=U(ind,j:end);
1,j)
U(ind,j:end)=t;
end % Calculate the remaining rows of U
% LU
matrix
L(j,j)=1;
for k = i+1:N
for i=(1+j):size(U,1)
L(k,i) = (A(k,i)-L(k,1:i-1)*U(1:i-
c= U(i,j)/U(j,j);
1,i))/U(i,i)
U(i,j:s)=U(i,j:s)-U(j,j:s)*c;
end %Calculate the remaining columns of
L(i,j)=c;
L matrix
end
end
end
Y = L\b;
P=zeros(s,s);
Gauss-Jordan elimination
• Gauss-Jordan elimination reduces Ax = b to a row-reduced echelon form,
Rx = d
Row-reduced echelon form
• The rref function in MATLAB transforms a system of equations in the
augmented matrix form [A|b] to the row-reduced echelon augmented
matrix form [R|d]
Gauss-Jordan elimination example
Linear system Augmented form

[ |] [ | ]
1 2 3 9 𝑹 𝟐=𝑹𝟐 −𝟐 𝑹𝟏 1 2 3 9
2 −1 1 8 0 −5 − 5 −10
3 0 −1 3 3 0 −1 3

[ | ] [ | ]
𝑹 = 𝑹 − 𝑹𝟐 1
𝑹 𝟑=𝑹𝟑 −𝟑 𝑹𝟏1 2 3 9 𝟑 𝟑
𝟓 2 3 9
0 −5 −5 − 10 0 −5 − 5 − 10
0 −6 − 10 −24 0 0 − 4 − 12
Row-Echelon form obtained
by Gaussian elimination
Gauss-Jordan elimination example

[ | ] [
𝑹 𝟐=− 𝑹 𝟐 /𝟓 1
| ]
1 2 3 9 2 3 9
0 −5 − 5 − 10 0 1 1 2
0 0 − 4 − 12 0 0 − 4 − 12

[ |] 𝑹 𝟐=𝑹𝟐 − 𝑹 𝟑 1
[ | ]
𝑹 𝟑=− 𝑹 𝟑 /𝟒 1 2 3 9 2 3 9
0 1 1 2 0 1 0 −1
0 0 1 3 0 0 1 3

𝑹 𝟏=𝑹𝟏 −𝟐 𝑹𝟐 1
[ | ] 𝑹 𝟏=𝑹𝟏 −𝟑 𝑹𝟑1
[ | ]
0 3 11 0 0 2
0 1 0 −1 0 1 0 −1
0 0 1 3 0 0 1 3

Row-Reduced Echelon form


obtained by Gaussian Jordan
Ex: Each week, you work a total of 20 hours at a bookstore,
pharmacy, and restaurant. You work 6 more hours at the
bookstore than at the pharmacy, and 1.5 times more
hours at the bookstore than the total hours at the
drugstore and restaurant. How many hours do you work
for the bookstore?
• Hours working for bookstore = x
• Hours working for drugstore = y
• Hours working for restaurant = z

x + y + z = 20
x = y + 6  x-y = 6
x = 1.5(y + z)  x – 1.5y – 1.5 = 0

Matrix form:  Ax = b
Gauss-Jordan elimination example
Linear system Augmented form

[ ][ ] [ ] [ |] [
𝑹 𝟐=𝑹𝟐 − 𝑹 𝟏 1
| ]
1 1 1 20 1 1 20
1 1 1 𝑥 20
1 −1 0 6 0 −2 −1 − 14
1 −1 0 𝑦 = 6
1 −1 . 5 −1 .5 0 1 −1 .5 − 1. 5 0
1 −1 . 5 − 1 .5 𝑧 0

[ | ] [
20 𝑹 𝟑=𝑹𝟑 −𝟏 . 𝟐𝟓 𝑹𝟏1
| ]
1 1 20
𝑹 𝟑= 𝑹𝟑 − 𝑹 𝟏1 1 1
0 −2 −1 −14
0 −2 − 1 −14
0 −2 . 5 − 2 .5 −20 0 0 −1 . 25 − 2. 5
Row-Echelon form obtained by Gaussian elimination

[ | ] [ |]
𝑹𝟑

[ | ]
𝑹 𝟑 =− 1 1 1 20 𝑹 = 𝑹 + 𝑹1
𝟐 𝟐
1 1 20 𝑹 𝟐=𝑹𝟐 /−𝟐 1 1 1 20
𝟏 .𝟐𝟓 0 −2 − 1 − 14 0𝟑 −2 0 − 12 0 1 0 6
0 0 1 2 0 0 1 2 0 0 1 2

𝑹 𝟏=𝑹𝟏 − 𝑹 𝟑1
[ | ] 𝑹 𝟏=𝑹𝟏 − 𝑹 𝟐1
[ | ]
1 0 18 0 0 12 𝑥=12
0 1 0 6 0 1 0 6 𝑦=6
0 0 1 2 0 0 1 2 𝑧 =2
Make a function that solves a linear system with three
linear equations using Gauss Jordan method. The
function’s head should be:
function
𝑎 11 𝑎 12 𝑎x13=
𝑏1myGJ3(A, b) function x = myGJ3(A,b)

[ 𝑎 21
𝑎 31
𝑎 22
𝑎 32 |]
𝑎 23 𝑏2
𝑎 33 𝑏3
AG = [A b];
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) *
AG(1,:);

Recommended steps:
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) *
AG(1,:);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) *
AG(2,:);
RE = AG;

RE(1,:) = RE(1,:) / RE(1,1);


RE(2,:) = RE(2,:) / RE(2,2);
RE(3,:) = RE(3,:) / RE(3,3);

RE(2,:) = RE(2,:) - RE(3,:) * RE(2,3);


RE(1,:) = RE(1,:) - RE(3,:) * RE(1,3);
RE(1,:) = RE(1,:) - RE(2,:) * RE(1,2);
RRE = RE;

x(1,1) = RRE(1,4);
x(2,1) = RRE(2,4);
x(3,1) = RRE(3,4);
end
For 3 Linear Equations For N Linear Equations
function x = myGJ3(A,b) function x = myGJ(A,b)
AG = [A b]; [row, col] = size(A); % Assume A is a square matrix
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) * AG = [A b];
AG(1,:); for r = 2:row
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) * for c = 1:r-1
AG(1,:); S = AG(r, c) / AG(c, c) * AG(c, :);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) * AG(r, :) = AG(r, :) - S;
AG(2,:); end
RE = AG; end
RE = AG;
RE(1,:) = RE(1,:) / RE(1,1);
RE(2,:) = RE(2,:) / RE(2,2); for r = 1:row
RE(3,:) = RE(3,:) / RE(3,3); RE(r,:) = RE(r,:) / RE(r,r);
end
RE(2,:) = RE(2,:) - RE(3,:) * RE(2,3);
RE(1,:) = RE(1,:) - RE(3,:) * RE(1,3); for r = row-1:-1:1
RE(1,:) = RE(1,:) - RE(2,:) * RE(1,2); S = 0;
for c = col:-1:r+1
RRE = RE; S = S + RE(c, :) * RE(r, c);
end
x(1,1) = RRE(1,4); RE(r, :) = RE(r, :) - S;
x(2,1) = RRE(2,4); end
x(3,1) = RRE(3,4);
end RRE = RE;

for r = 1:row
x(r,1) = RRE(r,4);
end
end
Ex: Make a function that returns the inverse matrix of a 3x3
matrix using the Gauss Jordan method. The function’s
head should be:

function invMat = myInvMat(A)


function invMat = myInvMat(A)
AG = [A eye(3)];
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) *
AG(1,:);
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) *
AG(1,:);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) *
AG(2,:);
RE = AG;

RE(1,:) = RE(1,:) / RE(1,1);


RE(2,:) = RE(2,:) / RE(2,2);
RE(3,:) = RE(3,:) / RE(3,3);

RE(2,:) = RE(2,:) - RE(3,:) * RE(2,3);


RE(1,:) = RE(1,:) - RE(3,:) * RE(1,3);
RE(1,:) = RE(1,:) - RE(2,:) * RE(1,2);
RRE = RE;

invMat = RRE(:, 4:6);


end
Ex: Make a function that returns the inverse matrix of a NxN
matrix using the Gauss Jordan method. The function’s
head should be:

function invMat = myInvMat(A)


Example Run:
>> A = [2 5 0 8; 1 4 2 6; 7 8 9 3; 1 5 7 8]
A=
2 5 0 8
1 4 2 6
7 8 9 3
1 5 7 8
>> myInvMat(A)
ans =
0.9609 -1.9162 0.0782 0.4469
-1.0335 2.3575 0.0670 -0.7598
-0.0056 -0.2737 0.0112 0.2067
0.5307 -0.9944 -0.0615 0.3631
For 3 Linear Equations For N Linear Equations
function invMat = myInvMat(A) function invMat = myInvMat(A)
AG = [A eye(3)]; [row, col] = size(A); % Assume A is a square matrix
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) * AG = [A eye(row)];
AG(1,:); for r = 2:row
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) * for c = 1:r-1
AG(1,:); S = AG(r, c) / AG(c, c) * AG(c, :);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) * AG(r, :) = AG(r, :) - S;
AG(2,:); end
RE = AG; end % Make the elements under the main diagonal of
matrix A not an identity matrix equal to zero
RE(1,:) = RE(1,:) / RE(1,1); RE = AG;
RE(2,:) = RE(2,:) / RE(2,2);
RE(3,:) = RE(3,:) / RE(3,3); for r = 1:row
RE(r, :) = RE(r, :) / RE(r, r);
RE(2,:) = RE(2,:) - RE(3,:) * RE(2,3); end
RE(1,:) = RE(1,:) - RE(3,:) * RE(1,3);
RE(1,:) = RE(1,:) - RE(2,:) * RE(1,2); for r = row-1:-1:1
S = 0;
RRE = RE; for c = col:-1:r+1
S = S + RE(c, :) * RE(r, c);
invMat = RRE(:, 4:6); end
end RE(r, :) = RE(r, :) - S;
end % Make the elements above the main diagonal of
matrix A not an identity matrix equal to zero while the
elements in the main diagonal equal to 1

RRE = RE;

invMat = RRE(:, col+1:2*col);


end % The inverse matrix represents the matrix to the R.H.S beside the identity matrix
a = [10 3 1 19
3 10 2 29
Ex:
1 2 10 35];
[m,n]=size(a);
for j=1:m-1
for z=2:m %Switch the elements in row1 with
row2 if a(1,1)=0
if a(j,j)==0
A = [10 3 1;3 10 2;1 2 10]; t=a(1,:);a(1,:)=a(z,:);
b = [19 29 35]'; a(z,:)=t;
N = length(b); %number of columns end
x = zeros(N,1); end
Aug = [A b] for i=j+1:m
for j = 1:N % for columns a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
Aug(j,:) = Aug(j,:)/Aug(j,j) End %Make the elements under the main
for i = 1:N %for rows diagonal equal to zero
if i~=j end
m = Aug(i,j) for j=m:-1:2
Aug(i,:) = Aug(i,:)-m*Aug(j,:) for i=j-1:-1:1
end a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end end
end End %Make the elements above the main
diagonal equal to zero
for s=1:m
a(s,:)=a(s,:)/a(s,s);
• Matrix Inversion Method:

• The “x” values are the unknowns, and “a” and “b” represent constants.

Check whether the following matrix is


singular.
[ 1
−3
−2
6 ]
• Remember:
• A-1 does not exist for a singular matrix A.
• You will not be able to solve Ax = b if A is singular.
Linear System
• A linear system is a set of linear equations.

• The solution is a vector of all the x values that


satisfy every linear equation in the linear system.
Solving Linear System

 A-1Ax = A-1b
Ix = A-1b
x = A-1b

(Ex) I
Solving matrices: Ax = b
Ax = b  A-1Ax = A-1b
Ix = A-1b
x = A-1b
Jane is using an ATD phone plan, which costs $20 per
Ex: month, with calls costing an additional 50 cents per
minute. Another company, Spring, charges $40 per
month, but calls cost only 10 cents per minute. If Jane
uses more than a certain time of calls per month, it will be
beneficial to switch to Spring. What is that amount of
•time?
Assuming Jane uses her phone about “t” min per month.
• Assuming “c” = total costs

ATD: c = 20 + 0.5t
Two linear equations
Spring: c = 40 + 0.1t = A linear system with two linear equations

Matrix form:  Ax = b
Ex:
You are running a concession stand at a basketball game.
You are selling hot dogs and sodas. Each hot dog costs
$1.50 and each soda costs $0.50. At the end of the night,
you made a total of $78.50. You sold a total of 87 hot dogs
and sodas combined. You must report the number of hot
dogs sold and the number of sodas sold. How many hot
dogs were sold and how many sodas were sold?
• # of hot dogs sold = x
• # of sodas sold = y

1.5x + 0.5y = 78.5


x + y = 87
Matrix form:  Ax = b
Backward/forward substitution
• If you have a triangular coefficient matrix A, solving Ax = b is simple.

[ ][ ] [ ]
1 2 3 𝑥 0𝑦 + 3 𝑧 =1 𝑥 +2 ∗ ( − 2 ) +3 ∗3=10 → 𝒙=𝟖
1 𝑥1+2
0 1 5 +5 𝑧 =1 𝑦 +5 ∗ 3=13 → 𝒚 =− 𝟐
𝑦 =1 𝑦13
0 0 4 𝑧 412 𝑧 =12 → 𝒛 =𝟑

[ ][ ] [ ]
1 0 0 𝑥 1𝑥 1= 1 → 𝒙 =𝟏
2 1 0 2 𝑦 =2 ∗1+1 𝑦 =2 → 𝒚 =𝟎
2 𝑥+1
𝑦 =
1 2 1 3 𝑦 + 1 𝑧 =1 ∗1+ 2∗ 0+1 𝑧 =3 → 𝒛 =𝟐
𝑧 1 𝑥 +2
• Gauss Jacobi Method:
clear all
clc function x = Jacobi1(A, b, Eps)
A = [27 6 -1;6 15 2;1 1 54]; A = [27 6 -1;6 15 2;1 1 54];
b = [85;72;110]; b = [85;72;110];
maxerr = 1e-5; Eps = 1e-5;
x = zeros(1,size(A,1)); n = length(b) ;
n = size(A,1); x0 = zeros(3, 1) ;
err = Inf; x = ones(size(x0)) ;
itr = 0; while norm(x-x0, inf) >= Eps
while all(err>maxerr) x0 = x ;
xold = x; for i = 1 : n
for i = 1:n x(i) = b(i) ;
sum = 0; for j = 1 : n
for j = 1:n if j ~= i
if j~=i x(i) = x(i) - A(i, j)*x0(j) ;
sum = sum+A(i,j)*xold(j); end
end end
end x(i) = x(i) / A(i, i) ;
x(i)=(1/A(i,i))*(b(i)-sum); end
end end
itr = itr + 1;
y(itr,:) = x;
err = abs(xold-x);
end
disp(x)
• Gauss-Seidel Method:
A = [10 3 1;3 10 2;1 2 10];
A = [10 3 1;3 10 2;1 2 10]; b = [19;29;35];
B = [19;29;35]; x=[0 0 0]'
P = [0;0;0]; n=size(x,1);
n = 10; normVal=Inf;
e = 0.0001; tol=1e-5; itr=0;
N = length(B); while normVal>tol
X = zeros(N,1); x_old=x;
Y = zeros(N,1);%for stopping criteria for i=1:n
for j = 1:n %Number of iteration sigma=0;
for i = 1:N for j=1:i-1
X(i) = (B(i)/A(i,i))-(A(i,[1:i- sigma=sigma+A(i,j)*x(j);
1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i); end %summation of previous
P(i) = X(i); iteration
end for j=i+1:n
fprintf('Iteration no %d\n',j)
X sigma=sigma+A(i,j)*x_old(j);
if abs(Y-X)<e end %summation of current iteration
break x(i)=(1/A(i,i))*(b(i)-sigma);
end end%solution of vector x of current
Y = X; iteration
end itr=itr+1;
normVal=norm(x_old-x);
end
disp(x)
• Successive Over Relaxation Method:
clear all
clc if e < 1
A = [2 -1 0;-1 2 -1;0 -1 2]; while sum(abs(err)<=maxerr)~=size(A,1)
b = [7; 1; 1]; xnew = H*x+C;
x = [0;0;0]; err = xnew-x;
maxiter = 50; x = xnew;
err = 1e-5; iter = iter + 1;
maxerr = 0.04; y(iter,:) = x;
w = 0.8; if iter==maxiter
D = diag(diag(A)); break
L = tril(A)-D; end
U = triu(A)-D; end
H = inv(D+w*L)*((1-w)*D-w*U); else
C = w*inv(D+w*L)*b; disp('Not Converged');
e = max(eig(H)); end
iter = 1;
y(1,:) = x;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy