clear;
close all;
% Define function
f = @(x) 3*x - cos(x) - 1;
% Initial interval [a,b]
a = 0;
b = 1;
% Check validity
if f(a)*f(b) >= 0
error('f(a) and f(b) must have opposite signs');
end
% Tolerances
tol_x = 1e-6; % interval tolerance
tol_f = 1e-6; % function tolerance
maxIter = 50;
fprintf('Iter\t a\t\t b\t\t c\t\t |b-a|\t\t |f(c)|\n');
for n = 1:maxIter
% Midpoint
c = (a + b)/2;
fprintf('%d\t %.6f\t %.6f\t %.6f\t %.6e\t %.6e\n', ...
n, a, b, c, abs(b-a), abs(f(c)));
% Stopping criteria
if abs(b - a) < tol_x || abs(f(c)) < tol_f
break;
end
% Interval update
if f(a)*f(c) < 0
b = c;
else
a = c;
end
end
fprintf('\nApproximate root = %.8f\n', c);
fprintf('Iterations = %d\n', n);
fixed point
clc;
clear;
close all;
% Define functions
f = @(x) 3*x - cos(x) - 1;
g = @(x) (cos(x) + 1)/3;
% Initial guess
x0 = 0.5;
% Tolerances
tol_x = 1e-12; % iteration tolerance
tol_f = 1e-12; % function tolerance
maxIter = 50;
fprintf('Iter\t x_n\t\t |x_n-x_{n-1}|\t |f(x_n)|\n');
for n = 1:maxIter
x1 = g(x0);
fprintf('%d\t %.8f\t %.6e\t %.6e\n', ...
n, x1, abs(x1-x0), abs(f(x1)));
% Stopping criteria
if abs(x1 - x0) < tol_x || abs(f(x1)) < tol_f
break;
end
x0 = x1;
end
fprintf('\nApproximate root = %.8f\n', x1);
fprintf('Iterations = %d\n', n);
newton raphson
clc;
clear;
close all;
% Define function and derivative
f = @(x) 3*x - cos(x) - 1;
df = @(x) 3 + sin(x);
% Initial guess
x0 = 0.5;
% Tolerances
tol_x = 1e-6; % iteration tolerance
tol_f = 1e-6; % function tolerance
maxIter = 50;
fprintf('Iter\t x_n\t\t |x_n-x_{n-1}|\t |f(x_n)|\n');
for n = 1:maxIter
x1 = x0 - f(x0)/df(x0);
fprintf('%d\t %.8f\t %.6e\t %.6e\n', ...
n, x1, abs(x1-x0), abs(f(x1)));
% Stopping criteria
if abs(x1 - x0) < tol_x || abs(f(x1)) < tol_f
break;
end
x0 = x1;
end
fprintf('\nApproximate root = %.8f\n', x1);
fprintf('Iterations = %d\n', n);
regular falsi
clc;
clear;
close all;
% Function
f = @(x) 3*x - cos(x) - 1;
% Initial interval [a,b] with f(a)*f(b) < 0
a = 0;
b = 1 ;
% Parameters
iter = 10;
tol = 1e-12;
fprintf('iter\t\t a\t\t b\t\t c\t\t f(c)\n');
for i = 1:iter
% False position formula
c = (a*f(b) - b*f(a)) / (f(b) - f(a));
fprintf('%d\t %0.8f\t %0.8f\t %0.8f\t %0.15e\n', ...
i, a, b, c, f(c));
% Convergence check
if abs(f(c)) < tol
break;
end
% Interval update (bracketing preserved)
if f(a)*f(c) < 0
b = c;
else
a = c;
end
end
fprintf('\nApproximate root = %.12f\n', c);
secant method
clc;
clear;
close all;
% Define function
f = @(x) 3*x - cos(x) - 1;
% Initial guesses (twostarting points)
x0 = 0.0;
x1 = 1.0;
% Tolerances
tol_x = 1e-6; % iteration tolerance
tol_f = 1e-6; % function tolerance
maxIter = 50;
fprintf('Iter\t x_n\t\t |x_n-x_{n-1}|\t |f(x_n)|\n');
for n = 1:maxIter
% Secant formula
x2 = x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0));
fprintf('%d\t %.8f\t %.6e\t %.6e\n', ...
n, x2, abs(x2 - x1), abs(f(x2)));
% Stopping criteria
if abs(x2 - x1) < tol_x || abs(f(x2)) < tol_f
break;
end
% Update points
x0 = x1;
x1 = x2;
end
fprintf('\nApproximate root = %.8f\n', x2);
fprintf('Iterations = %d\n', n);
引用
Nishitha (2026). bisection (https://jp.mathworks.com/matlabcentral/fileexchange/183458-bisection), MATLAB Central File Exchange. 取得日: .
MATLAB リリースの互換性
作成:
R2025b
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linuxタグ
| バージョン | 公開済み | リリース ノート | |
|---|---|---|---|
| 1.0.0 |
