Advertisement
alfars215

n

Dec 20th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.77 KB | None | 0 0
  1. %% Central Difference
  2. % The Higher derivative Up to n " *Central Difference* " metrhod is implemented as
  3. % shown here using MATLAB(R).
  4.  
  5. %% Clear Memory and Screen
  6. clear
  7. clc
  8.  
  9. %% Here, The user enter the order of derivative that he needs.
  10. disp('Please Enter The order of Derivative');
  11. Order = input('  Order = ');
  12. disp('-------------------------------------');
  13.  
  14. %% Enter The Function.
  15. fs = input('Please Enter The Function \n f(x)= ','s');
  16. f  = inline(fs);
  17.  
  18. %% Input Mode
  19. % Here, the user has to define where he wants to get the values of the 1st derivative.
  20. % The user can input that using two formats: points that he wants to get the 1st derivative
  21. % or the boundaries(a,b) of an period[a,b] besides a number of periods(n).
  22. disp('-------------------------------------');
  23. while 1
  24.     disp('Please Choose points Input mode (Points or Boundary)');
  25.     disp('Points like [X0 X1 X2 ... ]');
  26.     disp('Boundary means enter [a,b] and n');
  27.     Point_Mode = input('','s');
  28.     disp('-------------------------------------');
  29.     if ~(strcmpi( Point_Mode , 'Boundary' )||strcmpi( Point_Mode , 'Points' ))
  30.         disp('Error!');
  31.     else
  32.         break;
  33.     end;
  34. end;
  35. % Here, the code deals with the one of the two formats (Go back to Line 19).
  36. if strcmpi( Point_Mode , 'Boundary' )
  37.     a = input('Please Enter [a,b]\n  a = ');
  38.     b = input('  b = ');
  39.     n = input('Please Enter number of periods\n  n = ');
  40.     h = (abs(b-a))/n;
  41.     X = a:h:b;
  42.     h(1:n+1) = h(1);
  43. else
  44.     X = input('Please Enter X \n For Example X = [0,1,5,6]\n  X = ');
  45.     %Enter The Value of Step Size h.
  46.     disp('Enter a value of (h) OR the corresponding (h) for every (x)');
  47.     disp('Note: if numbers of (h) is less than number of (x), the remaining');
  48.     disp('corresponding values of (h) will be last (h) entered');
  49.     disp('-------------------------------------');
  50.     h = input('Please Enter the value of Step Size\n  h = ');
  51.     n = numel(X);
  52.     nh = numel(h);
  53.     % If data not complate for unequal we let the other h equal last value of h.
  54.     if nh<n
  55.         h(nh:n) = h(nh);
  56.     end;
  57. end;
  58. disp('-------------------------------------');
  59.    
  60. % Find the Needed Derivative.
  61. n = numel(X);
  62. HD = X;
  63. for j = 1:n
  64.     HD(j) = 0;
  65.     for i = 0:Order
  66.         HD(j) = HD(j) + ((-1)^i) * nchoosek(Order,i) * feval(f,X(j)+(((Order/2)-i)*h(j)));
  67.     end;
  68.     HD(j) = HD(j)/(h(j)^Order);
  69. end;
  70.  
  71. %Display X, h,  HD
  72. fprintf('f(x) = %s\n',fs);
  73. disp   ('+---------------+------------+-------------------+');
  74. fprintf('|      x        |      h     |    %1.0f derivative   |\n',Order);
  75. disp   ('+---------------+------------+-------------------+');
  76. for i = 1:n
  77.     fprintf('| %12.4f  |  %8.4f  |   %12.4f    |\n',X(i),h(i),HD(i));
  78. end;
  79. disp   ('+---------------+------------+-------------------+');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement