Venkata Siva
Followers: 0 Following: 0
gauss clc; clear all; A = [6 -2 2 4; 12 -8 6 10; 3 -13 9 3; -6 4 1 -18]; b = [12; 34; 27; -38]; n = length(b); Aug = [A b]; % Combine into Augmented Matrix % Step 1: Forward Elimination (Make zeros below the diagonal) for k = 1:n-1 for i = k+1:n m = Aug(i,k) / Aug(k,k); % The Multiplier Aug(i,:) = Aug(i,:) - m * Aug(k,:); % Row operation end end % Step 2: Back Substitution (Solve bottom to top) x = zeros(n,1); for i = n:-1:1 x(i) = (Aug(i,n+1) - Aug(i,i+1:n) * x(i+1:n)) / Aug(i,i); end disp('Solution:'); disp(x); ====== sec clc; clear all; f = @(x) cos(x) - x*exp(x); x0 = input('Enter x0: '); x1 = input('Enter x1: '); tol = 1e-3; while true % The Secant formula x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)); if abs(f(x2)) < tol break; end % Shift everything forward for the next round x0 = x1; x1 = x2; end fprintf('Root = %.4f\n', x2);