Assignment 2
Assignment 2
%Tolerance error
tolerance = 1e-6;
%Initialize variables
iteration = 0;
x = (a+b)/2;
x_values = x;
y_values = [f(x)];
Output:
Approximate root: 94.999999
Number of iterations: 27
Newton Raphson Method
%Define the function
f = @(x)x^2-25;
%Define the derivative
df = @(x)2*x;
%Initial guess values
x0 = 10;
x1 = 95;
%Tolerance
tolerance = 1e-6;
%Maximum number of iterations
maxIterations = 100;
%Initialize arrays to store iteration data
x_values = [x0,x1];
y_values = [f(x0),f(x1)];
%Perform Newton Raphson iterations
for iteration = 1:maxIterations
x0 = x0 - f(x0) / df(x0);
x1 = x1 - f(x1) / df(x1);
x_values =[x_values,x0,x1];
y_values =[y_values,f(x0),f(x1)];
Output:
Root 1: 5.000000
Root 2: 5.000000
Secant Method
%Define the function
f = @(x)x^2-15;
%Initial guess values
x0 = 10;
x1 = 95;
Output:
Approximate root: 3.872983
Number of iterations: 10
Approximate root: 3.872983
Final function value: 0.000000