other

Version 1.0.0 (39.6 KB) by Hansika
.
0 Downloads
Updated 16 Mar 2026

View License

Bisection method
clear
clc
f = @(x) cos(x) - x.*exp(x);
a = 0;
b = 1;
n = 1e4;
error = 10.^(-4);
c_values = zeros(n,1);
i = 0;
while (b-a)/2 > error
i = i + 1;
c = (a+b)/2;
if f(a)*f(c) < 0
b = c;
else
a = c;
end
i = i + 1;
c_values(i) = c;
end
c_values = c_values(1:i);
root = (a+b)/2;
fprintf('The approximate root is %d \n', root);
fprintf('Number of iterations %d\n', i)
Newton–Raphson Method
% Newton-Raphson method
clc
clear
g = @(x) cos(x) - x*exp(x); % function
dg = @(x) -sin(x) - exp(x) - x*exp(x); % derivative of the function
x0 = 1; % initial point
error_nr = 10^(-4);
while true
x1 = x0 - g(x0)/dg(x0);
if abs(x1 - x0) < error_nr
break;
end
x0 = x1;
end
fprintf('the value of x0 %d\n', x0);
Secant Method
clc
clear
f = @(x) cos(x) - x.*exp(x);
x_0 = 0;
x_1 = 1;
error_2 = 10^(-4);
while abs(x_1 - x_0) > error_2
x_2 = x_1 - f(x_1)*(x_1 - x_0)/(f(x_1) - f(x_0));
x_0 = x_1;
x_1 = x_2;
end
root_secant = x_1
Fixed Point Iteration
clear
clc
f = @(x) cos(x) - x.*exp(x);
g = @(x) cos(x)./exp(x);
x0 = 0.5;
error_4 = 10^(-4);
while true
x1 = g(x0);
if abs(x1 - x0) < error_4
break;
end
x0 = x1;
end
root_fixed_point = x1
Regula Falsi Method
h = @(x) cos(x) - x.*exp(x);
c = 0;
d = 1;
error_3 = 10^(-4);
while true
x = (c*h(d) - d*h(c)) / (h(d) - h(c));
if abs(h(x)) < error_3
break;
end
if h(c)*h(x) < 0
d = x;
else
c = x;
end
end
root_regula_falsi = x;
2nd question
f1 = @(x,y) y*cos(x*y) + 1;
f2 = @(x,y) sin(x*y) + x - y;
df1dx = @(x,y) -y^2*sin(x*y);
df1dy = @(x,y) cos(x*y) - x*y*sin(x*y);
df2dx = @(x,y) y*cos(x*y) + 1;
df2dy = @(x,y) x*cos(x*y) - 1;
x = 1;
y = 2;
error_5 = 10^(-4);
while true
F = [f1(x,y); f2(x,y)];
J = [df1dx(x,y), df1dy(x,y);
df2dx(x,y), df2dy(x,y)];
J_inv = inv(J);
delta = J_inv * F;
x_new = x - delta(1);
y_new = y - delta(2);
if max(abs([x_new - x, y_new - y])) < error_5
break;
end
x = x_new;
y = y_new;
end
x_solution = x_new;
y_solution = y_new;
s = f1(x_new, y_new);

Cite As

Hansika (2026). other (https://www.mathworks.com/matlabcentral/fileexchange/183412-other), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2025b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Version Published Release Notes
1.0.0