0% found this document useful (0 votes)
24 views1 page

Numerical hw2

The document provides instructions for a homework assignment to write code to iteratively solve a linear algebraic equation. Students are asked to modify provided code to improve stability and convergence and will be evaluated on their ability to solve ill-conditioned test cases.

Uploaded by

980911seo
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)
24 views1 page

Numerical hw2

The document provides instructions for a homework assignment to write code to iteratively solve a linear algebraic equation. Students are asked to modify provided code to improve stability and convergence and will be evaluated on their ability to solve ill-conditioned test cases.

Uploaded by

980911seo
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/ 1

ME301 Numerical Analysis

Homework #1
Due: April 11, 2024
Iterative methods
Write your own code called hw2 that finds the solution for the given linear algebraic equation.

- Put any other subfunctions, if any, in a single file entitled hw2.m, and submit your code only.
- You need to modify the code provided below to improve the stability and convergence.
- Extensions get extra credits. For example, efficiency, rich outputs, etc.
- Your code will be evaluated with three trial (A, b) sets including ill-conditioned cases.

% Skeleton: the driver


% ==============
% A: n x n matrix
% b: n x 1 vector
% x: n x 1 vector
% ==============
% 2024/04/04 Byeongchan Lee
% ==============
function x = hw2(A, b)
lambda = 1.0; % default: no relaxation

% call Gauss-Seidel (See lecture 10)


% change as necessary
[x, stat] = getGS(A, b, lambda);

% Your code goes here to improve convergence & stability


end

% skeleton code: you can change the whole function


% xnew: predicted solution
% stat: 0 = not converged, 1 = converged (you can make more options)
% ==============
% 2024/04/04 Byeongchan Lee
% ==============
function [xnew, stat] = getGS(A, b, lambda)

es = 1e-3; % stopping criterion


maxSteps = 100; % maximum number of iterations
stat = 0; % initially not converged
n = size(A, 1);
x = zeros(n,1); xnew = zeros(n,1); % initial guess = origin

for step = 1:maxSteps


for i = 1:n
xnew(i) = b(i);
xnew(i) = xnew(i) - A(i,1:(i-1))*xnew(1:(i-1));
xnew(i) = xnew(i) - A(i,(i+1):n)*x((i+1):n);
xnew(i) = xnew(i)/A(i,i);
end

if norm(1 - x./xnew) < es


stat = 1; return
end

x = lambda*xnew + (1-lambda)*x; % relaxation


end
end

You might also like