In Matlab 1 Implement Newtons method to find a root of a giv
In Matlab...
1. Implement Newton\'s method to find a root of a given function FOx),i.e. solve F(x)S0. The main program should read in x0 initial guess for the root, maxIT maximum number of iterations to be performed TOL tolerance for testing convergence and should call the Newton rootfinder (see below) Your program should output the input values, and then the iterates F xn) and upon convergence, the root. The values of FCX) and FOx) should be computed in a subprogram FCN(xn, Fn, DFn) called by the rootfinder. A good way to code Newton rootfinder and decide convergence is something like this (pseudocode): subprogram NewtonlD( x0 TOL, maxi x0 000.0 something big 1 print n labels for values for n-1 Max IT call FCN xn, Fn, DFn) returns Fn-F (xn), DFn-F (xn) 1 print n use formated printing in columns if ABS (dx TOL) then if ABS (Fn TOL then print DONE: root Fa\' Fn, in \',n, iters break out of the loop how depends on the language. else print STUCK dx TOL BUT residual Fn, TOL break out of the loop endif endif Fn/DFna [take Newton step end for print \"BAD: reached maxIT END subprogram.Solution
function Newton1D(x0, TOL, maxIT)
 % x0=100;
 % maxIT=10;
 % TOL=1*exp(-6);
 % Define Your Function Here
 syms x; % Define Variable
 Fun = x^2-3; % Define Function
 %%%%%%%%%%%%%%%%%%%%%%%%%%%
 xn=x0;
 dx=100000; % Initial dx value
 Fn=0; % Initial Fn Value
 DFn=0; % Initial DFn Value
 for n=1:maxIT
 [xn,Fn,DFn]=FCN(xn,Fun); % Calculate F(xn) and F\'(xn)
 fprintf(\'n=%d \\t xn=%f \\t Fn=%f \ \',n,xn,Fn);   
 if(abs(dx)<TOL) % Check the dx with TOL
 if(abs(Fn)<TOL)
 fprintf(\'n=%d \\t xn=%f \\t Fn=%f \ \',n,xn,Fn);
 break;
 else
 fprintf(\'n=%d \\t xn=%f \\t Fn=%f \ \',n,xn,Fn);
 end
 end
 dx=-Fn/DFn; % dx value Updation
 xn=xn+dx; % New xn Value
 end
 fprintf(\'n=%d \\t xn=%f \\t Fn=%f \ \',n,xn,Fn);
 fprintf(\'Root Value is : %f \ \',xn);
 end
function [xn,Fn,DFn]= FCN(xn,Fun)
 %% Define Variable Here
 syms x;
 %%%%%%%%%%%%%%%%%%%%%
 DFun= diff(Fun,x); % Differentiation to evaluate F\'
 Fn=double(subs(Fun,xn)); % Calculate F\'(xn)
 DFn=double (subs(DFun,xn)); % Calculate dF\'(xn)
 end
>> Newton1D(100,1*exp(-6),10)
 n=1    xn=100.000000    Fn=9997.000000
 n=2    xn=50.015000    Fn=2498.500225
 n=3    xn=25.037491    Fn=623.875956
 n=4    xn=12.578656    Fn=155.222578
 n=5    xn=6.408577    Fn=38.069865
 n=6    xn=3.438350    Fn=8.822251
 n=7    xn=2.155431    Fn=1.645882
 n=8    xn=1.773632    Fn=0.145770
 n=9    xn=1.732538    Fn=0.001689
 n=10    xn=1.732051    Fn=0.000000
 n=10    xn=1.732051    Fn=0.000000
 n=10    xn=1.732051    Fn=0.000000
 Root Value is : 1.732051>>


