Project

Discretisation basics

<!-- [if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> </xml><![endif]--><!-- [if gte mso 9]><xml> Normal 0 false false false false EN-IN X-NONE X-NONE </xml><![endif]--><!-- [if gte mso 9]><xml> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0cm; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt; mso-ligatures:standardcontextual; mso-fareast-language:EN-US;} </style> <![endif]-->

<!-- [if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> </xml><![endif]--><!-- [if gte mso 9]><xml> Normal 0 false false false false EN-IN X-NONE X-NONE </xml><![endif]--><!-- [if gte mso 9]><xml> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0cm; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt; mso-ligatures:standardcontextual; mso-fareast-language:EN-US;} </style> <![endif]-->

Question

Write a program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.

1. The function that you will compute if f(x) = sin(x)/x^3

2. You will compute the first derivative at x = pi/3 for dx = pi/30.You will compute the derivative both numerically and analytically. Make sure you compute 1st, 2nd and 4th order approximations.

3. Define an error parameter as the absolute difference in numerical and exact derivative at x = pi/3

4. Plot a bar graph that shows the error

5. You need to have appropriate labels and legends on your plot

6. Make sure you upload your file

Formula for the fourth order approximation of the first derivative

Answer

Aim

To write a program to compare the first,second and fourth order derivatives of the first derivative of the function against the analytical derivative.

Program

`f(x)=sin(x)/x^3`

`f'(x)=(x^3*cos(x)-sin(x)*3x^2)/x^6`

`x=pi/3,dx=pi/30`

Matlab code

clear all
close all
clc

%analytical function f(x)= sin(x)/x^3
%first derivative f'(x)=((x^3*(cos(x)))-(sin(x)*3*x^2))/x^6

x=pi/3;
analytical_derivative=((x^3*(cos(x)))-(sin(x)*3*x^2))/x^6;

dx=pi/30;
%first order approximation
forward_differencing_1st=(sin(x+dx)/(x+dx)^3 - sin(x)/x^3)/dx;

%second order approximation
central_differencing_2nd=(sin(x+dx)/(x+dx)^3 - sin(x-dx)/(x-dx)^3)/(2*dx);

%fourth order approximation
central_differencing_4th=((sin(x-(2*dx))/(x-(2*dx))^3)-8*(sin(x-dx)/(x-dx)^3)+8*(sin(x+dx)/(x+dx)^3)-(sin(x+(2*dx))/(x+(2*dx))^3))/(12*dx);


%first order, second order and fourth order errors
error_first_order=abs(forward_differencing_1st - analytical_derivative);
error_second_order=abs(central_differencing_2nd - analytical_derivative);
error_fourth_order=abs(central_differencing_4th - analytical_derivative);

 
�r chart representing errors
figure(1)
x_plot=categorical({'1st order error', '2nd order error', '4th order error'});
y_plot=[error_first_order, error_second_order, error_fourth_order];
bar(x_plot,y_plot)
grid on
title('Comparison of 1st,2nd and 4th order derivative against analytical derivative')
xlabel('Order of the error')
ylabel('Error value')

Explanation

  • The function f(x) is given, its derivative f'(x) is calculated as

       `f'(x)=(x^3*cos(x)-sin(x)*3x^2)/x^6`

  • Then x and dx values are assigned
  • For calculating first order approximation, forward differencing scheme is used.
  • For calculating second order and fourth order approximation, central differencing schemes are used.
  • To calculate their errors, the difference between their respective order of the approximation and the analytical derivative is found, from which the absolute values of the errors are also found
  • To plot the errors in a bar chart, the names of the order of the error is kept along the x-axis using categorical command and error value is given along y-axis
  • Using bar(x_plot,y_plot), the desired bar graph is obtained.From which we can compare the error values of first, second and fourth order derivatives.

Bar graph

 

Values of the variables used in program

Conclusion

Thus, from the bar chart we can conclude that the error is least when we use higher order approximation(i.e fourth order approximation) for the first derivative of function and the error is greater for first order approximation.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.