0% found this document useful (0 votes)
130 views14 pages

BCSL 58 em

1. The document describes a program to calculate the value of Sin(π/6) using Lagrange's interpolation method. Related data provided includes x values of 0, π/4, π/2 and corresponding y (Sin(x)) values. 2. The program initializes arrays to store the x and y coordinate data points. It generates additional x values between 0 and π/2. 3. Lagrange's interpolation routine is initialized with the known data points and interrogated to find the interpolated y values, including the value of Sin(π/6).

Uploaded by

AnjnaKandari
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)
130 views14 pages

BCSL 58 em

1. The document describes a program to calculate the value of Sin(π/6) using Lagrange's interpolation method. Related data provided includes x values of 0, π/4, π/2 and corresponding y (Sin(x)) values. 2. The program initializes arrays to store the x and y coordinate data points. It generates additional x values between 0 and π/2. 3. Lagrange's interpolation routine is initialized with the known data points and interrogated to find the interpolated y values, including the value of Sin(π/6).

Uploaded by

AnjnaKandari
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/ 14

n g

di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T

1
n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T

2
B.C.S.L.-058
Computer-oriented Numerical
Techniques Lab
Disclaimer/Special Note: These are just the sample of the Answers/Solutions to some of the Questions given in the
Assignments. These Sample Answers/Solutions are prepared by Private Teacher/Tutors/Authors for the help and guidance
of the student to get an idea of how he/she can answer the Questions given the Assignments. We do not claim 100%
accuracy of these sample answers as these are based on the knowledge and capability of Private Teacher/Tutor. Sample

g
answers may be seen as the Guide/Help for the reference to prepare the answers of the Questions given in the assignment.

n
As these solutions and answers are prepared by the private Teacher/Tutor so the chances of error or mistake cannot be

di
denied. Any Omission or Error is highly regretted though every care has been taken while preparing these Sample Answers/
Solutions. Please consult your own Teacher/Tutor before you prepare a Particular Answer and for up-to-date and exact

a
information, data and solution. Student should must read and refer the official study material provided by the university.

Re
Note: The programs are to be written in C/C++ and/or in MS-Excel/Any spread sheet.
Q.1. Write a program in C/C++ to find the solution of system of linear equations (given below), by using
Gauss-Elimination method:
e
in ks
x+y+z=2
x + 2y + 3z = 14
x + 3z = 14 l
n o
x + 3y – 6z = – 23
O b o
Ans. #include <stdio.h>

o r -
f E
#include <stdlib.h>
#include<math.h>
int main()
b n d
u a
{

H
int n,i,j,k;
float a[10][10]={0.0}, x[10]={0.0};

e
float pivot = 0.0;

h
float factor = 0.0;

T
float sum = 0.0;
printf(“Gauss Elimination \n\n”);
printf(“Enter number of equations:”);
scanf(“%d”,&n);
printf(“%d\n”,n);
printf(“\n\t Input Coefficients a[i, j+1], row-wise\n”);
for(i=l;i<=n;i++)\
{
for(i=1;<=n+1;j++)

3
{
scanf(“%f’,&a[i][j]);
}
}
printf(“\n\n”);
for(i=l;i<=n;i++)
}
printf(“\t%10.0f”,a[i][j]);
}
printf(“\n\n”);
}
for(k=l;k<=n-l;k++)
{

g
if(a[k][k]==0.0)

in
printf(“error”);

d
}

a
else
{
pivot = a[k][k];
for(j=k;j<=n+l;j++)
Re
e
a[k]Q]=a[k][j]/pivot;

in ks
for(i=k+l;i<=n;i++)

l
{

n o
factor =a[i][k];
for(j = k;j<=n+l;j++)
a[i][j]=a[i][j]- factor * a[k]Q];
O b o
r -
}

fo E
}
}

b d
if(a[n][n]==0)

u a n
printf(“error”)

H
else
{

e
x[n] = a(n)(n+1)/a(n)(n);

for(i=n–1;i>=1;i--)
{
T h
sum = 0.0;
for(j=i+1;j<=n;j++)
sum = sum + a[i][j] * x[j];
x(i)=(a[i][n+1]–sum)/a[i] [i];
for(i=l;i<=n;i++)
}
}
}

4
for(i=1;i<=n;i++)
{
printf(“\n\tx[%ld]=%10.4f”,i/x[i]);
}
system(“PAUSE”);
return 0;
}
Output:

n g
di
ea
R
e
lin ks
n o
Q. 2. Write a program in C/C++ to determine the approximate value of the definite integral (I), by using
Simpson’s (1/3)rd rule:

O b o
r -
10
I= x 1/ 3 dx

o
0.2

f E
Using step size (h) = 0.2 .

b d
Ans. #include<studio.h>

u a n
#include<conio.h>
float f(f loat x)
{
H
e
return(l/(l+x});

h
}

T
void main()
int i,n;
float xO,xn,h,y[20],so,se,ans,x[20];
printf(“\n Enter values of xO,xn,h: “);
scanf(“%f%f%f”,&xO,&xn,&h);
n=(xn-xO)/h;
if(n%2==l)
{
n=n+l;

5
}
h=(xn-xO)/n;
printf(“\n Refined value of n and h are:%d %f\n”,n/h);
printf(“\nY values: \n”);
for(i=0; i<=n; i++)
{
{ x[i]=xO+i*h;
y[i]=f(x[i];
printf(“\n%f\n”,y[i]);
}
so=0;
se=0;
for(i=l; i<n; i++)

n g
i
{

d
if(i%2==l)

a
{

e
so=so+y[i];

R
}

e
else

in ks
{

l
se=se+y[i];

n o
}

o
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
O
r -
printf(“\n Final integration is %f” ,ans);
b
getch();
fo E
d
}
}
Output: b
u a n
H
h e
T

6
Q. 3. Write a program in C/C++ to find the value of Sin(/6) by using Lagrange’s Interpolation, the
related data is given below:
x : 0  /4  /2
y = Sin(x) : 0 0.70711 1.0
Ans. #include <cmath>
#include <iostream>
#include <iomanip>
Using namespace std;
#define PI 3.1415
#define N 12
int main()
{

g
// Declare and initialize two arrays to hold the coordinates of the initial data points.

n
double x[N],y[N];

i
// Generate the points

d
double xx = PI, step = 4 * PI / (N -1);

a
for (int i = 0; i < N; ++i, xx += step) {

e
x[i] = xx;
y[i] = sin(2 * xx) /xx;
}
R
e
// Initialize the Lagrange interpolation routine with known data points

in ks
Maths::lnterpolation::Lagrange A(N, x, y);

l
// Interrogate Lagrange polynomial to find interpolated values
int IM_out = 20;
xx = PI, step = (3 * PI) / (N_out -1);
n o
O o
for (int i = 0; i < N_out; ++i, xx += step) {

r - b
cout<< “x = ” « setw(7) << xx << “ y = ”;
cout<< setw(13) << A.get Value(xx, 3)<< endl;
}
return 0;
fo E
}
b n d
u a
Output

H
h e
T

7
Q. 4. Write a program in C/C++ to calculate the value of “cos x” by using the series expansion given
below:
x2 x4 x6
cos x = 1 – ...
2! 4! 6!
Note: Evaluate cos x only upto first three terms.
Also find the value of cos x by using the inbuilt function.
Compare the results i.e., the result produced by your program and that produced by inbuilt function.
Based on comparison, determine error.
Ans. #include <bits/stdc++.h>
using namespace std;
const double PI =3.142;

g
double cosXSertiesSum(double x, int n)

n
{

i
x = x * (PI /180.0);

d
double res = 1;

a
double sign = 1, fact = 1,

e
pow = 1;

R
for (int i = 1; i < 5; i++)
{

e
sign = sign * -1;

in ks
fact = fact * (2 * i - 1) * (2 * i);

l
pow = pow * x * x;

n o
res = res + sign *

O o
pow / fact;

b
}
return res;

o r -
f E
}

d
//Driver Code

b
int main()
{
u a n
H
float x = 50;
int n = 5;

e
cout « cosXSertiesSumfx, 5);

h
return 0;

T
}
Output:

8
6Q. 5. Write a program in C/C++ to find the root of the following equation by using “Bisection Method” :

Equation:
x3 – 5x + 1 = 0; x [1, 2]

Ans. #include<stdio.h>
//function used is xA3-5x+l
double func(double ×)
{
return x*x*x- 5*x+ 1;
}
double e=0.01;
double c;

g
void bisection(double a,double b)

n
{
if(func(a) * func(b) >= 0)
{
di
a
printf(“lncorrect a and b”);

e
return;

R
}
c = a;

e
while ((b-a) >= e)

in ks
{

l
c = (a+b)/2;

n o
if (func(c) == 0.0){
printf(“Root = %lf\n”,c);

O o
break;

r - b
}

o
elseif(func(c)*func(a)<0){

f E
printf(“Root = %lf\n”,c);

d
b = c;
else{
printf(“Root = %lf\n”,c); b
u a n
H
a = c;
}

e
}

h
}

T
int main()
}
double a,b;
a= – 10;
b = 20;
printf(“The function used is x^3-5x+l\n”);
printf(“a = %lf\n”,a);
printf(“b = %lf\n”,b);
bisection(a,b);
printf(“\n”);

9
printf(“Accurate Root calculated is = %lf\n”,c);
return 0;
}
Output:

n g
di
a
Q. 6. Write a program in C/C++ to approximate the value of Integral (I), by using Trapezoidal rule:

e
1 dx

R
I= 0.2
5+ x

e
Using step size (h) = 0.2 .

in ks
Ans. #include<studio.h>

l
#include<conio.h>

n o
#include<math.h>

O o
float f(float x)

r - b
{

o
return(l/(l+pow(x,2)));
void main()
f E
b d
{

u a n
int i,n;

H
float xO,xn,h,y[20],so,se”,ans,x[20];
printf(“\n Enter values of xO,xn,h:\n”);

e
scanf(“%f%f%f”,&xO,&xn,&h);

h
n=(xn-xO)/h;

T
if(n%2==l)
{
n=n+l;
}
h=(xn-xO)/n;
printf(“\nrefined value of n and h are:%d %f\n”,n,h);
printf(“\n Y values\n”);
for(i=0; i<=n; i++)
{

10
x[i]=x0+i*h;
printf(“\n%An”,y[i]);
}
so=0;
se=0;
for(i=l; kn; i++)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}

n g
i
if(i%2==l)

d
{

a
printf(“\nfinal integration is %f”, ans);

e
getch();
}
Output:
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T
Q. 7. Write a program in C or C++ to demonstrate the operation of the following operations, for the
function f(x) = x2+ x + 7:
(a) Forward Difference Operator
Ans. #include<conio.h>
#include <stdio.h>
#define F(x)(x)*(x)+(x) +7/FUNCTION F(x)
void main ()
{

11
int n;
float a,b, h, x;
double f1, f2, f0, WDOP, BWDOP, CDOP, AVGOP;
/*FWDOP-FORWARD DIFFERENCE OPERATOR*
clrscr();
printf(“\n Enter the inverval (a,b”);
printf(“/nEnter the value of a: ”);
Scanf(“f”, &a);
printf (“\nEnter the number of nodes (n)in the inverval (a,b):”);
Scanf(“%d”, &n);
h=(b-a)/n;//STEP SIZE CALCULATION
printf (“\nSTEP SIZE(h) = :%f” h);”
Printf (“nVALUE OF POINT OF EVALUATION (X) : ”);

n g
i
Scanf(“f”, & x);

d
FWDOP = F(x + h) -F(x);

a
printf(“\n RESULT OF FORWARD DIFFERENCE OPEATOR = % f”, FWDOP);

e
getch();

R
}

e
Output:

lin ks
n o
O b o
o r -
f E
b n d
u a
(b) Central Difference Operator
Ans. include<conio.h>
H
e
#include,stdio.h>

h
#define F(x) (x)* (x) + (x) + 7 // FUNCTION F(X)

T
void main ()
{
int n;
float a, b, h, x;
double f1, f2, f0, FWDOP, BWDOP, CDOP, AVGOP;
/*CDOP - CENTRAL DIfference OPERATOR*/
clrscr();
printf(“\n Enter the interval (a,b)”);
printf(“\nEnter the value of a:”);

12
Scanf(“%f”, &b);
printf(“\nEnter the number of nodes(n) in the interval (a,b):”);
scanf(“%d”, &n);
h=(b-a)/n;//STEP SIZE CALCULATION
printf(“nSTEP SIZE (h) = : % f”, h);
printf(“n VALUE OF POINT OF EVALUATION (X) :”);
Scanf(“%f”, &x);
CDOP = F(x+(h/2)) - F(x-(h/2));
printf(“\n RESULT OF CENTRAL DIFFERENCE OPEATOR = %f” CDOP);
getch();
}
Output:

n g
di
ea
R
e
lin ks
n o
Q. 8. Write a program in C or C++ to calculate the value of ex by using its series expansion given below:

O o
x2 x3
ex = 1 + x + + ...

r - b
2! 3!

o
Note: Evaluate ex only upto first three terms .

f E
Also find the value of ex by using the inbuilt function and compare it with the result produced by your

b d
program.

u a n
Ans. #include<iostream.h>

H
#include<iomanip.h>
#include<conio.h>
void main()
{
h e
int i, n;
T
floast x, sum = 1, t=1;
clrscr();
cout <<“Enter the value for x :”;
cin>>><;
cout<<” Enter the value for n : ”;
cin>>n;
/*Loop to calculate the value of Exponential*/

13
for (i=1; i<=n; i++)
{
t=t*x/i;
sum=sum+t;
}
cout<< “The Exponential Value of” “<<x<<” = “<<setp recision(4) <<sum;”
getch();
}
Output:

n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T

14

You might also like