matlab实现牛顿下山法(下山过程)

clear;clc
all float
syms x
f = (x^3)/3-x;
e=10;
i=1;
a(1)=-0.99;
fplot(f);
grid on
hold on
while abs(e)>1e-5
    
    lamda = 1/2;k=1;
    a(i+1)=a(i)-subs(f,a(i))/subs(diff(f),a(i));
    %下山过程
    while abs(subs(f,a(i+1)))>abs(subs(f,a(i)))
            a(i+1)=a(i)-lamda*subs(f,a(i))/subs(diff(f),a(i));
            k=k+1;
            lamda=(1/2)^k;
    end
    
    e=a(i+1)-a(i);
    i=i+1;
    if i>100
        break;
    end
    
   end
plot(a,subs(f,a),'r*')
a


对函数x^3/3-x=0 求解 x0=-0.99, 要求精度1e-5

你可能感兴趣的:(matlab,算法)