牛顿切线公式的理解就是把泰勒展式进行变形,之后不断重复控制精度,注意初始值的设置,牛顿切线对于初始值比较敏感,可能会导致不会收敛的情况。

clear;clc;close all;
syms x
[email protected](x)(2.*x.^2+5.*x-1);
x0=-2;
[email protected](x)(4*x + 5);
m=-10:0.1:10;
y=f(m);
plot(m,y,'r');
hold on;
x=x0;%初值
delta=1;
n=0;%迭代次数,下同
while abs(delta)>1e-6
delta=f(x)/df(x);
x=x-delta;
n=n+1;
end
x
n
 

机器学习---牛顿切线-牛顿二分机器学习---牛顿切线-牛顿二分


-----------------牛顿切线迭代
clear;clc;close all;
syms x
[email protected](x)(2.*x.^2+5.*x-1);
x0=-2;
[email protected](x)(4*x + 5);
x=x0;%初值
delta=1;
n=0;%迭代次数,下同
while abs(delta)>1e-6
delta=f(x)/df(x);
x=x-delta;
n=n+1;
end
x
n
fsolve(f,-2)
------------------二分迭代(有bug)
clear;clc;close all;
[email protected](x)(2.*x.^2+5.*x-1);
x=-10:0.1:10;
y=f(x);
plot(x,y,'r');
hold on;
fsolve(f,-2)
disp('锁定范围-3到-2')
a=-3;b=-2;
c=0;
for i=1:100  %迭代100代
    c=(a+b)*0.5;
    if(f(c)==0)
        c;
    end
    if(f(c).*f(a)<0)
        b=c;
    end
    if(f(b).*f(c)<0)
        a=c;
    end
end
c


 

相关文章:

  • 2021-05-16
  • 2021-10-26
  • 2021-08-20
猜你喜欢
  • 2021-07-14
  • 2022-12-23
  • 2021-07-04
  • 2021-04-16
  • 2022-12-23
  • 2021-09-08
相关资源
相似解决方案