Project

Discretization basics for range of dx

Discretization basics for range of dx

Objective :-

Function programe :-

First order approximation :-

% function for first order error calculation
function output1 = first_order_approximate(x,dx)
 analytical_derivative = (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;
 output1 = abs(analytical_derivative -  num_first_order);
end

Second order approximation :-

% function for second order error calculation
function output2 = second_order_approximate(x,dx)
 analytical_derivative = (x^3*(cos(x))- sin(x)*3*x^2)/x^6;
 num_second_order = (sin(x+dx)/(x+dx)^3-sin(x-dx)/(x-dx)^3)/(2*dx);
 output2 = abs(analytical_derivative - num_second_order);
end

Fourth order approximation :-

% function for fourth order error calculation
function output3 = fourth_order_approximate(x,dx)
 analytical_derivative = (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);   
 output3 = abs(analytical_derivative - num_fourth_order);
end

Main programe :-

% Programe for discretization basics
clear all
close all
clc

% analytical function fx = sin(x)/x^3
% analytical derivative 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)
    %grouping error
    error1(i) = first_order_approximate(x,dx(i));
    error2(i) = second_order_approximate(x,dx(i));
    error3(i) = fourth_order_approximate(x,dx(i));
end

% plotting the error
figure(1);
loglog(dx,error1,'b','linewidth',3);
hold on
loglog(dx,error2,'r','linewidth',3);
hold on
loglog(dx,error3,'g','linewidth',3);
hold off
grid on
title('Comparison of errorin fist order,second order and fourth order approx')
legend('first order','second order','fourth order');
xlabel('dx');
ylabel('Errors');

%Finding the slope of the curve 
m1 = (error1(2)-error1(1))/(dx(2)-dx(1));
m2 = (error2(2)-error2(1))/(dx(2)-dx(1));
m4 = (error3(2)-error3(1))/(dx(2)-dx(1));
figure(2);
bar(1,m1);
hold on 
bar(2,m2);
hold on
bar(3,m4);
hold off
legend('first_order','second_order','fourth_order');
ylabel('Slope');

Explanation :-

  1. In main programe we have to call function for first, second , fourth order approximation
  2. After that we have to create the analytical function of fx = sin(x)/x^3.
  3. Then create analytical derivative f'(x) = (x^3*(cos(x))- sin(x)*3*x^2)/x^6.
  4. we have to compute the first derivative at x = pi/3 , ather that we have to create linspace command .
  5. Create for loop command to analysis the error .
  6. By using Bar command we have to create the bar value error.
  7. To plot command we using here is loglog().
  8. The loglog command is use to see the error in acccurate.

Error while facing in programe :-

No error

Output:-

Slope of the curve :-

  • The slope of curve as it clear from the above image that as order of aproximation increases the slope of the curve get bend more towards the direction of origin , which means error grt reduce .
  • Then inial value for error computed by fourth order approximation is quite high later for the lesser value the size it gives a less error .that's why we can expect accuarcy of the curve in fourth order approximation.
  • Slope for fourth order curve is te high because eor dropping from high that willgive exact value .
  • The error will drop rate is high for fourth orser approxiation. It gives you high accurate result witha a minimum amont of error as we see from the left side of the graph.

Bar Gaph:-

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.