1 | Page Richardson's Extrapolation Numerical Methods (COSC 607) Home Work II Richardson's Extrapolation First and second derivatives approximation Group No 1 Group Members:- 1. Biruk Ambachewu 2. Ayalnesh Tigabie 3. Admasu Chane 4. Hidya Nurhusen 5. Wasie Legesse 6. Abebe Alemu Submitted by:- Name Abebe Alemu Balcha ID No. GSR/3003/08 Submitted to:- Teacher Prof. Okey Oseloka Onyejekwe July , 2016
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1 | P a g e Richardson's Extrapolation
Numerical Methods (COSC 607)
Home Work II
Richardson's Extrapolation First and second derivatives approximation
2.1. Taylor's series ................................................................................................................................ 3
3. Scope of work........................................................................................................................................ 8
4. Problem Definition One ........................................................................................................................ 8
4.1. Richardson's' Extrapolation using centered formula for First Derivatives .................................... 9
4.1.1. Part I: - Working By hand ...................................................................................................... 9
4.1.2. Analysis on Problem one ..................................................................................................... 15
4.1.3. PART II: - Computer Program using MATLAB ...................................................................... 16
4.2. Richardson's' Extrapolation using centered formula for Second Derivatives ............................. 22
4.2.1. PART I:- Working by Hand .................................................................................................. 22
4.2.2. PART II: - Computer Program using MATLAB ...................................................................... 28
5. Problem definition Two ...................................................................................................................... 32
5.1. Richardson's Extrapolation first derivatives for even power Order of h .................................... 32
5.1.1. PART I :- working by hand ................................................................................................... 32
5.1.2. PART II: - Computer Program using MATLAB ...................................................................... 42
5.2. Richardson's Extrapolation Using three point Second Derivatives ............................................. 46
5.2.1. PART I: - working by hand ................................................................................................... 46
5.2.2. PART II: - Computer Program using MATLAB ...................................................................... 49
Step 5:- Calculate for the second, third and fourth order, i=1,..,n
F(i+1,j)=F(i,j)+(F(i,j)-F(i,j-1))/4j-1
Step 6: Get the result and plot
4.1.3.2. MATLAB Program Code
Richardson's Extrapolation using Centered formula for first derivative MATLAB Program
function RichardSonExtraP() % %================================================================== % % This Program is Richardson's' Extrapolation for approximating % % the first derivative % % Written By Abebe Alemu, % % ID GSR/3003/08 % % Addis Ababa University, Faculty of Natural Science % % Computational Science Department % % Graduate Program, -------------- Dated on July 2016 % % ===================================================================== format short; %==============Definition ================ h=zeros(50); h1=zeros(50); h2=zeros(50); h3=zeros(50); F1=zeros(50,50); F2=zeros(50,50); A1=zeros(10); A2=zeros(10); A3=zeros(10); %======Enter Input size from key board ============= fprintf('Please Enter the number of Step sizes n \n n='); n=input(''); A=zeros(n,n); % Assining A1=zeros(n); A2=zeros(n); A3=zeros(n); h=zeros(n);
17 | P a g e Richardson's Extrapolation
h1=zeros(n); h2=zeros(n); h3=zeros(n); x=2.3;% ------------Reference point %===========define Step size ============================================== for i=1:n h(i)=2^-(i-1); % Calculating the step size h end %============End ========================================================== %========================================================================== % Richardson's Extrapolation of first order using centered formula for f'(x) %========================================================================== for j=1:n h(j); F1(1,j)=(((x+h(j))^3)*(cos(x+h(j)))-((x-h(j)))^3*cos(x-h(j)))/(2*h(j)); A(j,1)= F1(1,j); A1(j)=A(j,1); h1(j)=h(j); end % % ---- Second order -------------------------------- for k=2:n F2(k,2)=F1(1,k)+((F1(1,k)-F1(1,k-1))/((4^1)-1)); A(k,2)= F2(k,2); A2(k)=A(k,2); h2(k)=h(k); end % % ------------Third, fourth up to n order ----------------- for m=3:n for k=2:m-1 F2(m,k+1)=F2(m,k)+((F2(m,k)-F2(m-1,k))/((4^k)-1)); A(m,k+1)=F2(m,k+1); A3(k)=A(m,k+1); h3(k)=h(k); end end %======================Dispaly====================================== % Display the result of first derivative value of f(x) at x=1.25 % numerically approximating using Richardson extrapolation disp(' f(x)=(x^3)*cos(x), its first derivative approximate value at x=1.25'); disp('==========================================================='); disp(' No h F_1s | F_2s | F_3s | F_4s |' ); disp('==========================================================='); for i=1:n msg1=sprintf('%0.0f %0.4f %0.6f %0.6f %0.6f %0.6f ',... i, h(i), A(i,1), A(i,2), A(i,3), A(i,4) ); disp([' ',msg1,' ']); end % %===================================================================
18 | P a g e Richardson's Extrapolation
% % The diagram for n step size and its respective value of f'(x) % %=================================================================== figure; % plot on the same figure windows in to four sub plots %--------------- First order plot----------------------------------- subplot(2,2,1) plot(h1,A1,'--rs'); legend('First Order'); axis([0.0 1.0 -20.0 -17.0]); ylabel('f''(x)'); xlabel('Step size h(n)'); title('First Order Richardson''s Extrapolation using Centered Formula'); %------------- Second order plot------------------------------------ subplot (2,2,2); plot(h2,A2,'*'); legend('Second order'); axis([0.0 1.0 -20.0 -17.0]); ylabel('f''(x)'); xlabel('Step size h(n)'); title('Second Order Richardson''s Extrapolation using Centered Formula'); %--------------Third order plot------------------------------------- subplot(2,2,3); plot(h3,A3,'d'); legend('Third Order'); axis([0.0 1.0 -20.0 -17.0]); ylabel('f''(x)'); xlabel('Step size h(n)'); title('Third Order Richardson''s Extrapolation using Centered Formula'); %----------------- on this quadrant I try to show all three plots------ subplot(2,2,4); plot(h1,A1,'--rs'); hold on; plot(h2,A2,'*'); hold on; plot(h3,A3,'d'); hold off; lgnd=legend('Richardson''s Extrapolation using Centered Formula'); axis([0.0 1.0 -20.0 -17.0]); ylabel('f''(x)'); xlabel('Step size h(n)'); title(lgnd,' Title'); % ----------------All in one new figure windows --------------- figure plot(h1,A1,'--rs'); hold on; plot(h2,A2,'*'); hold on; plot(h3,A3,'d');
19 | P a g e Richardson's Extrapolation
hold off; lgnd=legend('Richardson''s Extrapolation using Centered Formula'); axis([0.0 1.0 -20.0 -17.0]); ylabel('f''(x)'); xlabel('Step size h(n)'); title(lgnd,'Title') % % ------------------------END of PROGRAM -------------------- end
4.1.3.3. MATLAB Program Out put
Please Enter the number of Step sizes n
n=4
f(x)=(x^3)*cos(x), its first derivative approximate value at x=1.25
Step 5:- Calculate for the second, third and fourth order, i=1,..,n
F(i+1,j)=F(i,j)+(F(i,j)-F(i,j-1))/4j-1
Step 6: Get the result and plot
4.2.2.2. Computer MATLAB Program function SecRichardSonExtraP() % %================================================================== % % This Program is Extrapolation for approximating the first derivative % % Written By Abebe Alemu, % % ID GSR/3003/08 % % Addis Ababa University, Faculty of Natural Science % % Computational Science Department % % Graduate Program, -------------- Dated on July 2016 % % ===================================================================== format shorte; %==============Definition ================ h=zeros(50); h1=zeros(50); h2=zeros(50); h3=zeros(50); F1=zeros(50,50); F2=zeros(50,50); A1=zeros(10); A2=zeros(10); A3=zeros(10); n=10; %======Enter Input size from key board ============= fprintf('Please Enter the number of Step sizes n \n n='); n=input(''); A1=zeros(n); A2=zeros(n); A3=zeros(n); h=zeros(n); h1=zeros(n); h2=zeros(n); h3=zeros(n); x=2.3;% ------------Reference point
29 | P a g e Richardson's Extrapolation
%===========define Step size ============================================== for i=1:n h(i)=2^-(i-1); end A=zeros(n,n); %============End ========================================================== %====================================================================== % Richardson's Extrapolations for second order derivatives %====================================================================== for j=1:n h(j); F1(1,j)=(((x+h(j))^3)*(cos(x+h(j)))-2*x^3*cos(x)+((x-h(j)))^3*cos(x-h(j)))/(h(j)^2); A(j,1)= F1(1,j); A1(j)=A(j,1); h1(j)=h(j); end for k=2:n % ---- Second order F2(k,2)=F1(1,k)+((F1(1,k)-F1(1,k-1))/((4^1)-1)); A(k,2)= F2(k,2); A2(k)=A(k,2); h2(k)=h(k); end for m=3:n % Third to n th order for k=2:m-1 F2(m,k+1)=F2(m,k)+((F2(m,k)-F2(m-1,k))/((4^k)-1)); A(m,k+1)=F2(m,k+1); A3(k)=A(m,k+1); h3(k)=h(k); end end %=======================End ======================================== %======================Dispaly====================================== % Display the approximate first derivative value of f(x) at x=1.25 disp(' f(x)=x^3cos(x), its first derivative approximate value at x=1.25'); disp('=============================================================='); disp(' No h F_1s | F_2s | F_3s | F_4s |' ); disp('=============================================================='); for i=1:n msg1=sprintf('%0.0f %0.4f %0.8f %0.8f %0.8f %0.8f ',... i, h(i), A(i,1), A(i,2), A(i,3), A(i,4) ); disp([' ',msg1,' ']); end % %=================================================================== % % Plot % %=================================================================== figure; %--------------- First order plot------------------------------------- subplot(2,2,1) plot(h1,A1,'--rs'); title('Richardson''s Extrapolation First Order Derivatives'); xlabel('Step Size h'); legend('First Order'); ylabel('f''(x)');
30 | P a g e Richardson's Extrapolation
axis([0.0 1.0 -25.0 -17.0]); %------------- Second order plot--------------------------------------- subplot (2,2,2); plot(h2,A2,'*'); title('Richardson''s Extrapolation Second Order Derivatives'); xlabel('Step Size h'); legend('Second Order'); axis([0.0 1.0 -25.0 -17.0]); %--------------Third order plot-------------------------------------- subplot(2,2,3); plot(h3,A3,'d'); title('Richardson''s Extrapolation Third Order Derivatives'); xlabel('Step Size h'); legend('Third Order'); axis([0.0 1.0 -25.0 -17.0]); subplot(2,2,4); plot(h1,A1,'--rs',h2,A2,'*',h3,A3,'d'); lgnd=legend('Richardson''s Extrapolation '); ylabel('f''(x)'); xlabel('Step size h(n)'); title(lgnd,' Title'); axis([0.0 1.0 -25.0 -17.0]); % % ------------------------END of PROGRAM -------------------- end
4.2.2.3. Computer MATLAB Output
Please Enter the number of Step sizes n
n=6
f(x)=x^3cos(x), its first derivative approximate value at x=1.25
4.2.2.4. Richardson's extrapolation plot step size for second derivatives
32 | P a g e Richardson's Extrapolation
5. Problem definition Two Given f(x)=exp(x2)cos(3x), reference point x=1.25
Use Richardson's Extrapolation and the central difference results of the first derivatives and
second derivatives,
The exact value is given as which is equal to exact= -1.606374738, To check this is true, let's find analytically by finding the first derivatives of the function f( f(x)=exp(x2)cos(3x)
Step 5:- Calculate for the foruth, sixth and eighth order, as well i=1,..,n
F(i+1,j)=F(i,j)+(F(i,j)-F(i,j-1))/4j-1
Step 6: Get the result and plot
5.1.2.2. Computer MATLAB Program function OrderH() % %================================================================== % % This Program is Richardson's Extrapolation and central difference formula % % for the first derivative % % Written By Abebe Alemu, % % ID GSR/3003/08 % % Addis Ababa University, Faculty of Natural Science % % Computational Science Department % % Graduate Program, -------------- Dated on July 2016 % % ===================================================================== format shorte; %==============Definition ================ h=zeros(50); h1=zeros(50); h2=zeros(50); e=zeros(50); e2=zeros(50); e3=zeros(50); F1=zeros(50,50); F2=zeros(50,50); x=1.25; % ------------Reference point %======Enter Input size from key board ============= fprintf('Please Enter the number of Step sizes n \n n='); n=input('');
43 | P a g e Richardson's Extrapolation
% % ============================================ % % f(x)=exp(x^2)cos(3x) given function % % ================================================ % Analytical Exact value of the first derivative of the function at x=1.25 exact=-1.606374738; %===========define Step size ============= for i=1:n h(i)=0.4*(2^-(i-1)); end %A=zeros(n,n); Oh2=zeros(n,n); %============End ============================= %=========================================== % Richardson's Extrapolation, centered difference for first derivative %============================================== for j=1:n h(j); F1(1,j)=(((exp((x+h(j))^2))*(cos(3*(x+h(j)))))- ... ((exp((x-h(j))^2))*cos(3*(x-h(j)))))/(2*h(j)); Oh2(j,1)=exact-F1(1,j); e(j)=Oh2(j,1); end for k=2:n F2(k,2)=F1(1,k)+((F1(1,k)-F1(1,k-1))/((4^1)-1)); Oh2(k,2)= exact-F2(k,2); e2(k)=Oh2(k,2); end for m=3:n for k=3:m F2(m,k)=F2(m,k-1)+((F2(m,k-1)-F2(m-1,k-1))/((4^(k-1))-1)); Oh2(m,k)=exact-F2(m,k); e3(k)=Oh2(m,k); end end % %=============================End ============================= % %===========================Display============================ disp('=============================================================='); disp(' No h O(h^2) O(h^4) O(h^6) O(h^8) '); disp('=============================================================='); for i=1:n % 0.10f ten digit precision msg1=sprintf('%0.0f %0.4f %0.10f %0.10f %0.10f %0.10f ', ... i, h(i), Oh2(i,1), Oh2(i,2), Oh2(i,3), Oh2(i,4) ); disp([' ',msg1,' ']); end %========================================================================= for i=2:n h1(i)=h(i); end for j=3:n h2(j)=h(j); end % %=======================================================
44 | P a g e Richardson's Extrapolation
% % Plotting the graph using step size h and the error % %======================================================= figure; subplot(2,2,1)% ----- Plot the first order in the first quadrant plot(h,e,'--rs') title('Richardson''s Extrapolation Second Derivatives'); xlabel('Step Size h'); legend('Secodn order O(h^2)'); ylabel('f''(x)'); axis([0.0 0.5 -10.0 0.0]); grid on; subplot (2,2,2); % ----- Plot the second order in the second quadrant plot(h1,e2,'*'); title('Richardson''s Extrapolation Second Derivatives'); xlabel('Step Size h'); legend('Secodn order O(h^4)'); ylabel('f''(x)'); axis([0.0 0.3 0.0 0.25]); grid on; subplot(2,2,3); % -----Plot the third order on the third quadrant plot(h2,e3,'--rs'); title('Richardson''s Extrapolation Second Derivatives'); xlabel('Step Size h'); legend('Second order O(h^6)'); ylabel('f''(x)'); axis([0.0 0.510 -0.10 0.0]); grid on; subplot(2,2,4); % -----Plot the three figures in the same quadrant plot(h,e) hold on; plot(h1,e2,'*'); hold on; plot(h2,e3); title('Richardson''s Extrapolation Secodn Derivatives'); xlabel('Step Size h'); legend('Secodn order O(h^2)'); ylabel('f''(x)'); axis([0.0 0.5 -10.0 0.0]); grid on; end
Step 5:- Calculate for the second, third and fourth order, i=1,..,n
Step 6:- Get the result and plot
5.2.2.2. Computer MATLAB Program function Order2H() % %================================================================== % % This Program is Extrapolation for approximating the first derivative % % Written By Abebe Alemu, % % ID GSR/3003/08 % % Addis Ababa University, Faculty of Natural Science % % Computational Science Department % % Graduate Program, -------------- Dated on July 2016 % % ===================================================================== format short; %==============Defintion ================
50 | P a g e Richardson's Extrapolation
h=zeros(50); F1=zeros(50,50); F2=zeros(50,50); h1=zeros(50); h2=zeros(50); e=zeros(50); e2=zeros(50); e3=zeros(50); %======Enter Input size from key board ============= fprintf('Please Enter the number of Step sizes n \n n='); n=input(''); x=1.25;% ------------Refernce point exact=43.8375005482; %===========Define Step size ============= for i=1:n h(i)=0.4*(2^-(i-1)); h(i); A=zeros(n,n); Oh2=zeros(n,n); %============End ============================= %=========================================== % Calucation %f(x)=exp(x^2)cos(3x) %============================================== for j=1:n h(j); F1(1,j)=((exp((x+h(j))^2)*(cos(3*(x+h(j)))))-(2*(exp(x^2))*cos(3*x))+((exp((x-h(j))^2))*cos(3*(x-h(j)))))/(h(j)^2); Oh2(j,1)=exact-F1(1,j); e(j)=Oh2(j,1); % A(j,1)= F1(1,j); end for k=2:n F2(k,2)=F1(1,k)+((F1(1,k)-F1(1,k-1))/((4^1)-1)); Oh2(k,2)= exact-F2(k,2); e2(k)=Oh2(k,2); end for m=3:n for k=2:m-1 F2(m,k+1)=F2(m,k)+((F2(m,k)-F2(m-1,k))/((4^k)-1)); Oh2(m,k+1)=exact-F2(m,k+1); e3(k)=Oh2(m,k); end end % for i=2:n % h1(i)=h(i); % end % for j=3:n % h2(j)=h(j); % end % %=======================End ==================================== %===============Dispaly=========================== disp('=============================================================='); disp(' F_1s F_2s F_3s F_4s '); disp('=============================================================='); %Oh2(i,j)=Oh2(i,j);
51 | P a g e Richardson's Extrapolation
for i=1:n %for j=1:i %err=Oh2(i,j); msg1=sprintf('%0.0f %0.4f %0.10f %0.10f %0.10f %0.10f ', ... i, h(i), Oh2(i,1), Oh2(i,2), Oh2(i,3), Oh2(i,4) ); disp([' ',msg1,' ']); %end end figure; subplot(2,2,1)% ----- Plot the first order in the first quadrant plot(h,e,'--rs') title('Richardson''s Extrapolation Second Derivatives'); xlabel('Step Size h'); legend('Secodn order O(h^2)'); ylabel('f''(x)'); axis([0.0 0.5 -10.0 0.0]); grid on; subplot (2,2,2); % ----- Plot the second order in the second quadrant plot(h1,e2,'*'); title('Richardson''s Extrapolation Second Derivatives'); xlabel('Step Size h'); legend('Secodn order O(h^4)'); ylabel('f''(x)'); axis([0.0 0.3 0.0 0.25]); grid on; subplot(2,2,3); % -----Plot the third order on the third quadrant plot(h2,e3,'--rs'); title('Richardson''s Extrapolation Second Derivatives'); xlabel('Step Size h'); legend('Second order O(h^6)'); ylabel('f''(x)'); axis([0.0 0.510 -0.10 0.0]); grid on; subplot(2,2,4); % -----Plot the three figures in the same quadrant plot(h,e) hold on; plot(h1,e2,'*'); hold on; plot(h2,e3); title('Richardson''s Extrapolation Secodn Derivatives'); xlabel('Step Size h'); legend('Secodn order O(h^2)'); ylabel('f''(x)'); axis([0.0 0.5 -10.0 0.0]); grid on; %================================================ end
52 | P a g e Richardson's Extrapolation
5.2.2.3. Computer MATLAB Output for second derivatives