%% NEWTON'S METHOD via FOR LOOP
%(Selcuk Bayin, METU-NCC, bayin@metu.edu.tr)
%
clc
disp('EX 2:   Solve x^6 + 2x^5 - 3x^4 - 10x^3 - 6x^2 + 8x + 8')
f = inline('x.^6 + 2*x.^5 - 3*x.^4 - 10*x.^3 - 6*x.^2 + 8*x + 8', 'x')
df = inline('6*x.^5 + 10*x.^4 - 12*x.^3 - 30*x.^2 - 12*x + 8', 'x')
%%
%To find appropriate guesses see plotForNewton3
%ITERATION via 'for' LOOPS  
fprintf('-----\n First Guess x=-2.5')
guess = -2.5;                    
x = guess;                                  
for iteration = 0:3      
    x = x-(f(x)./df(x));
    error = abs(f(x));
     end
x
%%
clc
clear x
fprintf('-----\n Second Guess x=-1.5')
guess = -1.5;        
x = guess;
iteration_count=0;                                     
for iteration = 0:5      
    x = x-(f(x)./df(x));
    error = abs(f(x));
    iteration_count = iteration_count + (error > .000001); 
end
x
iteration_count
%%
clc
fprintf('-----\n Third Guess x=1.5')
guess = 1.5;                    
x = guess;
iteration_count=0;                                 
for iteration = 0:10      
    x = x-(f(x)./df(x));
    error = abs(f(x));
    iteration_count = iteration_count + (error > .000001); 
end
x
iteration_count
%%
clc
fprintf('-----\n Fourth Guess x=2.5')
format long
guess = 2.5;                    
x = guess;
iteration_count=0;                                 
for iteration = 0:5     
    x = x-(f(x)./df(x));
    error = abs(f(x));
    iteration_count = iteration_count + (error > .000001); 
end
x
iteration_count
error
 
disp('--------------------------')
 

