Advertisement
alfars215

Backward

Dec 20th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.47 KB | None | 0 0
  1. %% Backward Difference
  2. % The first derivative " *% Backward Difference Difference* " metrhod is implemented as
  3. % shown here using MATLAB(R).
  4.  
  5. %% Clear Memory and Screen
  6. clear
  7. clc
  8.  
  9. %% Firstly, the user has to choose between two input modes
  10. % 1- Function: Formula of a function.
  11. % 2- Data    : some (x,y) points.
  12. % the user has to write one of two words (Function or Data) to indicate the mode.
  13. while 1
  14.     disp('Please Choose your Input mode (Function or Data)');
  15.     Mode = input('','s');
  16.     disp('-------------------------------------');
  17.     if ~(strcmpi( Mode , 'Function' )||strcmpi( Mode , 'Data' ))
  18.         disp('Error!')
  19.     else
  20.         break;
  21.     end;
  22. end;
  23.  
  24. %% Function Mode
  25. % The user has chosen the Function mode (Go to line 10 ).
  26. if strcmpi( Mode , 'Function' )
  27.     fs = input('Please Enter The Function \n f(x)= ','s');
  28.     f  = inline(fs);
  29.     disp('-------------------------------------');
  30.    
  31.     % Here, the user has to define where he wants to get the values of the 1st derivative.
  32.     % The user can input that using two formats: points that he wants to get the 1st derivative
  33.     % or the boundaries(a,b) of an period[a,b] besides a number of periods(n).
  34.     while 1
  35.         disp('Please Choose points Input mode (Points or Boundary)');
  36.         disp('Points like [X0 X1 X2 ... ]');
  37.         disp('Boundary means enter [a,b] and n');
  38.         Point_Mode = input('','s');
  39.         disp('-------------------------------------');
  40.         if ~(strcmpi( Point_Mode , 'Boundary' )||strcmpi( Point_Mode , 'Points' ))
  41.             disp('Error!');
  42.         else
  43.             break;
  44.         end;
  45.     end;
  46.     % Here, the code deals with the one of the two formats (Go back to Line 31).
  47.     if strcmpi( Point_Mode , 'Boundary' )
  48.         a = input('Please Enter [a,b]\n  a = ');
  49.         b = input('  b = ');
  50.         n = input('Please Enter number of periods\n  n = ');
  51.         h = (abs(b-a))/n;
  52.         X = a:h:b;
  53.         h(1:n+1) = h(1);
  54.     else
  55.         X = input('Please Enter X \n For Example X = [0,1,5,6]\n  X = ');
  56.         % Enter The Value of Step Size h.
  57.         disp('Enter a value of (h) OR the corresponding (h) for every (x)');
  58.         disp('Note: if numbers of (h) is less than number of (x), the remaining');
  59.         disp('corresponding values of (h) will be last (h) entered');
  60.         disp('-------------------------------------');
  61.         h = input('Please Enter the value of Step Size\n  h = ');
  62.         n = numel(X);
  63.         nh = numel(h);
  64.         % If data not complate we let the other h equal last value of h.
  65.         if nh<n
  66.             h(nh:n) = h(nh);
  67.         end;
  68.     end;
  69.     disp('-------------------------------------');
  70.    
  71.     % Find First Derivative.
  72.     n = numel(X);
  73.     FD = X;
  74.     for i = 1:n
  75.         FD(i) = (feval(f,X(i))-feval(f,X(i)-h(i)))/(h(i));
  76.     end;
  77.  
  78.     %Display X, h, First Derivative.
  79.     fprintf('f(x) = %s\n',fs);
  80.     disp   ('+---------------+------------+-------------------+');
  81.     fprintf('|      x        |      h     | first derivative  |\n');
  82.     disp   ('+---------------+------------+-------------------+');
  83.     for i = 1:n
  84.         fprintf('| %12.4f  |  %8.4f  |   %12.4f    |\n',X(i),h(i),FD(i));
  85.     end;
  86.     disp   ('+---------------+------------+-------------------+');
  87.  
  88. end;
  89.  
  90. %% Data Mode
  91. % The user has chose the Data mode (Go back to line 11 ).
  92. if strcmpi( Mode , 'Data' )
  93.      % The user has to enter the Boundary of interval.
  94.      a = input('Please Enter the interval [a,b]\n  a = ');
  95.      b = input('  b = ');
  96.      n = input('Please Enter number of periods\n  n = ');
  97.      if b<a
  98.          tmp = a;
  99.          a = b;
  100.          b = tmp;
  101.      end;
  102.      h = (b-a)/n;
  103.      X = a:h:b;  
  104.      
  105.      % The user has to enter Y(x).
  106.      Y = X;
  107.      for i = 1:n+1
  108.         fprintf('Please Enter Y(%1.4f) = ',X(i));
  109.         Y(i) = input('');
  110.      end;
  111.      
  112.      % Find First Derivative.
  113.      FD = X;
  114.      for i = 2:n+1
  115.          FD(i) = (Y(i)-Y(i-1))/(h);
  116.      end;
  117.      
  118.      %Display X, Y, First Derivative.
  119.     disp   ('+---------------+---------------+---------------------+');
  120.     fprintf('|      x        |      y        |   first derivative  |\n');
  121.     disp   ('+---------------+---------------+---------------------+');
  122.     fprintf('| %12.4f  | %12.4f  | Can`t be calculated |\n',X(1),Y(1));
  123.     for i = 2:n+1
  124.         fprintf('| %12.4f  | %12.4f  |     %12.4f    |\n',X(i),Y(i),FD(i));
  125.     end;
  126.     disp   ('+---------------+---------------+---------------------+');
  127.      
  128.      
  129. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement