matlab如何用牛顿迭代发解方程?请举个例子.

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/16 01:56:35

matlab如何用牛顿迭代发解方程?请举个例子.
matlab如何用牛顿迭代发解方程?请举个例子.

matlab如何用牛顿迭代发解方程?请举个例子.
% f(x) = x*sin(1/x)-0.2*exp(-x) 求此方程f(x) = 0的解
x = 0.55; % 初始值
eps = 1; tol = 10^(-14); total = 100; k = 0;
format long;
while ((eps > tol) & (k < total)) % 判断
f = x*sin(1/x)-0.2*exp(-x); %
f1 = sin(1/x)-cos(1/x)/x+0.2*exp(-x); % f的导数
xx = x-f/f1; % 新的近似解
eps = abs(xx-x);
x = xx;
k = k+1;
fprintf('k = %2.0f, x = %12.10f\n',k,x);
end
运行结果
k = 1, x = 0.2769143433
k = 2, x = 0.3717802027
k = 3, x = 0.3636237820
k = 4, x = 0.3637156975
k = 5, x = 0.3637157087
k = 6, x = 0.3637157087