0% found this document useful (0 votes)
149 views3 pages

kiran Thapa //numerical Methods //assignment: 3

The document contains code that calculates the derivative of a function f(x) = exp(3x)sin(2x) at the point x = PI/4 using various numerical differentiation formulas including: three-point midpoint, three-point endpoint, five-point midpoint, five-point endpoint, and two-point formulas. It outputs the exact derivative value and the values calculated from each formula along with the absolute and relative errors.

Uploaded by

upadhayaymadan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views3 pages

kiran Thapa //numerical Methods //assignment: 3

The document contains code that calculates the derivative of a function f(x) = exp(3x)sin(2x) at the point x = PI/4 using various numerical differentiation formulas including: three-point midpoint, three-point endpoint, five-point midpoint, five-point endpoint, and two-point formulas. It outputs the exact derivative value and the values calculated from each formula along with the absolute and relative errors.

Uploaded by

upadhayaymadan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Assignment3

//Kiran Thapa
//Numerical Methods
//Assignment: 3

{
public static void Main(String[] args)
{
double Xn = Math.PI / 4;
double h = 0.01;
double p = 3 * Math.Exp(3 * (Math.PI / 4));
//Given value of p in the question,
double Xnh = Math.PI / 4 + h;
double Xn2h = Math.PI / 4 + 2 * h;
double Xn3h = Math.PI / 4 + 3 * h;
double Xn4h = Math.PI / 4 + 4 * h;
double Xnmh = Math.PI / 4 - h;
double Xnm2h = Math.PI / 4 - 2 * h;

//f(Xo) at various points used in the formulas later on.

double f_Xn = Math.Exp(3 * Xn) * Math.Sin(2 * Xn);


double f_Xnh = Math.Exp(3 * Xnh) * Math.Sin(2 * Xnh);
double f_Xn2h = Math.Exp(3 * Xn2h) * Math.Sin(2 * Xn2h);
double f_Xn3h = Math.Exp(3 * Xn3h) * Math.Sin(2 * Xn3h);
double f_Xn4h = Math.Exp(3 * Xn4h) * Math.Sin(2 * Xn4h);
double f_Xnmh = Math.Exp(3 * Xnmh) * Math.Sin(2 * Xnmh);
double f_Xnm2h = Math.Exp(3 * Xnm2h) * Math.Sin(2 * Xnm2h);
Console.WriteLine("Exact value = {0}",p);

//Three point midpoint formula.

double three_point_midpoint = (f_Xnh - f_Xnmh) / (2 * h);


Console.WriteLine("Three point midpoint = {0]", three_point_midpoint);
double three_p_abso = p - three_point_midpoint;
Console.WriteLine("Three point midpoint Absolute Error = {0}",
Math.Abs(three_p_abso));
Console.WriteLine("Three point midpoint Relative Error = {0}",
(Math.Abs(three_p_abso) / p) * 100 + "%");

//Three point end-point formula.

double f_prime__Xn = ((-3 * f_Xn + 4 * f_Xnh - f_Xn2h) / (2 * h));


Console.WriteLine("Exact value = " + p);
Console.WriteLine("Three point endpoint value = " + f_prime__Xn);
double abso = p - f_prime__Xn;
Console.WriteLine("Three point endpoint Absolute Error = {0}", Math.Abs(abso));
Console.WriteLine("Three point endpoint Relative Error = {0}", (Math.Abs(abso) /
p) * 100 + "%");

//Five-point midpoint formula.


double five_point_midpoint = (f_Xnm2h - 8 * f_Xnmh + 8 * f_Xnh - f_Xn2h) / (12 *
h);
Console.WriteLine("Five point midpoint value = {0}" ,five_point_midpoint);
double five_abso = p - five_point_midpoint;
Console.WriteLine("Five point midpoint Absolute Error = {0}",
Math.Abs(five_abso));
Console.WriteLine("Five point midpoint Relative Error = {0}",
(Math.Abs(five_abso) / p) * 100 + "%");

//Five point end-point.

double five_point_endpoint = (-25 * f_Xn + 48 * f_Xnh - 36 * f_Xn2h + 16 * f_Xn3h


- 3 * f_Xn4h) / (12 * h);
Console.WriteLine("Five point end-point value = {0}", five_point_endpoint);
double fpe_abso = p - five_point_endpoint;
Console.WriteLine("Five point end-point Absolute Error = {0}",
Math.Abs(fpe_abso));
Console.WriteLine("Five point end-point Relative Error = {0}",
(Math.Abs(fpe_abso) / p) * 100 + "%");

//Two point formula.


Console.WriteLine("Exact value = {0}", p); double two_point = (f_Xnh - f_Xn) / h;
Console.WriteLine("Two point value = {0}", two_point);
double tp_abso = p - two_point;
Console.WriteLine("Two point Absolute Error = {0}" , Math.Abs(tp_abso));
Console.WriteLine("Two point Relative Error = {0}",(Math.Abs(tp_abso) / p) * 100
+ "%");
}
}
OUTPUT:

Exact value = 31.652172222593286


Three point midpoint = 31.650589089074543
Three point midpoint Absolute Error = 0.0015831335187428408
Three point midpoint Relative Error = 0.0050016583620532745%
Exact value = 31.652172222593286
Three point endpoint value = 31.65565876247527
Three point endpoint Absolute Error = 0.0034865398819832194
Three point endpoint Relative Error = 0.01101516779785032%
Exact value = 31.652172222593286
Five point midpoint value = 31.65217432237396
Five point midpoint Absolute Error = 2.099780672892848E-6
Five point midpoint Relative Error = 6.6339228098665115E-6%
Exact value = 31.652172222593286
Five point end-point value = 31.65218555037678
Five point end-point Absolute Error = 1.3327783495498124E-5
Five point end-point Relative Error = 4.210701054502909E-5%
Exact value = 31.652172222593286
Two point value = 31.91430487394058
Two point Absolute Error = 0.2621326513472937

You might also like