% Class 11 - Root Finding Open Methods % Coded by NFR on 10.1.2019 % % Five different methods % Method 1: Fixed Point Iteration (FPI) f1 = @(x) sin(sqrt(x))-x; % To use FPI, add x to the give function h1 = @(x) f1(x) + x; % Call the FPI algorithm to solve root = FPI(h1,0.5,0.01) % Method 2: Newton Raphson Method (use analytical slope) % Remember this method can diverge % make your initial guess close to the root f2 = @(x) -2 + 6*x -4*x^2 + 0.5*x^3; d2 = @(x) 6 - 8*x + 1.5*x^2; % Use newton raphson algorithm root1 = newtraph(f2,d2,4.5) root2 = newtraph(f2,d2,4.43) % Let's investigate by plotting fplot(f2,[0 8]) % Method 3: Secant method % Requires two initial x guesses root1 = secantmethod(f2,5,5.2) % Method 4: Modified secant method root1 = modsecantmethod(f2,5,.05) % Method 5: Brent's Method --> Matlab's function % fzero % fzero is a blend of open and bracket methods % makes it efficient and (fairly) robust root1 = fzero(f2,6) % Two other functions for roots that might % come in handy (only for polynomial functions) root_ans = roots([0.5 -4 6 -2]) % Remember to insert coefficients in descending order (matlab syntax) poly_coef = poly(root_ans) % Extra Problem 6.14 f3 = @(x) x/(1-x)*sqrt(2*3/(2+x))-0.05 root_x = fzero(f3,0.01)