function newton_n2; % newtons's method p=-.1; % n is the number if iterations n=20; fprintf(1, 'Examples of work of Newton"s Method \n'); for i=1:1:n p=p-f(p)/der_f(p); fprintf(1,'Iteration: %3i Newton`s: %12.10f \n', i,p); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Add graphics, just to have a %% picture of the function near %% the initial approximation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% figure(1); x=[-1:0.01:1]; plot(x,-x.^3-cos(x)); %plot(x,x.^2); %plot(x,sign(x).*sqrt(abs(x))); grid on %%% END OF GRAPHICS function y=f(x); % use example 2.3#2 solve as root finding problem by Newton's method y=-x^3-cos(x); % An exampl eof slower convergence: y=x^2 %y=x^2; % a funny example: the function has infinite derivative at $x=0$ %y=sign(x)*sqrt(abs(x)); function y=der_f(x); % use example 2.3#2 solve as root finding problem by Newton's method y=-3*x^2+sin(x); % solve y=x^2 %y=2*x; % a funny example: the function has infinite derivative at $x=0$ %y=1/(2*sqrt(abs(x)));