Advertisement
alfars215

Richardson Extrapolation

Dec 20th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.90 KB | None | 0 0
  1. %% Richardson Extrapolation
  2. % The first derivative " *Richardson Extrapolation* " method is implemented as
  3. % shown here using MATLAB(R).
  4.  
  5. %% Clear Momory and Screen
  6. clear
  7. clc
  8. %% Enter The Function
  9. fs = input('Please Enter The Function \n f(x)= ','s');
  10. f  = inline(fs);
  11. disp('-------------------------------------');
  12.  
  13. %% Choose Output Mode
  14. % 1- Direvative table (Dn1, Dn2, Dn3, ....) for only one point.
  15. % 2- only the value of Derivative for many value of x.
  16. while 1
  17.     disp('choose output mode');
  18.     disp('1 => Direvative table (Dn1, Dn2, Dn3, ....) for only one point');
  19.     disp('2 => only the value of Derivative for many value of x');
  20.     Out_Mode = input('Enter mode (1 or 2) = ');
  21.     disp('-------------------------------------');
  22.     if Out_Mode == 1 || Out_Mode == 2
  23.         break;
  24.     else
  25.         disp('Error');
  26.     end;
  27. end;
  28.  
  29. %% Input Mode
  30. % Here, the user has to define where he wants to get the values of the 1st derivative.
  31. % The user can input that using two formats: points that he wants to get the 1st derivative
  32. % or the boundaries(a,b) of an period[a,b] besides a number of periods(n).
  33.    
  34. % 1- Direvative Table Mode.
  35. if Out_Mode == 1
  36.     Point_Mode = 'Points'; %Just one Point X(1).
  37. end;
  38. % 2- Value Mode.
  39. while Out_Mode == 2
  40.     disp('Please Choose points Input mode (Points or Boundary)');
  41.     disp('Points like [X0 X1 X2 ... ]');
  42.     disp('Boundary means enter [a,b] and n');
  43.     Point_Mode = input('','s');
  44.     disp('-------------------------------------');
  45.     if ~(strcmpi( Point_Mode , 'Boundary' )||strcmpi( Point_Mode , 'Points' ))
  46.         disp('Error!');
  47.     else
  48.         break;
  49.     end;
  50. end;
  51.  
  52. %% Enter Boundary or Points :: Values of X
  53. if strcmpi( Point_Mode , 'Boundary' )
  54.     a = input('Please Enter [a,b]\n  a = ');
  55.     b = input('  b = ');
  56.     n = input('Please Enter number of periods\n  n = ');
  57.     h = (abs(b-a))/n;
  58.     X = a:h:b;
  59.     h(1:n+1) = h(1);
  60. else
  61.     X = input('Please Enter the value of x = ');
  62. end;
  63. disp('-------------------------------------');
  64.  
  65. %% Choose the accuracy (h,m)
  66. disp('Choose the accuracy you need');
  67. disp('If the T.E = O(h^m) and h<1 then for greater m we get small error');
  68. while 1    
  69.     disp('Note: m must be even number');
  70.     m = input('Enter m = ');
  71.     disp('-------------------------------------');
  72.     if (mod(m,2) == 0)
  73.         break;
  74.     end;
  75. end;
  76. m = int32(m/2);
  77.  
  78. %% Enter The Value of Step Size (h) for points mode
  79. if strcmpi( Point_Mode , 'Points' )
  80.     if Out_Mode == 2
  81.         disp('Enter a value of (h) OR the corresponding (h) for every (x)');
  82.         disp('Note: if numbers of (h) is less than number of (x), the remaining');
  83.         disp('corresponding values of (h) will be last (h) entered');
  84.         disp('-------------------------------------');
  85.     end;
  86.     h = input('Please Enter the value of Step Size\n  h = ');
  87.     disp('-------------------------------------');
  88.     n = numel(X);
  89.     nh = numel(h);
  90.     % If data not complate for unequal we let the other h equal last value of h.
  91.     if nh<n
  92.         h(nh:n) = h(nh);
  93.     end;
  94. end;
  95.    
  96. %% Find First Drivative
  97.    
  98. D = zeros(m); % prepare size of the matrix D to avoid resizing.
  99. % First Derivative of X(j) ==> FD
  100. FD = X; % prepare size of the matrix FD to avoid resizing.
  101.  
  102. % Direvative Table Mode just use X(1).
  103. % This Condition to deal with more than one value of X.
  104. if Out_Mode == 1
  105.     n = 1;
  106. else
  107.     n = numel(X);
  108. end;
  109.  
  110. for j = 1:n
  111.     hj = h(j);
  112.     D(1,1) = (feval(f,X(j)+hj)-feval(f,X(j)-hj))/(2*hj);
  113.     for i = 1:1:m-1
  114.         hj = hj/2;
  115.         D(i+1,1) = (feval(f,X(j)+hj)-feval(f,X(j)-hj))/(2*hj);
  116.         for k = 1:1:i
  117.             D(i+1,k+1) = D(i+1,k)+(D(i+1,k)-D(i,k))/((4.0^double(k))-1);
  118.         end;        
  119.     end;    
  120.     FD(j) = D(m,m);
  121. end;
  122.    
  123. %% Output for Table Mode
  124. if Out_Mode == 1
  125.     fprintf('f(x) = %s\n',fs);
  126.     Line = '+------+------------+';
  127.     for i = 1:m  
  128.         Line = strcat(Line,'-----------------+');    
  129.     end;
  130.    
  131.     disp(Line); % Before Header.
  132.     fprintf('|   i  |      h     |');
  133.     for i = 1:m
  134.         fprintf('       Di,%1.0d      |',i);
  135.     end;
  136.     fprintf('\n');
  137.  
  138.     disp(Line); % Before Data.
  139.     for i = 1:m
  140.         fprintf('|  %2.0f  |  %8.4f  |',i,h);
  141.         for j = 1:m
  142.             if j>i
  143.                 fprintf('                 |');
  144.             else
  145.                 fprintf('   %12.4f  |', D(i,j));
  146.             end;
  147.         end;
  148.         fprintf('\n');
  149.         h =h/2;
  150.     end;
  151.  
  152.     disp(Line); % End.
  153. end;
  154.  
  155. %% Output for Value Mode
  156. if Out_Mode == 2
  157.     fprintf('f(x) = %s\n',fs);
  158.     disp   ('+---------------+------------+-------------------+');
  159.     fprintf('|      x        |      h     | first derivative  |\n');
  160.     disp   ('+---------------+------------+-------------------+');
  161.     for i = 1:n
  162.         fprintf('| %12.4f  |  %8.4f  |   %12.4f    |\n',X(i),h(i),FD(i));
  163.     end;
  164.     disp   ('+---------------+------------+-------------------+');
  165. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement