All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Objective: Write a program that compares the first, second and fourth-order approximations of the first derivative against the analytical/exact derivative. Given Function : f(x)=sin(x)x3 First derivative of the function is f'(x)=x3cos(x)−sin(x)⋅3x2x6 The function is computed at x=π3 First-order…
Nikith M
updated on 05 Dec 2019
Objective: Write a program that compares the first, second and fourth-order approximations of the first derivative against the analytical/exact derivative.
Given Function : f(x)=sin(x)x3
First derivative of the function is f'(x)=x3cos(x)−sin(x)⋅3x2x6
The function is computed at x=π3
First-order approximation : sin(x+dx)(x+dx)3−sin(x)x3dx
Second-order approximation : sin(x+dx)(x+dx)3−sin(x−dx)(x−dx)32⋅dx
Fourth-order approximation : ((sin(x−2dx)(x−2dx)3−8⋅(sin(x−dx)(x−dx)3))+8⋅(sin(x+dx)(x+dx)3))−(sin(x+2dx)(x+2dx)3)12⋅dx
Main Code:
% Numerical derivative vs. exact derivative
clear all
close all
clc
% f(x) = sin(x)/x^3
% f'(x) = (((x^3)*cos(x)) - (sin(x)*3*(x^2)))/x^6
x = pi/3;
dx = linspace(pi/4,pi/4000,20);
for i = 1:length(dx)
error1(i) = first_order_aprox(x,dx(i))
error2(i) = second_order_aprox(x,dx(i))
error3(i) = fourth_order_aprox(x,dx(i))
end
% Plotting Bar Graph
figure(1)
errorgraph= [error1(15) error2(15) error3(15)]
bar(errorgraph)
set(gca,'XTicklabel',{'First order','Second order','Fourth order'})
ylabel('Error');
Function Code :
First-order approximation code :
% First order approximation
function error1 = first_order_aprox(x,dx)
analytical_soln = ((x^3)*cos(x) - sin(x)*3*(x^2))/(x^6)
num_first_order = (sin(x+dx)/(x+dx)^3 - sin(x)/x^3)/dx;
error1 = abs(num_first_order - analytical_soln);
end
Second-order approximation code :
% Second order approximation
function error2 = second_order_aprox(x,dx)
analytical_soln = ((x^3)*cos(x) - sin(x)*3*(x^2))/(x^6)
num_second_order = (sin(x+dx)/(x+dx)^3 - sin(x-dx)/sin(x-dx)^3) / 2*dx;
error2 = abs(num_second_order - analytical_soln);
end
Fourth-order approximation code :
% Fourth order approximation
function error3 = fourth_order_aprox(x,dx)
analytical_soln = ((x^3)*cos(x) - sin(x)*3*(x^2))/(x^6)
num_fourth_order = (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);
error3 = abs(num_fourth_order - analytical_soln);
end
Bar Graph
Figure 1
Conclusion :
From Figure 1 it is inferred that the fourth-order approximations are the lowest followed by first-order and second-order approximations respectively.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Steady Vs Unsteady (Mid Term)
Objective: To solve steady and unsteady 2D heat conduction equation using different iterative techniques for implicit and explicit methods in MATLAB. Introduction: Assumptions: - The domain is a square domain - Number of points along the 'x-direction' is equal to the number of points along the 'y-direction'…
28 Jan 2020 03:33 AM IST
1D Linear Convection (Grid points)
Objective: Write a MATLAB Code and to solve 1D linear equation Given Data: Length( L ) = 1m Convective Coeffieient( C ) = 1 Time step( dt ) = 0.01 Grid point( n ) = [20,40,80,160]…
08 Dec 2019 01:56 AM IST
Linear Convection Effect of Time steps on Solution
Objective: To write a MATLAB program that solves a 1D Linear equation for various time-steps. Given Data: Length( L ) = 1m Convective Coeffieient( C ) = 1 Time step( dt ) = (1e-1,1e-2,1e-3,1e-4)…
05 Dec 2019 11:56 PM IST
Discretization basics Numerical derivative vs. exact derivative
Objective: Write a program that compares the first, second and fourth-order approximations of the first derivative against the analytical/exact derivative. Given Function : f(x)=sin(x)x3 First derivative of the function is f'(x)=x3cos(x)−sin(x)⋅3x2x6 The function is computed at x=π3 First-order…
05 Dec 2019 11:56 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.