% Metoda Newton pentru sisteme de ecuatii neliniare
rehash
clc
clearvars
format longG

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x_tol=1e-6; % toleranta de calcul


x = [-2; 1]; % aproximatiile initiale din grafic

f = @(x)[ x(1)^2+x(2)^2-5
          x(2)-exp(x(1))-1]; % vectorul ce contine functiile (F(x)=0)
          
          
          
% ## Jacobianul

df = @(x) [2*x(1)       2*x(2)  % Jacobianul sistemului de ecuatii neliniare
          -exp(x(1))        1];% se delimiteaza prin "spatiu" elementele 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Main program          
          
i=0; 
eps_calc=1e3;   
dim=size(x,1); 
while eps_calc>=x_tol
  x1=x;
  x = x  -df(x)\f(x);; % formula de iterare 
  eps_calc=norm(x1-x);
  i=i+1;
  disp('  ');
  disp('  ');
  fprintf ( 'Iteratia nr %4.0f  \n', i);
  for j1=1:dim
    fprintf ( 'Aproximatia x(%2.0f) = %2.20f  \n', j1,x(j1));
  end
end 
  disp('  ');
  %disp('  ');
  %disp('Final program')
  disp ('   ');
  fprintf ( 'Solutia calculata in precizia ceruta s-a obtinut dupa %2.0f iteratii:  \n', i);
 % disp(' ' );
   
for i2=1:dim 
fprintf ( 'Solutia x(%2.0f)= %2.20f  \n',i2,x(i2))
%fprintf ( 'Solutia Y= %2.20f  \n', x(2));
end
disp ('   ');
fprintf ( 'Verificarea sistemului de ecuatii F(x)=0 ?  \n');
fprintf ( ' %2.20f  \n', f(x));
disp ('   ');
fprintf ( 'EPS maxim calculat la ultima iteratie=  %2.35f  \n', max(eps_calc));
