Task 1
Gauss Elimination program
List 3.4 (CODE) or download L3_4.m file from M-Files link%
L3_4. See Example 3.8. Copyright S. Nakamura, 1995
clear
a = [-0.04 0.04 0.12 3; 0.56 -1.56 0.32 1; -0.24 1.24 -0.28 0]
x = [0,0,0]'; % x is initialized as a column vector
% First pivoting (Rows 1 and 2 are exchanged)
tempo = a(2,:); a(2,:) = a(1,:); a(1,:)=tempo;a
% Elimination of elements below the first pivot.
a(2,:) = a(2,:) - a(1,:)*a(2,1)/a(1,1);
a(3,:) = a(3,:) - a(1,:)*a(3,1)/a(1,1);a
% Second pivoting (Rows 2 and 3 are exchanged)
tempo = a(3,:); a(3,:) = a(2,:); a(2,:)=tempo;a
% Eliminating the elements below the second pivot.
a(3,:) = a(3,:) - a(2,:)*a(3,2)/a(2,2);a
x(3) = a(3,4)/a(3,3);
x(2) = (a(2,4) - a(2,3)*x(3))/a(2,2);
x(1) = (a(1,4) - a(1,2:3)*x(2:3))/a(1,1);x
Task 3
Try use of Back Slash
A=[1,2,3.........];
b=[1;2;3.........];
x=A\b
function x = gelim( a, b, pv ) % % Solves ax = b using Gauss elimination % with optional (pv > 0) total pivoting, returning solution x. % [n n] = size(a); pvc = 1:n; x = b; u = a; for k = 1 : n-1 if pv > 0 % total pivoting pr = k; pc = k; for i = k : n for j = k : n if abs(u(i,j)) >= abs(u(pr,pc)), pr = i; pc = j; end end end % swap rows k and pr for u and x. t = u(k,k:n); u(k,k:n) = u(pr,k:n); u(pr,k:n) = t; t = x(k); x(k) = x(pr); x(pr) = t; % swap columns k and pc for u and pvc. t = u(:,k); u(:,k) = u(:,pc); u(:,pc) = t; t = pvc(k); pvc(k) = pvc(pc); pvc(pc) = t; end % % elimination % for i = k+1 : n r = u(i,k)/u(k,k); u(i,k+1:n) = u(i,k+1:n) - r*u(k,k+1:n); x(i) = x(i) - r*x(k); end end % % backsubstitution % for k = n : -1 : 1 x(k) = ( x(k) - u(k,k+1:n)*x(k+1:n) )/u(k,k); end % % reorder solution components % if pv > 0, xt = x; for k = 1 : n, x(pvc(k)) = xt(k); end, end % end gelim
Task 3
Try use of Back Slash
A=[1,2,3.........];
b=[1;2;3.........];
x=A\b