fork download
  1. %use these values
  2. c1=.5
  3. c2=1/4
  4. c3=-1/2
  5. c4=-1
  6.  
  7. % system of equations
  8. F(1,1)=x(1)^2-x(2)+c1;
  9. F(2,1)=x(2)^2-x(1)+c1;
  10.  
  11. return
  12.  
  13.  
  14. function J= jacobi(x)
  15. %Jacobian is the derivative of each term of the matrix
  16. J(1,1)=2*x(1);
  17. J(1,2)=2;
  18. J(2,1)=-1;
  19. J(2,2)=2*x(2);
  20.  
  21. return
  22.  
  23. function[x,F,Counter]= mynewton(equation, jacobi, g,tol,iter,varargin)
  24. %function used to find zero
  25. %input: given system of equations
  26. %counter returns the number of of turns
  27. counter=0;
  28. error =tol +1;
  29. x=G;
  30.  
  31. while error >=tol & counter < iter
  32. J=faval(equation, x, varargin{:});
  33. delta=-j\f;
  34. x=x+delta;
  35. error=norm(delta);
  36. counter=counter+1 ;
  37. end
  38.  
  39. if (counter==iter &error>tol)
  40. fprintf(['fails to converge within max number of turns','.'the returned has relative residual % of e']'F);
  41.  
  42. else
  43.  
  44. fprint (['the method onverges at iteration','%i'],counter);
  45. end
  46.  
  47. return
  48.  
Success #stdin #stdout #stderr 0.23s 65056KB
stdin
Standard input is empty
stdout
c1 =  0.50000
c2 =  0.25000
c3 = -0.50000
c4 = -1
stderr
error: `x' undefined near line 5 column 8