0% found this document useful (0 votes)
2K views17 pages

BCSL 058 Computer Oriented Numerical Techniques Lab Solved Assignment 2019 20

The document contains the solutions to 7 questions related to numerical analysis techniques in C/C++. The questions cover topics like solving systems of linear equations using Gauss elimination, approximating integrals using Simpson's rule and trapezoidal rule, Lagrange interpolation, Taylor series expansion for cosine, root finding using bisection method, and forward/central difference operators. Programs with inputs/outputs are provided as solutions for each question.

Uploaded by

mahesh sharma
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)
2K views17 pages

BCSL 058 Computer Oriented Numerical Techniques Lab Solved Assignment 2019 20

The document contains the solutions to 7 questions related to numerical analysis techniques in C/C++. The questions cover topics like solving systems of linear equations using Gauss elimination, approximating integrals using Simpson's rule and trapezoidal rule, Lagrange interpolation, Taylor series expansion for cosine, root finding using bisection method, and forward/central difference operators. Programs with inputs/outputs are provided as solutions for each question.

Uploaded by

mahesh sharma
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/ 17

www.techiya.

in

www.techiya.in
BCA Solved Assignment 2019-20

Course Code : BCSL-058


Course Title : Computer oriented Numerical techniques Lab
Assignment Number: BCA(5)/L-058/Assignment/2019-20

Q1. Write a program in C/C++ to find the solution of system of linear equations
(given below), by suing Gauss- Elimination method:
𝑥+𝑦+𝑧=2
𝑥−2𝑦+3𝑧=14
𝑥+3𝑦−6𝑧=−23
Ans: program.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,i,j,k;
float a[10][10]={0.0}, x[10]={0.0};
float pivot = 0.0;
float factor = 0.0;
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=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)

1
www.techiya.in
www.techiya.in
{
scanf("%f",&a[i][j]);
}
}
printf("\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("\t%10.0f",a[i][j]);
}
printf("\n\n");
}
for(k=1;k<=n-1;k++)
{
if(a[k][k]==0.0)
{
printf("error");

}
else
{
pivot = a[k][k];
for(j=k;j<=n+1;j++)
a[k][j]= a[k][j]/pivot;
for(i=k+1;i<=n;i++)
{
factor = a[i][k];
for(j = k;j<=n+1;j++)
{
a[i][j] = a[i][j] - factor * a[k][j];
}
}
}
if(a[n][n]==0)
printf("error");
else
{
x[n] = a[n][n+1]/a[n][n];
2
www.techiya.in
www.techiya.in
for(i=n-1;i>=1;i--)
{
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=1;i<=n;i++)
{
printf("\n\tx[%1d]=%10.4f",i,x[i]);
}
system("PAUSE");
return 0;
}

Output:

3
www.techiya.in
www.techiya.in

Q2. Write a program in C/C++ to determine the approximate value of the


definite integral (I), by using Simpson’s (1/3)rd rule:

Using step size (ℎ) = 0.2.

Ans: program.c
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
4
www.techiya.in
www.techiya.in
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);
getch();
}

Output:

5
www.techiya.in
www.techiya.in
Q3. 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()
{
// Declare and initialize two arrays to hold the coordinates of the initial data
points
double x[N], y[N];

// Generate the points


double xx = PI, step = 4 * PI / (N - 1);
for (int i = 0; i < N; ++i, xx += step) {
x[i] = xx;
y[i] = sin(2 * xx) / xx;
}

// Initialize the Lagrange interpolation routine with known data points


Maths::Interpolation::Lagrange A(N, x, y);

// Interrogate Lagrange polynomial to find interpolated values


int N_out = 20;
xx = PI, step = (3 * PI) / (N_out - 1);
for (int i = 0; i < N_out; ++i, xx += step) {
cout << "x = " << setw(7) << xx << " y = ";
cout << setw(13) << A.getValue(xx, 3) << endl;
}
return 0;
}
6
www.techiya.in
www.techiya.in

Output:

Q4. Write a program in C/C++ to calculate the value of “cos 𝑥” by using the
series expansion given below:

cos𝑥= 1−𝑥2/2!+𝑥4/4!−𝑥6/6!+⋯

Ans: program.cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double cosXSertiesSum(double x, int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
7
www.techiya.in
www.techiya.in
{
sign = sign * -1;
fact = fact * (2 * i - 1) * (2 * i);
pow = pow * x * x;
res = res + sign *
pow / fact;
}
return res;
}
// Driver Code
int main()
{
float x = 50;
int n = 5;
cout << cosXSertiesSum(x, 5);
return 0;
}

Output:

8
www.techiya.in
www.techiya.in

Q5. Write a program in C/C++ to find the root of the following equation by using
“Bisection Method”:
Equation:
𝑥3−5𝑥+1=0; 𝑥∈[1,2]

Ans: program.c
#include<stdio.h>
//function used is x^3-5x+1
double func(double x)
{
return x*x*x - 5*x + 1;
}

double e=0.01;
double c;

void bisection(double a,double b)


{
if(func(a) * func(b) >= 0)
{
printf("Incorrect a and b");

9
www.techiya.in
www.techiya.in
return;
}
c = a;
while ((b-a) >= e)
{
c = (a+b)/2;
if (func(c) == 0.0){
printf("Root = %lf\n",c);
break;
}
else if (func(c)*func(a) < 0){
printf("Root = %lf\n",c);
b = c;
}
else{
printf("Root = %lf\n",c);
a = c;
}
}
}
int main()
{
double a,b;
a=-10;
b=20;
printf("The function used is x^3-5x+1\n");
printf("a = %lf\n",a);
printf("b = %lf\n",b);
bisection(a,b);
printf("\n");
printf("Accurate Root calculated is = %lf\n",c);
return 0;
}

Output:

10
www.techiya.in
www.techiya.in

Q6. Write a program in C/C++ to approximate the value of Integral (I), by using
Trapezoidal rule:

Using step size (ℎ)=0.2 .

Ans: program.c
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;

11
www.techiya.in
www.techiya.in
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/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++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}

Output:

12
www.techiya.in
www.techiya.in

Q7. Write a program in C or C++ to demonstrate the operation of the following


operations, for the function (𝑥)=𝑥2+𝑥+7∶
(a) Forward Difference Operator

Ans: forward.c
#include<conio.h>
#include<stdio.h>
#define F(x) (x)*(x) + (x) + 7 //FUNCTION F(X)
void main()
{
int n;
float a,b,h,x;
double f1,f2,f0,FWDOP,BWDOP,CDOP,AVGOP;
/*FWDOP – FORWARD DIFFERENCE OPERATOR*/
clrscr();
printf("\n Enter the interval (a,b)");
printf("\nEnter the value of a: ");
scanf("%f",&a);
printf("\nEnter the value of b: ");
scanf("%f",&b);
printf("\nEnter the number of nodes(n) in the interval (a,b): ");

13
www.techiya.in
www.techiya.in
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);
FWDOP = F(x+h) - F(x);
printf("\n RESULT OF FORWARD DIFFERENCE OPEATOR = %f",FWDOP);
getch();
}

Output:

(b) Central Difference Operator


Ans: central.c
include<conio.h>
#include<stdio.h>
#define F(x) (x)*(x) + (x) + 7 //FUNCTION F(X)
void main()
{
int n;
float a,b,h,x;
double f1,f2,f0,FWDOP,BWDOP,CDOP,AVGOP;

14
www.techiya.in
www.techiya.in
/* CDOP – CENTRAL DIFFERENCE OPERATOR */
clrscr();
printf("\n Enter the interval (a,b)");
printf("\nEnter the value of a: ");
scanf("%f",&a);
printf("\nEnter the value of b: ");
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:

15
www.techiya.in
www.techiya.in

Q8. Write a program in C or C++ to calculate the value of 𝑒𝑥 by suing its series
expansion, given below:

𝑒𝑥=1+𝑥+𝑥2/2!+𝑥3/3!+⋯

Note: Evaluate 𝑒𝑥only upto first three terms.


Also find the value of 𝑒𝑥 by using the inbuilt function and compare it with the
result produced by your program.

Ans: expo.c

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>

void main()
{
int i, n;
float x, sum=1, t=1;
clrscr();

cout<<" Enter the value for x : ";


cin>>x;

cout<<" Enter the value for n : ";


cin>>n;

/* Loop to calculate the value of Exponential */


for(i=1;i<=n;i++)
{
t=t*x/i;
sum=sum+t;
}

cout<<" The Exponential Value of "<<x<<" = "<<setprecision(4)<<sum;


getch();
}

16
www.techiya.in
www.techiya.in

Output:

17
www.techiya.in

You might also like