0% found this document useful (0 votes)
3 views3 pages

Code

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)
3 views3 pages

Code

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

9/5/25 9:50 PM D:\Books\3.1\numerical lab Classw...\exp6.

m 1 of 3

CODE:
clc;
clear;
close all;

% Define the function and its derivative


f = @(x) x .* sin(x) - 1; % Function: f(x) = x*sin(x) - 1
f_prime = @(x) sin(x) + x .* cos(x); % Derivative: f'(x) = sin(x) + x*cos(x)

tol = 1e-4; % Convergence tolerance


max_iter = 100; % Maximum number of iterations allowed

%% Plotting the function to visualize root location


x_vals = linspace(0, 4, 500); % X-range for plotting
y_vals = f(x_vals);

figure;
plot(x_vals, y_vals, 'LineWidth', 1.5);
hold on;
yline(0, '--k'); % Add horizontal line at y = 0 (x-axis)
xlabel('x');
ylabel('f(x)');
title('Function Plot: f(x) = xsin(x) - 1');
grid on;

%% Bisection Method
fprintf('--- Bisection Method ---\n');

a = 0; % Left interval bound


b = 2; % Right interval bound

if f(a)*f(b) > 0
error('f(a) and f(b) must have opposite signs.');
end

iter = 1; % Iteration counter


errors_bisect = []; % Store errors for plotting
results_bisect = []; % Store table data

while iter <= max_iter


mid = (a+b)/2; % Midpoint of interval
f_mid = f(mid); % Function value at midpoint

if iter > 1
error_val = abs(mid - prev_mid); % Error
errors_bisect(end+1) = error_val;
if error_val < tol
break;
end
else
error_val = NaN; % No error in first iteration
end
9/5/25 9:50 PM D:\Books\3.1\numerical lab Classw...\exp6.m 2 of 3

% Store iteration data


results_bisect(iter,:) = [a, b, f(a), f(b), mid, error_val];

% Display current iteration


fprintf('Iteration %d: x = %.6f, f(x) = %.6f\n', iter, mid, f_mid);

if f(a)*f_mid < 0
b = mid;
else
a = mid;
end

prev_mid = mid;
iter = iter + 1;
end

fprintf('Root (Bisection): %.6f\n\n', mid);

% Display Bisection Table


T_bisect = array2table(results_bisect, ...
'VariableNames', {'a','b','f(a)','f(b)','x_c','Error'});
disp('Bisection Method Table:');
disp(T_bisect);

% Plot error vs iteration for Bisection


figure;
plot(1:length(errors_bisect), errors_bisect, '-o');
xlabel('Iteration');
ylabel('Error');
title('Number of Iterations vs. Error (Bisection Method)');
grid on;

%% Newton-Raphson Method
fprintf('--- Newton-Raphson Method ---\n');

x0 = 1.5; % Initial guess


iter = 1;
errors_newton = [];
results_newton = [];

while iter <= max_iter


fx = f(x0);
fpx = f_prime(x0);

if fpx == 0
error('Derivative became zero. Stopping iteration.');
end

x1 = x0 - fx / fpx;
error_val = abs(x1 - x0);
9/5/25 9:50 PM D:\Books\3.1\numerical lab Classw...\exp6.m 3 of 3

errors_newton(end+1) = error_val;

% Store iteration data


results_newton(iter,:) = [x0, x1, fx, f(x1), error_val];

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

if error_val < tol


break;
end

x0 = x1;
iter = iter + 1;
end

fprintf('Root (Newton-Raphson): %.6f\n\n', x1);

% Display Newton-Raphson Table


T_newton = array2table(results_newton, ...
'VariableNames', {'x_i','x_i+1','f(x_i)','f(x_i+1)','Error'});
disp('Newton-Raphson Method Table:');
disp(T_newton);

% Plot error vs iteration for Newton-Raphson


figure;
plot(1:length(errors_newton), errors_newton, '-o');
xlabel('Iteration');
ylabel('Error');
title('Number of Iterations vs. Error (Newton-Raphson Method)');
grid on;

You might also like