% Bastian Groß
% Universität Trier
%
% 26. Juni 2014
%
% Newton Verfahren

clear all; clc;

fid=fopen ('Programmieraufgabe_5.txt','w');
fprintf(fid, 'Newton Verfahren zur Nullstellenbestimmung\n\n');

%Funktionen
f1 = inline('x.^2 - 1','x');
f2 = inline('x.^2 - 2*x + 1','x');
f3 = inline('exp(x-1) - 1','x');

%Ableitungen
f1_abl = inline('2*x','x');
f2_abl = inline('2*x - 2','x');
f3_abl = inline('exp(x-1)','x');

x_stern = 1;

%Grafik
t = [-2:0.1:2];
plot(t,f1(t),'rd-'); hold on;
plot(t,f2(t),'b*-');
plot(t,f3(t),'gs-'); plot(t,0*t,'k'); legend('f1','f2','f3');

%f1=x^2-1
fprintf(fid, '\n   f_1(x)=x^2-1 \n\n');
fprintf(fid,'  k     x_k            f(x_k)      Fehler    conv_lin    conv_quadr. \n');
fprintf(fid,'______________________________________________________________________ \n');


for x0=[2,-4]
    fprintf(fid, 'Startwert x_0= %2.0f \n',x0);
    x = x0;
    k = 0;
    fwert = f1(x);
    
    while fwert > 10^(-10)
        xneu = x - f1(x)/f1_abl(x);
        k = k+1;
        fwert = f1(x);
        fehler = abs(x-x_stern);
        conv_l = abs(xneu-x_stern)/fehler;
        conv_q = abs(xneu-x_stern)/fehler^2;
        x = xneu;
        fprintf(fid,' %2.0f  %1.12f  %1.4e  %1.4e  %1.4e  %1.4e \n',k,x,fwert,fehler,conv_l,conv_q);
    end
end

%f2=x^2-2x+1
fprintf(fid, '\n\n   f_2(x)=x^2-2x+1 \n\n');
fprintf(fid,'  k     x_k            f(x_k)      Fehler    conv_lin    conv_quadr. \n');
fprintf(fid,'______________________________________________________________________ \n');


for x0=[2,-4]
    fprintf(fid, 'Startwert x_0= %2.0f \n',x0);
    x = x0;
    k = 0;
    fwert = f2(x);
    
    while fwert > 10^(-10)
        xneu = x - f2(x)/f2_abl(x);
        k = k+1;
        fwert = f2(x);
        fehler = abs(x-x_stern);
        conv_l = abs(xneu-x_stern)/fehler;
        conv_q = abs(xneu-x_stern)/fehler^2;
        x = xneu;
        fprintf(fid,' %2.0f  %1.12f  %1.4e  %1.4e  %1.4e  %1.4e \n',k,x,fwert,fehler,conv_l,conv_q);
    end
end

%f3=e^(2-1)-1
fprintf(fid, '\n\n   f_3(x)=e^(2-1)-1 \n\n');
fprintf(fid,'  k     x_k            f(x_k)      Fehler    conv_lin    conv_quadr. \n');
fprintf(fid,'______________________________________________________________________ \n');


for x0=[2,-4]
    fprintf(fid, 'Startwert x_0= %2.0f \n',x0);
    x = x0;
    k = 0;
    fwert = abs(f3(x));
    
    while fwert > 10^(-10)
        xneu = x - f3(x)/f3_abl(x);
        k = k+1;
        fwert = abs(f3(x));
        fehler = abs(x-x_stern);
        conv_l = abs(xneu-x_stern)/fehler;
        conv_q = abs(xneu-x_stern)/fehler^2;
        x = xneu;
        fprintf(fid,' %2.0f  %1.12f  %1.4e  %1.4e  %1.4e  %1.4e \n',k,x,fwert,fehler,conv_l,conv_q);
    end
end


%Aufgabenteil ii)
fprintf(fid, '\n\n modifizierte Newton Verfahren zur Nullstellenbestimmung \n');
fprintf(fid, 'bei zweifachen Nullstellen\n\n');
%f2=x^2-2x+1
fprintf(fid, '\n\n   f_2(x)=x^2-2x+1 \n\n');
fprintf(fid,'  k     x_k            f(x_k)      Fehler    conv_lin    conv_quadr. \n');
fprintf(fid,'______________________________________________________________________ \n');


for x0=[2,-4]
    fprintf(fid, 'Startwert x_0= %2.0f \n',x0);
    x = x0;
    k = 0;
    fwert = f2(x);
    
    while fwert > 10^(-10)
        xneu = x - 2*f2(x)/f2_abl(x);
        k = k+1;
        fwert = f2(x);
        fehler = abs(x-x_stern);
        conv_l = abs(xneu-x_stern)/fehler;
        conv_q = abs(xneu-x_stern)/fehler^2;
        x = xneu;
        fprintf(fid,' %2.0f  %1.12f  %1.4e  %1.4e  %1.4e  %1.4e \n',k,x,fwert,fehler,conv_l,conv_q);
    end
end

fclose(fid);
