function [] = prog1main()
n=input('enter n  ')
if (n<=0)
    break
end

eps=1e-6;

for i=1:n
    nk(i)=1;
end
for i=1:n
    Y(i)=0;
end
%iteration bis kleiner als 10^-6
while (norm(nk)>=eps*norm(Y))
      X=Y;
      Y=X- invdfmalf(df(X),f(X));
      nk=-invdfmalf(df(X),f(X));
  end

Loesung=Y
%zurueck liefern zu kontrolieren
f(Y)

return
%berechnen funktion f
function Fy = f(y)
a=0;
b=1.0;
n=length(y);
h=1.0/(n+1);
c(1)=-a/(h*h);
 for i=2:(n-1)
    c(i)=0;
 end
c(n)=-b/(h*h);
 for i= 1:n
   for j = 1:n 
       if(i==j)
         A(i,j)=2/(h*h);
       elseif (1==abs(i-j))
               A(i,j)=-1/(h*h);
           else A(i,j)=0;
       end 
   end 
 end
Fy=A*(y')+exp(y)'+c';

%berechnen Df
function dFy = df(y)
n=length(y);
h=1/(n+1);
  for i= 1:n
    for j = 1:n 
       if(i==j)
         dFy(i,j)=2/(h*h)+exp(y(i));
       elseif (1==abs(i-j))
               dFy(i,j)=-1/(h*h);
           else dFy(i,j)=0;
       end 
    end 
  end

%berechnen Norm von Vektor
function ny = norm(y)
g=0;
  for i=1:length(y)
    g=g+y(i)^2;
  end
ny=sqrt(g);

%berechnen (Df)^-1*f
function x = invdfmalf(A,b)

n=length(b);
%Givens-Rotationen
for i=1:(n-1)
    r=sqrt(A(i,i)^2+A(i+1,i)^2);
    c=A(i,i)/r;
    s=A(i+1,i)/r;
    
    t=b(i);
    b(i)=c*t+s*b(i+1);
    b(i+1)=-s*t+c*b(i+1);
    
    for k=i:n
        p=A(i,k);
        q=A(i+1,k);
        A(i,k)=c*p+s*q;
        A(i+1,k)=-s*p+c*q;
    end
end   

%Rueckwaertsub.
x(n)=b(n)/A(n,n);
for i=(n-1):-1:1
      add=A(i,i+1:n)*x(i+1:n)';
      x(i)=(b(i)-add)/A(i,i);
end 


