National Institute of Technology, Delhi: Numerical Methods (MAP 281) Assignment
National Institute of Technology, Delhi: Numerical Methods (MAP 281) Assignment
Technology,
Delhi
Submitted to:
Dr. Prashant Kumar
Submitted By:
Name: Vivek Kumar
Roll no.: 181210060
CSE 2nd Year
Question 3.04(Page 115). The following data represents the function f(x) = e2x. Using the
forward differences and the entire data, compute the approximation to f′(0.3). Also, find the
first order and second order approximations to f′(0.3). Compute the approximation to f′′(0.3)
using the entire data and the first order approximation.
CODES:
clc;
clear all;
close all;
n = input("Enter the number of elements: ");
x=[];
y=[];
for i = 1:n
x(i) = input("Enter the values for Xs: ");
x = [x x(i)];
y(i) = input("Enter the values for Ys: ");
y = [y y(i)];
end
temp=y;
i=n-1;
dif=0;
while(i>0)
j=1;
dif=dif+1;
for j = 1:i
T emp(n+1-i, j) = (temp(n-i, j+1)-temp(n-i, j));
end
i=i-1;
end
h = x(2)-x(1);
x0 = input("Enter value of x: ");
s = (x0-x(1))/h;
k = s+1;
f11 = (y(k+1)-y(k))/h;
f12 = (-3*y(k)+4*y(k+1)-y(k+2))/(2*h);
f2 = (y(k+2)-2*y(k+1)+y(k))/(h^2);
fprintf("First differentiation using First Order: %f\n" ,f11);
fprintf("First differentiation using Second Order: %f\n" ,f12);
fprintf("Second differentiation using First Order: %f\n" ,f2);
OUTPUT CONSOLE:
1 1
0 (1+ )
Question 3.12(Page 133). Evaluate I = with 4 and 8 subintervals
using the trapezium rule. Compare with the exact solution and find the
absolute errors in the solutions. Comment on the magnitudes of the errors
obtained. Find the bound on the errors.
#include <bits/stdc++.h>
using namespace std;
// A sample function whose definite integral's approximate value is computed using Trapezoidal
rule.
float y(float x)
{
// Declaring the function f(x) = 1/(1+x)
return 1 / (1 + x);
}
cout << "Value of integral is " << trapezoidal(x0, xn, n) << endl;
}
return 0;
}
OUTPUT CONSOLE:
Question 3.18(Page 143). Using Simpson’s 1/3 rule, evaluate the integral I =
1 1
0 ( 2 +6 +10)
, with 2 and 4 subintervals.
CODE:
#include <iostream>
#include <math.h>
using namespace std;
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++)
{
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver program
int main()
{
float lower_limit, upper_limit;
cout << "Enter lower limit" << endl;
cin >> lower_limit;
cout << "Enter upper limit" << endl;
cin >> upper_limit;
cout << "Enter number of sub intervals" << endl;
int n; // Number of interval
cin >> n;
cout << simpsons_(lower_limit, upper_limit, n);
return 0;
}
OUTPUT CONSOLE
1 1
0 (5+3 )
Question 3.20(Page 146). Using the Simpson’s 3/8 rule, evaluate I = with 3 and
6 subinterval.
#include <iostream>
using namespace std;
// Driver Code
int main()
{
float lower_limit, upper_limit;
cout << "Enter lower limit" << endl;
cin >> lower_limit;
cout << "Enter upper limit" << endl;
cin >> upper_limit;
cout << "Enter number of sub intervals" << endl;
int interval_limit; // Number of interval
cin >> interval_limit;
float integral_res = calculate(lower_limit, upper_limit,
interval_limit);