0% found this document useful (0 votes)
8 views12 pages

Assignement C

The document outlines various numerical methods for finding roots of equations, including the Secant Method, Gauss-Seidel, Jacobi, Regula Falsi, Bisection Method, and Newton-Raphson. Each method is implemented in MATLAB with specified functions, initial guesses, tolerances, and iteration limits. The document provides iteration outputs and convergence checks for each method.

Uploaded by

mahrukhagha65
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)
8 views12 pages

Assignement C

The document outlines various numerical methods for finding roots of equations, including the Secant Method, Gauss-Seidel, Jacobi, Regula Falsi, Bisection Method, and Newton-Raphson. Each method is implemented in MATLAB with specified functions, initial guesses, tolerances, and iteration limits. The document provides iteration outputs and convergence checks for each method.

Uploaded by

mahrukhagha65
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/ 12

Self Secant:

clc;

clear;

% Define the function

f = @(x) x^3 + x^2 - 1;

% Parameters

tol = 1e-4; % Convergence tolerance

max_iteration = 10; % Maximum number of iterations

x0 = 0; % First initial guess

x1 = 1; % Second initial guess

fprintf('Secant Method Iterations:\n');

fprintf('--------------------------\n');

for i = 1:max_iteration

% Apply Secant Method formula

x2 = (-x1 * f(x0) + f(x1) * x0) / (f(x1) - f(x0));

% Display current iteration

fprintf('Iteration %2d: x = %.6f\n', i, x2);

% Check for convergence

if abs(x2 - x1) < tol


fprintf('Converged in %d iterations.\n', i);

break;

end

% Update for next iteration

x0 = x1;

x1 = x2;

end

% Display final result

fprintf('\nRoot found: %.6f\n', x2);

Gauss:

clc;

clear;

close all;

% Initial guesses

x0 = 0;

y0 = 0;

z0 = 0;

% Tolerance

tol = 1e-4;

maxIter = 100;

for i = 1:maxIter

x = (1/10)*(9 - 2*y0 - z0);

w(i)=x;
y = (1/10)*(7 - 2*x- 3*z0);

f(i)=y;

z = (1/10)*(6 - x - 2*y);

d(i)=z;

% Display current iteration

fprintf('Iteration %d: x=%.3f, y=%.3f, z=%.3f\n', i, x, y, z);

% Convergence check

if abs(x - x0) < tol && abs(y - y0) < tol && abs(z - z0) < tol

break;

end

y0=y;

z0=z;

x0=x;

end

fprintf('\nSolution:\nx = %.3f\ny = %.3f\nz = %.3f\n', x, y, z);

plot(w);hold on

plot(f);hold on

plot(d);hold on
Jacobi:

% Initial guesses

x0 = 0;

y0 = 0;

z0 = 0;

% Tolerance

tol = 1e-4;

maxIter = 100;

for i = 1:maxIter

x = (1/4)*(4 - y0 - z0);

y = (1/5)*(7 -3* x0 - z0);

z = (1/3)*(3 - x0 - y0);
% Display current iteration

fprintf('Iteration %d: x=%.3f, y=%.3f, z=%.3f\n', i, x, y, z);

% Convergence check

if abs(x - x0) < tol && abs(y - y0) < tol && abs(z - z0) < tol

break;

end

% Update guesses

x0 = x;

y0 = y;

z0 = z;

end

fprintf('\nSolution:\nx = %.3f\ny = %.3f\nz = %.3f\n', x, y, z);


Falsi:

clc;

clear;

% Define the function

f = @(x) exp(-x)*(3.2*sin(x) - 0.5*cos(x));

% Initial interval

a = 3;

b = 4;

% Tolerance for stopping

tol = 1e-6;
% Check that a root exists in [a, b]

if f(a)*f(b) >= 0

error('f(a) and f(b) must have opposite signs');

end

% Storage for approximations

x0 = zeros(1, 20);

fprintf('Regula Falsi Iterations:\n');

fprintf('--------------------------\n');

for i = 1:20

% Apply Regula Falsi (False Position) formula

x0(i) = (a*f(b) - b*f(a)) / (f(b) - f(a));

fprintf('Iteration %2d: x = %.6f, f(x) = %.6e\n', i, x0(i), f(x0(i)));

% Check if the approximation is good enough

if abs(f(x0(i))) < tol

fprintf('Converged after %d iterations.\n', i);

break;

end

% Update interval based on sign of function

if f(a)*f(x0(i)) < 0

b = x0(i);
else

a = x0(i);

end

end

% Final result

fprintf('\nApproximate root: x = %.6f\n', x0(i));

% Plotting convergence

figure;

plot(1:i, x0(1:i), '-o', 'LineWidth', 2);

xlabel('Iteration');

ylabel('Approximate Root');

title('Regula Falsi Method Convergence');

grid on;
Bisection Mehtod:

clc

clear all

close all

f = @(x) exp(-x)*(3.2*sin(x)-0.5*cos(x));

a = 3;

b = 4;

tol = 0.0001;

if (f(a)*f(b)) < 0

for i = 1:17

x0 = (a+b)/2 ;

if (f(a)*f(x0)) < 0

b = x0;

elseif f(b)*f(x0) < 0

a = x0;

end

fprintf('Root found: %.5f\n', x0);


if abs(f(x0)) < tol

break;

end

end

end

Newton Rapshon:

clc;

clear;

% Define the function and its derivative

f = @(x) exp(-x)*(3.2*sin(x) - 0.5*cos(x));

f_prime = @(x) exp(-x)*((0.5*cos(x)) - 3.2*sin(x)) - exp(-x)*(3.2*cos(x) + 0.5*sin(x));

% Initial guess

x0 = 3.5;

% Tolerance and maximum iterations

tol = 1e-6;

max_iter = 20;

fprintf('Newton-Raphson Iterations:\n');

fprintf('---------------------------\n');
for i = 1:max_iter

x1 = x0 - f(x0)/f_prime(x0); % Newton-Raphson formula

fprintf('Iteration %2d: x = %.6f, f(x) = %.6e\n', i, x1, f(x1));

if abs(x1 - x0) < tol

fprintf('Converged after %d iterations.\n', i);

break;

end

x0 = x1; % Update guess

end

fprintf('\nApproximate root: x = %.6f\n', x1);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy