1 function [f,L]=Newton(f,a) 2 %this is newton teration whic is used for solving implicit One-dimensional Euler method 3 %users can used it directly for solve equation. 4 %the code was writen by HD.dong in january 8 2017. 5 %-------------------------------- 6 % syms x; 7 % % h=\'[x^4-4*x^2+4]\'; 8 % % h=\'[x^3+2*x^2+10*x-20]\'; 9 % h=\'[x^3-x-1]\'; 10 % % h=\'[x^3+4*x^2-10]\'; 11 % x0=0.6;%users can set any value except zero,because diff(h,x) is Singular when x is zero. 12 % [X L]=Newton(h,x0); 13 %-------------------------------------------------------------------- 14 lambda=1;%newton downhill factor 15 L(1)=lambda; 16 x0=a; 17 x1=x0-Jacoi(f,x0)\F(f,a)*lambda; 18 tol=1e-5; 19 ttol=1e-8; 20 i=1; 21 while norm(x1-x0,1)>=tol 22 lambda=1; 23 while abs(F(f,x1))>=abs(F(f,x0)) & lambda>=ttol 24 lambda=lambda/2; 25 x1=x0-Jacoi(f,x0)\F(f,x0)*lambda; 26 end 27 x0=x1; 28 x1=x0-Jacoi(f,x0)\F(f,x0)*lambda; 29 i=i+1; 30 L(i)=lambda; 31 end 32 f=x1; 33 function G=Jacoi(f,x0) 34 syms x; 35 G=vpa(subs(diff(f,x),\'x\',x0)); 36 function H=F(f,x0) 37 H=vpa(subs(f,\'x\',x0));
Matlab-5:牛顿迭代法工具箱
内容版权声明:除非注明,否则皆为本站原创文章。