1.不动点迭代
function [x,num] = budongdian(a,tol,f_i)
i = 0;
fa = func_x_diedai(a);
fa = fa(f_i);
while(abs(a - fa) > tol)
a = fa;
fa = func_x_diedai(a);
fa = fa(f_i);
i = i + 1;
end
num = i;
x = a;
end
2.二分法迭代
function [x,num] = erfen(a,b,tol,f_i)
i = 0;
fa = func_x(a);
fb = func_x(b);
while(abs(a - b) > tol)
x = a + 0.5 * (b - a);
fx = func_x(x);
if((fa(f_i) * fx(f_i)) > 0)
a = x;
fa = fx;
elseif((fb(f_i) * fx(f_i)) > 0)
b = x;
fb = fx;
end
i = i + 1;
end
num = i;
end
3.牛顿法迭代
function [x,num] = newton(a,tol,f_i)
i = 0;
fa = func_x(a);
dfa = func_x_qiudao(a);
fa = fa(f_i);
dfa = dfa(f_i);
a2 = a - fa / dfa;
while(abs(a - a2) > tol)
a = a2;
fa = func_x(a);
dfa = func_x_qiudao(a);
fa = fa(f_i);
dfa = dfa(f_i);
a2 = a - fa / dfa;
i = i + 1;
end
x = a2;
num = i;
end
4.割线法迭代
function [x,num] = gexian(a,b,tol,f_i)
i = 0;
x0 = a;
fx0 = func_x(x0);
x1 = b;
fx1 = func_x(x1);
fx0 = fx0(f_i);
fx1 = fx1(f_i);
x2 = x1 - fx1 * (x1 - x0) / (fx1 - fx0);
while(abs(x2 - x1) > tol)
x0 = x1;
x1 = x2;
fx1 = func_x(x1);
fx1 = fx1(f_i);
fx0 = func_x(x0);
fx0 = fx0(f_i);
x2 = x1 - fx1 * (x1 - x0) / (fx1 - fx0);
i = i + 1;
end
num = i;
x = x2;
end
其中一些前提函数定义如下:
1.函数方程式定义
function fx = func_x(x)
fx1 = x ^ 5 - 3 * x - 10;
fx2 = sin(10 * x) + 2 * cos(x) - x - 3;
fx3 = x + atan(x) - 3;
fx4 = (x + 2) * log(x ^ 2 + x + 1) + 1;
fx = [fx1 fx2 fx3 fx4];
end
2.迭代格式定义
function fx = func_x_diedai(x)
fx1 = (3 * x + 10) ^ 0.2;
fx2 = sin(10 * x) + 2 * cos(x) - 3;
fx3 = -atan(x) + 3;
fx4 = -1 / log(x ^ 2 + x + 1) - 2;
fx = [fx1 fx2 fx3 fx4];
end
3.求导格式定义
function dfx = func_x_qiudao(y)
syms x;
fx1(x) = x ^ 5 - 3 * x - 10;
fx2(x) = sin(10 * x) + 2 * cos(x) - x - 3;
fx3(x) = x + atan(x) - 3;
fx4(x) = (x + 2) * log(x ^ 2 + x + 1) + 1;
dfx1(x) = diff((fx1(x)));
dfx2(x) = diff((fx2(x)));
dfx3(x) = diff((fx3(x)));
dfx4(x) = diff((fx4(x)));
dfx = [double(dfx1(y)) double(dfx2(y)) double(dfx3(y)) double(dfx4(y))];
end