0% found this document useful (0 votes)
98 views9 pages

National Institute of Technology, Delhi: Numerical Methods (MAP 281) Assignment

The document contains code to solve numerical integration problems using numerical methods like the trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. The code takes the limits of integration and number of subintervals as input, calculates the value of the integral using the specified method, and outputs the result. The problems involve evaluating definite integrals of functions like 1/(1+x), 1/(x^2 + 6x + 10), and 1/(5+3x) over given intervals using different numerical methods and numbers of subintervals.

Uploaded by

Vivek Kumar
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)
98 views9 pages

National Institute of Technology, Delhi: Numerical Methods (MAP 281) Assignment

The document contains code to solve numerical integration problems using numerical methods like the trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. The code takes the limits of integration and number of subintervals as input, calculates the value of the integral using the specified method, and outputs the result. The problems involve evaluating definite integrals of functions like 1/(1+x), 1/(x^2 + 6x + 10), and 1/(5+3x) over given intervals using different numerical methods and numbers of subintervals.

Uploaded by

Vivek Kumar
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/ 9

National Institute of

Technology,
Delhi

Numerical Methods(MAP 281)


Assignment

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.

x 0.0 0.3 0.6 0.9 1.2


F(x) 1.0000 1.8221 3.3201 6.0496 11.0232

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);
}

// Function to evalute the value of integral


float trapezoidal(float a, float b, float n)
{
// Grid spacing
float h = (b - a) / n;
// Computing sum of first and last terms in above formula.
float s = y(a) + y(b);

// Adding middle terms in above formula


for (int i = 1; i < n; i++)
s += 2 * y(a + i * h);

// h/2 indicates (b-a)/2n. Multiplying h/2 with s.


return (h / 2) * s;
}

// Driver program to test above function


int main()
{
// Range of definite integral
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
float x0 = 0;
float xn = 1;
int n;
// cout << "Enter lower limit" << endl;
// cin >> x0;
// cout << "Enter upper limit" << endl;
// cin >> xn;
cout << "Enter number of intervals in which area is divided" << endl;
cin >> n;

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;

// Function to calculate f(x)


float func(float x)
{
return 1 / (x * x + 6 * x + 10);
}

// Function for approximate integral


float simpsons_(float ll, float ul, int n)
{
// Calculating the value of h
float h = (ul - ll) / n;

// Array for storing value of x and f(x)


float x[10], fx[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++)
{
x[i] = ll + i * h;
fx[i] = func(x[i]);
}

// 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;

// Given function to be integrated


float func(float x)
{
return (1 / (5 + 3 * x));
}

// Function to perform calculations


float calculate(float lower_limit, float upper_limit,
int interval_limit)
{
float value;
float interval_size = (upper_limit - lower_limit) / interval_limit;
float sum = func(lower_limit) + func(upper_limit);

// Calculates value till integral limit


for (int i = 1; i < interval_limit; i++)
{
if (i % 3 == 0)
sum = sum + 2 * func(lower_limit + i * interval_size);
else
sum = sum + 3 * func(lower_limit + i * interval_size);
}
return (3 * interval_size / 8) * sum;
}

// 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);

cout << "Value of integral is " << integral_res;


return 0;
}
OUTPUT CONSOLE:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy