Qustion in Numerical Analysis Write an efficient algorithm to solve an n times n
ID: 3109833 • Letter: Q
Question
Qustion in Numerical Analysis
Write an efficient algorithm to solve an n times n system (n greaterthanorequalto 3) with 4 nonzero diagonals of the form Ax = y where A = [a_1 b_1 c_1 0...0 d_2 a_2 b_2 c_2. 0 d_3 a_3 b_3.. 0 d_3 a_3 b_3 ... 0 d_4 a_4 .... 0 d_5... 0 ...0 ... c_n - 3 0 ...b_n - 2 c_n - 2 .. a_n - 1 b_n - 1 0 ... 0], x = [x_1 x_2 x_3 x_4.. x_n - 1 x_n], y = [y_1 y_2 y_3 y_4 .. y_n - 1 y_n] Assume that A is nonsingular and that pivoting is unnecessary. You should first convert to an upper-triangular system and then apply back-substitution. Fully exploit the sparsely pattern.Explanation / Answer
%% Mat lab code ( with function)
function u=Thomas_alg(a,b,c,f)
%u is x, f is y , Au=f form
n=length(f);
b1(1)=b(1);
f1(1)=f(1);
for i=1:n
c1(i)=c(i);
end
%changing upper traingular form
for i=2:n
b1(i)=b(i)-(c1(i-1)*(a(i)/b1(i-1)));
end
for i=2:n
f1(i)=f(i)-(f1(i-1)*(a(i)/b1(i-1)));
end
%back substitution
u(n)=f1(n)/b1(n);
for i=n-1:-1:1
u(i)=(f1(i)-c1(i)*u(i+1))/b1(i);
end
end
% Or
%% Matlab code ( without function)
a=[2 2 2 2];
b=[-1 -1 -1 -1];
c=[3 3 3 3];
f=[2 0 0 1];
n=length(f);
b1(1)=b(1);
f1(1)=f(1);
for i=1:n
c1(i)=c(i);
end
%changing upper traingular form
for i=2:n
b1(i)=b(i)-(c1(i-1)*(a(i)/b1(i-1)));
end
for i=2:n
f1(i)=f(i)-(f1(i-1)*(a(i)/b1(i-1)));
end
%back substitution
u(n)=f1(n)/b1(n);
for i=n-1:-1:1
u(i)=(f1(i)-c1(i)*u(i+1))/b1(i);
end
u
% result
u =
-0.2632 0.5789 0.3684 -0.2632
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.