0% found this document useful (0 votes)
28 views4 pages

NM Assignment Zain

numerical methods assignment

Uploaded by

mustafasid1912
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)
28 views4 pages

NM Assignment Zain

numerical methods assignment

Uploaded by

mustafasid1912
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/ 4

NM ASSIGNMENT

MATLAB

PREPARED FOR
Mr. Shoaib Ahmed
PREPARED BY
M. Zain ul Abideen
CMS: 408541
QUESTION 1;
CODE:
% Define the function
f = @(x, y, z) x^2 + y^2 + z^2 - 25;

% Initial guesses for the roots (ensure that f(xl) and f(xu) have opposite signs)
xl = 0; % Lower bound guess for x
xu = 5; % Upper bound guess for x

yl = 0; % Lower bound guess for y


yu = 5; % Upper bound guess for y

zl = 0; % Lower bound guess for z


zu = 5; % Upper bound guess for z

% Tolerance and maximum number of iterations


tol = 1e-6; % Tolerance for error
maxIter = 100; % Maximum number of iterations

% Error analysis initialization


error = inf; % Initialize error to a large value
iter = 0; % Initialize iteration counter

% Perform False Position Method


while error > tol && iter < maxIter
% Calculate function values at current bounds
f_xl = f(xl, yl, zl);
f_xu = f(xu, yu, zu);

% Calculate next approximation using False Position Method


xr = xu - (f_xu * (xl - xu)) / (f_xl - f_xu);
yr = yu - (f_xu * (yl - yu)) / (f_xl - f_xu);
zr = zu - (f_xu * (zl - zu)) / (f_xl - f_xu);

% Update error and iteration count


error = abs(f(xr, yr, zr));
iter = iter + 1;

% Update bounds for next iteration


if f(xr, yr, zr) * f_xl < 0
xu = xr;
yu = yr;
zu = zr;
else
xl = xr;
yl = yr;
zl = zr;
end
end

% Display the result


if iter < maxIter
disp(['Root found: (x, y, z) = (', num2str(xr), ', ', num2str(yr), ', ',
num2str(zr), ')']);
disp(['Iterations: ', num2str(iter)]);
else
disp('Maximum number of iterations reached without convergence.');
end

OUTPUT:

QUESTION 2;
CODE:
% Define the coefficients matrix A and the constants vector b
A = [4, -1, 1;
2, 5, 2;
1, 2, 4];

b = [8; 3; 11];

% Augmented matrix [A | b]
augmented_matrix = [A, b];

% Number of equations and unknowns


n = size(A, 1);

% Perform Gauss-Jordan elimination


for col = 1:n
% Divide the row by the diagonal element
diagonal_element = augmented_matrix(col, col);
augmented_matrix(col, :) = augmented_matrix(col, :) / diagonal_element;

% Perform row operations to make the rest of the column 0


for row = 1:n
if row ~= col
multiplier = augmented_matrix(row, col);
augmented_matrix(row, :) = augmented_matrix(row, :) - multiplier *
augmented_matrix(col, :);
end
end
end

% Extract the solution from the augmented matrix


solution = augmented_matrix(:, end);

% Display the solution


disp('Solution:');
disp(solution);

OUTPUT:

THE END

You might also like