Sie sind auf Seite 1von 6

EN.530.

371 Quantitative Applications in Mechanical Engineering


Mechanical Engineering
Johns Hopkins University
Fall 2014
David R. B. Kraemer


HW2, due Mon 22 Sep

A. 1. Write a Matlab program to perform the Gaussian elimination to solve a matrix equation.

Here is the skeleton of a suggested program.
function x= GaussElim(A,b)
%
Forward elimination:
for
%
for
%
for
%

loop through pivots


loop through rows
loop through columns

end
end
end
%

Backward substitution:

for
for

%
%

loop through rows


loop through rows

end
end

Submit the commented program.


A. 2. Use your program to solve the following matrix equation:
2 1 0 !
1

1 2 1
! = 2
0 1 2 !
3
Submit the output from the program.


A. 3. Use your program to solve the following matrix equation:
0 1 2 !
3
2 1 0 ! = 1
1 2 1 !
2
Submit the output from the program. Comment briefly.

B.1. Modify your Gaussian Elimination program to incorporate partial pivoting.



Here is the skeleton of a suggested program.

function x= GaussElimPivot(A,b)
%
Forward elimination with partial pivoting:
for
%
loop through pivots
%
Partial pivoting:
for
%
loop through other rows
if
% check to see if entry is larger...
% than pivot entry;
% if so, store max value and rows
end
end
if
% if another entry is bigger, then...
for
% loop through columns
% switch the pivot row with...
% the one with bigger entry
end
end
%
Forward elimination:
for
%
loop through rows
for
%
loop through columns
end
end
end
%
Backward substitution:
for
%
for
%

loop through rows


loop through rows

end
end

Submit the commented program.


B. 2. Use your program to solve the following matrix equation:
2 1 0 !
1
1 2 1 ! = 2
0 1 2 !
3
Submit the output from the program.


B. 3. Use your program to solve the following matrix equation:
0 1 2 !
3
2 1 0 ! = 1
1 2 1 !
2
Submit the output from the program. Comment briefly.

C. Set up and solve the following boundary-value problem:



How far will an ordinary piece of lumber sag under its own weight and a 12-pack of soda (10.5
pounds) placed in the middle if it is supported on each end?

Lumber data:
2x4 (cross-section dimensions, in inches, nominal. Note that this is 2 inches by 4 inches
rough, before finishing. Look up the actual dimensions for typical lumber 2x4)
Material: pine
Length: 8 feet

The beam lies flat (the wide dimension of the cross-section is horizontal), and it is simply
supported on each end.

Solve the beam bending equation:
!
! =

where E is the modulus of elasticity, I is the area moment of inertia of the cross-section of the
beam about its neutral axis (in this case, I is constant), y is the deflection of the beam from its rest
position, x is the distance along the beam, and M is the bending moment about the origin (x=0) (in
this problem, the bending moment is a function of x).

Divide the domain into 11 elements (i.e. 12 nodes, with 10 of these nodes being interior; n = 10).

I would recommend using SI units, to maintain your sanity.

Solve the problem using your Gaussian Elimination program.

Plot your results for the deflection of the beam.

What is the predicted maximum deflection of the beam?

9/20/14 5:15 PM

C:\Users\PauloHenrique\Docu...\GaussElim.m

1 of 1

% This function performs Gaussian Elimination to solve


% the matrix equation A x = b for the unknown vector x.
% Paulo Werner
%/09/12/2014
function x = GaussElim(A, b)
[rows,columns] = size (A);
if rows ~= columns
error('Matrix A must be square.');
end
AugMatrix = [A b]; % Form the augmented matrix where the b colum is concatenated onto A
TempMatrix = zeros(1,columns+1);
% Forward Elimination
for pivot = 1:rows-1
% Loop through pivots
for row = pivot+1:rows % Loop through rows
factor = AugMatrix(row,pivot)/AugMatrix(pivot,pivot);
for col = pivot:columns+1
% Loop through columns
AugMatrix(row,col) = AugMatrix(row,col)-factor*AugMatrix(pivot,col);
Eliminates elements below pivots
end
end
end

% Back Substitution
% Loop: row from # row down to 1
%
x(row,1) = [AugMatrix(row,#columns+1)-SUM[from col=row+1 to #columns](AugMatrix
(row,col)*x(col,1)]/AugMatrix(row,row)
% end
for row = rows:-1:1 %Because it's a back substitution, the size of x is limited by the
number of rows, so it's not necessary to define x before the loop
x(row,1) = AugMatrix(row,columns+1);
for col = row+1:columns
x(row,1) = x(row,1) - AugMatrix(row,col)*x(col,1);
end
x(row,1)=x(row,1)/AugMatrix(row,row);
end
return

9/20/14 5:26 PM
%
%
%
%

C:\Users\PauloHenriq...\GaussElimPartPiv.m

1 of 2

This function performs Gaussian Elimination to solve...


the matrix equation A x = b for the unknown vector x.
Paulo Werner
09/12/2014

function x = GaussElimPartPiv(A, b)
[rows,columns] = size (A);
if rows ~= columns
error('Matrix A must be square.');
end
AugMatrix = [A b]; % Form the augmented matrix where the b colum is concatenated onto A
TempMatrix = zeros(1,columns+1);
% Forward Elimination with partial pivoting
for pivot = 1:rows-1
% Loop through pivots
maxElement = 0;
% Initialize maxElement and set it equal to 0 (it will zero after
each loop)
maxRow = 0;
% Initialize maxRow and set it equal to 0 (it will zero after each
loop)
% Partial pivoting
for rowPartPiv = pivot+1:rows % Loop through other rows
if maxElement == 0
%If this is the first loop or no values greater than the
pivot have been found
if AugMatrix(rowPartPiv, pivot) > AugMatrix(pivot, pivot) %Check to see if
entry is larger than pivot entry
maxElement = AugMatrix(rowPartPiv,pivot);
% Store value of the largest
entry
maxRow = rowPartPiv;
TempMatrix(1,:) = AugMatrix(pivot,:); % Store elements of pivot row
end
else
if AugMatrix(rowPartPiv, pivot) > maxElement % Check to see if entry is
larger than pivot entry
maxElement = AugMatrix(rowPartPiv,pivot);
% Store value of the
largest entry
maxRow = rowPartPiv;
TempMatrix(1,:) = AugMatrix(pivot,:); % Store elements of pivot row
end
end
end
if maxElement ~= 0
AugMatrix(pivot,:) = AugMatrix(maxRow,:);
% Place row with largest value in the
pivot position as a pivot
AugMatrix(maxRow,:) = TempMatrix(1,:);
% Place former pivot row where row
that took its place was
end
for row = pivot+1:rows % Loop through rows
factor = AugMatrix(row,pivot)/AugMatrix(pivot,pivot);
for col = pivot:columns+1
% Loop through columns

9/20/14 5:26 PM

C:\Users\PauloHenriq...\GaussElimPartPiv.m

AugMatrix(row,col) = AugMatrix(row,col)-factor*AugMatrix(pivot,col);
Eliminates elements below pivots
end
end
end

2 of 2
%

% Back Substitution
% Loop: row from # row down to 1
%
x(row,1) = [AugMatrix(row,#columns+1)-SUM[from col=row+1 to #columns](AugMatrix
(row,col)*x(col,1)]/AugMatrix(row,row)
% end
for row = rows:-1:1 %Because it's a back substitution, the size of x is limited by the
number of rows, so it's not necessary to define x before the loop
x(row,1) = AugMatrix(row,columns+1);
for col = row+1:columns
x(row,1) = x(row,1) - AugMatrix(row,col)*x(col,1);
end
x(row,1)=x(row,1)/AugMatrix(row,row);
end
return