Advertisement
Guest User

newton

a guest
May 22nd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.12 KB | None | 0 0
  1. %Newton
  2. clear all
  3. close all
  4. format long g
  5.  
  6. eps=1e-12;
  7. nmax=150;
  8.  
  9. VT=26e-3;
  10. R=1e3;
  11. Is=1e-15;
  12. E=5;
  13. w=2*pi*1e3;
  14. ns=2*4;
  15. time=(2*pi/w)/ns;
  16. vs=sqrt(2)*E*sin(w*time);
  17. xstart=sqrt(2)*E/2;
  18. %xstart=6;
  19. x(1)=xstart;%initial guess
  20. %f_cur=f(x(1));
  21. %df_cur=df_dx(x(1));
  22. f_cur=x(1)-R*Is*(exp((vs-x(1))/VT)-1); %initial fct
  23. df_cur=1+(R*Is/VT)*(exp((vs-x(1))/VT)); %initial fct derivative
  24.  
  25. ncount=1;
  26. while abs(f_cur)>eps & ncount<nmax
  27.     ncount=ncount+1;
  28.     x(ncount)=x(ncount-1)-inv(df_cur)*f_cur;
  29.     %f_cur=f(x(ncount));
  30.     %df_cur=df_dx(x(ncount);
  31.     f_cur=x(ncount)-R*Is*(exp((vs-x(ncount))/VT)-1);
  32.     df_cur=1+(R*Is/VT)*(exp((vs-x(ncount))/VT));
  33.     hst(ncount-1,1)=x(ncount);
  34.     hst(ncount-1,2)=f_cur;
  35. end
  36.    
  37. figure
  38. semilogy(hst(:,1),abs(hst(:,2)),'b*')
  39. xlabel("Root")
  40. ylabel("Residual")
  41. title("Newton Raphson method")
  42. grid on
  43.  
  44.  
  45. disp("Approximated root")
  46. disp(x(ncount))
  47. disp("Final residual on f")
  48. disp(abs(f_cur))
  49. %disp("Final error on x")
  50. %disp(abs(a(ncount)-b(ncount)))
  51. disp("Number of iterations")
  52. disp(ncount)
  53. disp("Time instant [s]")
  54. disp(time)
  55. disp("Voltage [V]")
  56. disp(x(ncount))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement