Advertisement
HimikoWerckmeister

OK is this insanity?

Mar 18th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.10 KB | None | 0 0
  1. %Code written by Echo Liu
  2.  
  3. format 'long';
  4.  
  5. k = 10;
  6. NMax = 50000;
  7. N = 10;
  8. dummy = 0;
  9. dummy1 = 0;
  10. dummy2 = 0;
  11. %QhatN = zeros((NMax-k)+1,1);
  12. %a = zeros(N,1);
  13. IiVec = zeros(NMax,1);
  14. Qn = zeros(NMax,1);
  15. a = zeros(NMax+1,1);
  16. QhatN = zeros(NMax-2,1);
  17. InteEval = 0;
  18. QhatDiff = zeros(NMax-3,1);
  19. QnDiff = zeros(NMax-1,1);
  20. %f = @(r) r^(-1)*(cos(r));
  21. f = @(r) r.^-1.*(sin(r.^-1.*log(r)));
  22. a(1) = 1;
  23. xaxisforQhatDiff = 1:1:NMax-3;
  24. xaxisforQnDiff = 1:1:NMax-1;
  25. xaxisforQn = 1:1:NMax;
  26. xaxisforQhatN = 1:1:NMax-2;
  27. for i=1:NMax
  28.   b = fzero(@(x) x*exp(x)-i*pi,0);
  29.   a(i+1) = exp(-b);
  30.    
  31.   dummy = a(i);
  32.   dummy1 = a(i+1);
  33.   dummy2 =a(i) - a(i+1);
  34.  
  35.  InteEval = integral(f,a(i),a(i+1));
  36.  IiVec(i) = InteEval;
  37.    %if (i == 1)
  38.     %   Qn(i) = IiVec(i);
  39.    %else
  40.     %   Qn(i) = Qn(i-1) + IiVec(i);
  41.    %end
  42.    
  43. end
  44.  
  45. %Summing up all the elements
  46. Qn(1) = IiVec(1);
  47. for s=2:NMax
  48.     Qn(s) = Qn(s-1) + IiVec(s);
  49. end
  50.  
  51. %Aitken Part
  52. for r=1:NMax-2
  53.     %QhatN(r) = QhatN(r)- (((IiVec(r+1))^(2))/(IiVec(r+2))-(IiVec(r+1)));
  54.     QhatN(r) = Qn(r) - (((Qn(r+1)-Qn(r))^(2))/(Qn(r+2)-2*Qn(r+1)+Qn(r)));
  55. end
  56.  
  57. for z=1:length(QhatN)-1
  58.     QhatDiff(z) = QhatN(z+1) - QhatN(z);
  59. end
  60.  
  61. for f=1:length(Qn)-1
  62.     QnDiff(f) = Qn(f+1)-Qn(f);
  63. end
  64. % we take the abs value because we
  65. % are interested in the abs error for each p(i)
  66.  
  67. figure;
  68. plot(log10(xaxisforQnDiff),log10(abs(QnDiff')))
  69. title(['Graph of differences up to N for regular version for N = ' num2str(NMax) '.'])
  70. xlabel(['log of N'])
  71. ylabel(['log of differences'])
  72.  
  73. % we take the abs value because we
  74. % are interested in the abs error for each p(i)
  75. figure;
  76. plot(log10(xaxisforQhatDiff),log(abs(QhatDiff')))
  77. title(['Graph of differences up to N for Aitkens method for N = ' num2str(NMax) ','])
  78. xlabel(['log of N'])
  79. ylabel(['log of differences'])
  80.  
  81. figure;
  82. plot(xaxisforQn,Qn');
  83. title(['Qn vs N']);
  84.  
  85. figure;
  86. plot(xaxisforQhatN,QhatN');
  87. title(['QhatN vs N']);
  88.  
  89. figure;
  90. plot(log10(xaxisforQnDiff),log10(abs(QnDiff')),'r',log10(xaxisforQhatDiff),log(abs(QhatDiff')),'b')
  91. %title{'Differences for both sequences over N with log base 10 both axes')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement