不动点法
function a = fix_p(a,eps,f)
while abs(f(a) - a) >= eps
a = f(a);
end
牛顿法
function n = newton(a,eps,f,g)
n = a - f(a)/g(a);
while abs(n - a) >= eps
a = n;
n = a - f(a)/g(a);
end
end
二分法
function c = sec_div(a,b,eps,fun)
if fun((a+b)/2) == 0
c = (a+b)/2;
else
while abs(a-b) >= eps
c = (a + b)/2;
if fun(a)*fun(c) < 0
b = c;
end
if fun(b)* fun(c) < 0
a = c;
end
end
end
c = (a+b)/2;
end
运行实例
clc
fun1 = @(x) sin(x) - 6*x - 5;
fun2 = @(x) log(x) + x^2 - 3;
fun3 = @(x) exp(x) + x - 7;
fun4 = @(x) (sin(x) - 5)/6;
fun5 = @(x) s