Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Backward Difference
- % The first derivative " *% Backward Difference Difference* " metrhod is implemented as
- % shown here using MATLAB(R).
- %% Clear Memory and Screen
- clear
- clc
- %% Firstly, the user has to choose between two input modes
- % 1- Function: Formula of a function.
- % 2- Data : some (x,y) points.
- % the user has to write one of two words (Function or Data) to indicate the mode.
- while 1
- disp('Please Choose your Input mode (Function or Data)');
- Mode = input('','s');
- disp('-------------------------------------');
- if ~(strcmpi( Mode , 'Function' )||strcmpi( Mode , 'Data' ))
- disp('Error!')
- else
- break;
- end;
- end;
- %% Function Mode
- % The user has chosen the Function mode (Go to line 10 ).
- if strcmpi( Mode , 'Function' )
- fs = input('Please Enter The Function \n f(x)= ','s');
- f = inline(fs);
- disp('-------------------------------------');
- % Here, the user has to define where he wants to get the values of the 1st derivative.
- % The user can input that using two formats: points that he wants to get the 1st derivative
- % or the boundaries(a,b) of an period[a,b] besides a number of periods(n).
- while 1
- disp('Please Choose points Input mode (Points or Boundary)');
- disp('Points like [X0 X1 X2 ... ]');
- disp('Boundary means enter [a,b] and n');
- Point_Mode = input('','s');
- disp('-------------------------------------');
- if ~(strcmpi( Point_Mode , 'Boundary' )||strcmpi( Point_Mode , 'Points' ))
- disp('Error!');
- else
- break;
- end;
- end;
- % Here, the code deals with the one of the two formats (Go back to Line 31).
- if strcmpi( Point_Mode , 'Boundary' )
- a = input('Please Enter [a,b]\n a = ');
- b = input(' b = ');
- n = input('Please Enter number of periods\n n = ');
- h = (abs(b-a))/n;
- X = a:h:b;
- h(1:n+1) = h(1);
- else
- X = input('Please Enter X \n For Example X = [0,1,5,6]\n X = ');
- % Enter The Value of Step Size h.
- disp('Enter a value of (h) OR the corresponding (h) for every (x)');
- disp('Note: if numbers of (h) is less than number of (x), the remaining');
- disp('corresponding values of (h) will be last (h) entered');
- disp('-------------------------------------');
- h = input('Please Enter the value of Step Size\n h = ');
- n = numel(X);
- nh = numel(h);
- % If data not complate we let the other h equal last value of h.
- if nh<n
- h(nh:n) = h(nh);
- end;
- end;
- disp('-------------------------------------');
- % Find First Derivative.
- n = numel(X);
- FD = X;
- for i = 1:n
- FD(i) = (feval(f,X(i))-feval(f,X(i)-h(i)))/(h(i));
- end;
- %Display X, h, First Derivative.
- fprintf('f(x) = %s\n',fs);
- disp ('+---------------+------------+-------------------+');
- fprintf('| x | h | first derivative |\n');
- disp ('+---------------+------------+-------------------+');
- for i = 1:n
- fprintf('| %12.4f | %8.4f | %12.4f |\n',X(i),h(i),FD(i));
- end;
- disp ('+---------------+------------+-------------------+');
- end;
- %% Data Mode
- % The user has chose the Data mode (Go back to line 11 ).
- if strcmpi( Mode , 'Data' )
- % The user has to enter the Boundary of interval.
- a = input('Please Enter the interval [a,b]\n a = ');
- b = input(' b = ');
- n = input('Please Enter number of periods\n n = ');
- if b<a
- tmp = a;
- a = b;
- b = tmp;
- end;
- h = (b-a)/n;
- X = a:h:b;
- % The user has to enter Y(x).
- Y = X;
- for i = 1:n+1
- fprintf('Please Enter Y(%1.4f) = ',X(i));
- Y(i) = input('');
- end;
- % Find First Derivative.
- FD = X;
- for i = 2:n+1
- FD(i) = (Y(i)-Y(i-1))/(h);
- end;
- %Display X, Y, First Derivative.
- disp ('+---------------+---------------+---------------------+');
- fprintf('| x | y | first derivative |\n');
- disp ('+---------------+---------------+---------------------+');
- fprintf('| %12.4f | %12.4f | Can`t be calculated |\n',X(1),Y(1));
- for i = 2:n+1
- fprintf('| %12.4f | %12.4f | %12.4f |\n',X(i),Y(i),FD(i));
- end;
- disp ('+---------------+---------------+---------------------+');
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement