Course Code : BCSL-058
Course Title : Computer Oriented Numerical Techniques Lab
Assignment Number : BCA(V)/058/Assign/14-15
Problem 2: Write a programme that implements (non-pivoting) Gaussian elimination method for
solving n linear equations in n variables, that calls procedures
(i) lower-triangularisation and
(ii) back substitutions
(codes of procedures are also to be written).
Use the programme for solving the following system of linear equations:
x+2y=10z
2x+y+z=9
5x+3y+2z=23
Solution:
#include<stdio.h>
int main()
{
double matrix[10][10],a,b, temp[10]; int i, j, k, n;
printf(“Enter the no of variables: “); scanf(“%d”, &n);
printf(“Enter the agumented matrix:n”); for(i = 0; i < n ; i++){
for(j = 0; j < (n+1); j++){
scanf(“%lf”, &matrix[i][j]);
}
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(j<i){
a = matrix[j][i];
b = matrix[i][i];
for(k = 0; k < n+1; k++){
matrix[j][k] = matrix[j][k] – (a/b) * matrix[i][k];
}
}
}
}
printf(“The Upper triangular matrix is: n”); for( i = 0; i < n; i++){
for(j = 0; j < n+1; j++){
printf(“%.2f”, matrix[i][j]); printf(“t”);
}
printf(“n”);
}
printf(“nThe required result is: “); for(i = n-1; i>=0; i–){
b = matrix[i][n];
for(j = n-1 ; j > i; j–){
b -= temp[n-j]*matrix[i][j];
}
temp[n-i] = b/matrix[i][i];
printf(“n%c => %.2f”,97+i, temp[n-i]);
}
}
Problem 3: Write a programme that approximates a root of the equation f (x) = 0 in an interval
[a, b] using regula-falsi method. The necessary assumptions for application of regulafalsi method
should be explicitly mentioned. Use the method to find a root of the equation x2-8x+15=0.
Solution:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define ESP 0.001
#define F(x)(x)*(x)-8*(x)+15
void main()
{
int i = 1;
float x0,x1,x2;
double f1,f2,f0,t;
clrscr();
printf(“nEnter the value of x0: “);
scanf(“%f”,&x0);
printf(“nEnter the value of x1: “);
scanf(“%f”,&x1);
printf(“n_______________________________________________________________
___n”);
printf(“niterationt x0t x1t x2t f0t f1t f2″);
printf(“n_______________________________________________________________
____n”);
do
{
x2=(x0*f1-x1*f0)/(f1 - f0);
f0=F(x0);
f1=F(x1);
f2=F(x2);
printf(“n%d %f %f %f %lf %lf %lf”,i,x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0=x2;
}
i++;
}while(fabs(f2)>ESP);
printf(“n__________________________________________________________n”);
printf(“nnApp.root = %f”,x2);
getch();
}
Problem No. 4: Write a programme that approximates a root of the equation f (x) = 0 in interval
[a, b] using Gauss-Seidel method. Use the method to solve the system of linear equations given in
Q. No. 2 above. You may make assumption for appropriate initial values of the variable.
Solution:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_DIM 100
#define MAX_ITER 500
#define TOLERANCE 1.e-6
void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n); void main ()
{
int i, j, n;
int violation_counter, answer;
int violation_rows[MAX_DIM];
double sum;
double a[MAX_DIM][MAX_DIM];
double b[MAX_DIM],x[MAX_DIM];
/* read in data */
n = MAX_DIM + 1;
while (n > MAX_DIM) {
printf (“Enter the dimension of the system to be solved: “); scanf (“%d”,&n);
}
printf (“n”);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf (“Enter the (%d,%d) element of the system matrix: “,i+1,j+1); scanf (“%lf”,&a[i][j]);
}
printf (“Enter element (%d) of the right-hand-side vector: “,i+1); scanf (“%lf”,&b[i]);
printf (“n”);
}
/* test the convergence criterion */
violation_counter = 0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++)
if (i != j)
sum = sum + fabs(a[i][j]);
if (fabs(a[i][j]) < sum) {
violation_rows[violation_counter] = i;
violation_counter = violation_counter + 1;
}
if (a[i][j] == 0.0) {
printf (“Found diagonal element equal to zero; rearrange equations; exiting …n”); exit
(0);
}
}
if (violation_counter > 0) {
printf (“The Gauss_Seidel convergence criterion is violated in %d rows out of %dn”,
violation_counter,n);
printf (“Specifically, it was violated in rows:n”);
for (i = 0; i < violation_counter; i++)
printf (“%d “,violation_rows[i]+1);
printf (“n”);
printf (“Enter 1 if you want to continue; any other number to abort: “); scanf
(“%d”,&answer);
if (answer != 1)
exit(1);
printf (“Check results carefullynn”);
}
/* Initialize the solution vector — initial guesses */
for (i = 0; i < n; i++) {
printf (“Enter an initial guess for element (%d) of the solution vector: “i+1); scanf
(“%lf”,&x[i]);
}
/* solve the system */
gauss_seidel (a,b,x,n);
/* output solution */
for (i = 0; i < n; i++)
printf (“x[%d]=%fn”,i+1,x[i]);
printf (“n”);
}
/* function to solve a system using Gauss-Seidel. THIS IS THE PART WHERE I NEED
HELP!!*/ void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n)
maxerror = 1.0;
while (maxerror > 1.e-6) {
on_error = 0;
for (i = 0; i < n; i++) {
xold = x[i];
/* Need help here! */
e = fabs((x[i] – xold) / x[i]);
if (e > iteration_error)
iteration_error = e;
}
maxerror = iteration_error;
}
Problem No. 5: Write a programme that constructs Newton’s Interpolating Polynomials, for
which at most four nodes are given (hence interpolating polynomial will be at most cubic). Using
the programme, find Newton’s Interpolating polynomial that approximiates f(x)=2×3–x+3 and
the nodes given are x0=-1, x1=0, x2=1 and x3=2. Use the polynomial to approximate value at x =
1.5.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],temp=1,f[10],sum,p; int i,n,j,k=0,c;
clrscr();
printf(“nhow many record you will be enter: “); scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]);
}
printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p);
for(i=0;i<n;i++)
{
temp = 1;
k = i;
for(j=0;j<n;j++)
{
if(k==j)
{
continue;
}
else
{
temp = temp * ((p-x[j])/(x[k]-x[j]));
}
}
f[i]=y[i]*temp;
}
{
sum = sum + f[i];
}
printf(“nn f(%.1f) = %f “,p,sum);
getch();
}
Problem No. 6: Repeat Problem No. 5 for constructing Lagrangian Polynomial instead of
Newton’s Polynomials.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10][10],sum,p,u,temp;
int i,n,j,k=0,f,m;
float fact(int);
clrscr();
printf(“nhow many record you will be enter:”); scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[k][i]);
}
printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p);
for(i=1;i<n;i++)
{
k=i;
for(j=0;j<n-i;j++)
{
y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
k++;
}
}
printf(“n_____________________________________________________n”);
printf(“n x(i)t y(i)t y1(i) y2(i) y3(i) y4(i)”);
printf(“n_____________________________________________________n”);
for(i=0;i<n;i++)
{
printf(“n %.3f”,x[i]);
for(j=0;j<n-i;j++)
{
printf(“ “);
printf(“ %.3f”,y[j][i]);
}
printf(“n”);
}
i=0;
do
{
if(x[i]<p && p<x[i+1])
k=1;
else
i++;
}while(k != 1);
f=i;
sum=0;
for(i=0;i<n-1;i++)
{
k=f;
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (p - x[k]);
k++;
}
sum = sum + temp*(y[i][f]);
}
printf(“nn f(%.2f) = %f “,p,sum);
getch();
}
Problem No.7: Write a programme that approximates the value of a definite integral (
)fb
z f(x)dx using Trapezoidal Rule, with m sample points. Find an approximate value of the
integral of f(x)=3x(cos2 x) over the interval [1, 6].
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf(“nhow many record you will be enter: “); scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
sum = sum + y[i];
sum = sum * (h/2);
printf(“nn I = %f “,sum);
}
getch();
}
Problem No. 8: Write a program that solves Initial Value Problem (IVP) of order
one:y’(x)=dy/dx=f(x,y) with initial condition at x=x0 as y(x0) = y0using Improved Euler’s Method.
Using your program solve the IVP: y’=dy/dx= x2 + y2 − 2, y(0)=1. Compute upto 5 places of
decimal.
Solution:
#include<stdio.h>
#include<conio.h>
float fun(float,float);
void main()
{
int i,n;
float x0,y0[10],h,e;
clrscr();
printf(“enter the value of x0 and y0n”);
scanf(“%f%f”,&x0,&y0[0]);
printf(“enter the value of hn”);
scanf(“%f”,&h);
printf(“enter the value at which you want to findn”); scanf(“%f”,&e);
n=(int)((e-x0)/h+0.5);
for(i=0;i<n;i++)
{
y0[i+1]=y0[i]+h*fun(x0,y0[i]);
x0=x0+h;
}
for(i=0;i<n;i++)
printf(“%f”,y0[i]);
getch();
}
float fun(float x,float y)
{
float a=x*x+y*y-2;
return(a);
}

BCSL 058 solved assignment

  • 1.
    Course Code :BCSL-058 Course Title : Computer Oriented Numerical Techniques Lab Assignment Number : BCA(V)/058/Assign/14-15 Problem 2: Write a programme that implements (non-pivoting) Gaussian elimination method for solving n linear equations in n variables, that calls procedures (i) lower-triangularisation and (ii) back substitutions (codes of procedures are also to be written). Use the programme for solving the following system of linear equations: x+2y=10z 2x+y+z=9 5x+3y+2z=23 Solution: #include<stdio.h> int main() { double matrix[10][10],a,b, temp[10]; int i, j, k, n; printf(“Enter the no of variables: “); scanf(“%d”, &n); printf(“Enter the agumented matrix:n”); for(i = 0; i < n ; i++){ for(j = 0; j < (n+1); j++){ scanf(“%lf”, &matrix[i][j]); } } for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ if(j<i){ a = matrix[j][i]; b = matrix[i][i]; for(k = 0; k < n+1; k++){ matrix[j][k] = matrix[j][k] – (a/b) * matrix[i][k]; } } } } printf(“The Upper triangular matrix is: n”); for( i = 0; i < n; i++){ for(j = 0; j < n+1; j++){ printf(“%.2f”, matrix[i][j]); printf(“t”); } printf(“n”); }
  • 2.
    printf(“nThe required resultis: “); for(i = n-1; i>=0; i–){ b = matrix[i][n]; for(j = n-1 ; j > i; j–){ b -= temp[n-j]*matrix[i][j]; } temp[n-i] = b/matrix[i][i]; printf(“n%c => %.2f”,97+i, temp[n-i]); } } Problem 3: Write a programme that approximates a root of the equation f (x) = 0 in an interval [a, b] using regula-falsi method. The necessary assumptions for application of regulafalsi method should be explicitly mentioned. Use the method to find a root of the equation x2-8x+15=0. Solution: #include<stdio.h> #include<math.h> #include<conio.h> #define ESP 0.001 #define F(x)(x)*(x)-8*(x)+15 void main() { int i = 1; float x0,x1,x2; double f1,f2,f0,t; clrscr(); printf(“nEnter the value of x0: “); scanf(“%f”,&x0); printf(“nEnter the value of x1: “); scanf(“%f”,&x1); printf(“n_______________________________________________________________ ___n”); printf(“niterationt x0t x1t x2t f0t f1t f2″); printf(“n_______________________________________________________________ ____n”); do { x2=(x0*f1-x1*f0)/(f1 - f0); f0=F(x0); f1=F(x1); f2=F(x2);
  • 3.
    printf(“n%d %f %f%f %lf %lf %lf”,i,x0,x1,x2,f0,f1,f2); if(f0*f2<0) { x1=x2; } else { x0=x2; } i++; }while(fabs(f2)>ESP); printf(“n__________________________________________________________n”); printf(“nnApp.root = %f”,x2); getch(); } Problem No. 4: Write a programme that approximates a root of the equation f (x) = 0 in interval [a, b] using Gauss-Seidel method. Use the method to solve the system of linear equations given in Q. No. 2 above. You may make assumption for appropriate initial values of the variable. Solution: #include <stdio.h> #include <math.h> #include <stdlib.h> #define MAX_DIM 100 #define MAX_ITER 500 #define TOLERANCE 1.e-6 void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n); void main () { int i, j, n; int violation_counter, answer; int violation_rows[MAX_DIM]; double sum; double a[MAX_DIM][MAX_DIM]; double b[MAX_DIM],x[MAX_DIM]; /* read in data */ n = MAX_DIM + 1; while (n > MAX_DIM) { printf (“Enter the dimension of the system to be solved: “); scanf (“%d”,&n); } printf (“n”);
  • 4.
    for (i =0; i < n; i++) { for (j = 0; j < n; j++) { printf (“Enter the (%d,%d) element of the system matrix: “,i+1,j+1); scanf (“%lf”,&a[i][j]); } printf (“Enter element (%d) of the right-hand-side vector: “,i+1); scanf (“%lf”,&b[i]); printf (“n”); } /* test the convergence criterion */ violation_counter = 0; for (i = 0; i < n; i++) { sum = 0.0; for (j = 0; j < n; j++) if (i != j) sum = sum + fabs(a[i][j]); if (fabs(a[i][j]) < sum) { violation_rows[violation_counter] = i; violation_counter = violation_counter + 1; } if (a[i][j] == 0.0) { printf (“Found diagonal element equal to zero; rearrange equations; exiting …n”); exit (0); } } if (violation_counter > 0) { printf (“The Gauss_Seidel convergence criterion is violated in %d rows out of %dn”, violation_counter,n); printf (“Specifically, it was violated in rows:n”); for (i = 0; i < violation_counter; i++) printf (“%d “,violation_rows[i]+1); printf (“n”); printf (“Enter 1 if you want to continue; any other number to abort: “); scanf (“%d”,&answer); if (answer != 1) exit(1); printf (“Check results carefullynn”); } /* Initialize the solution vector — initial guesses */ for (i = 0; i < n; i++) { printf (“Enter an initial guess for element (%d) of the solution vector: “i+1); scanf (“%lf”,&x[i]);
  • 5.
    } /* solve thesystem */ gauss_seidel (a,b,x,n); /* output solution */ for (i = 0; i < n; i++) printf (“x[%d]=%fn”,i+1,x[i]); printf (“n”); } /* function to solve a system using Gauss-Seidel. THIS IS THE PART WHERE I NEED HELP!!*/ void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n) maxerror = 1.0; while (maxerror > 1.e-6) { on_error = 0; for (i = 0; i < n; i++) { xold = x[i]; /* Need help here! */ e = fabs((x[i] – xold) / x[i]); if (e > iteration_error) iteration_error = e; } maxerror = iteration_error; } Problem No. 5: Write a programme that constructs Newton’s Interpolating Polynomials, for which at most four nodes are given (hence interpolating polynomial will be at most cubic). Using the programme, find Newton’s Interpolating polynomial that approximiates f(x)=2×3–x+3 and the nodes given are x0=-1, x1=0, x2=1 and x3=2. Use the polynomial to approximate value at x = 1.5. Solution: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],temp=1,f[10],sum,p; int i,n,j,k=0,c; clrscr(); printf(“nhow many record you will be enter: “); scanf(“%d”,&n); for(i=0; i<n; i++) { printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
  • 6.
    printf(“nnenter the valueof f(x%d): “,i); scanf(“%f”,&y[i]); } printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p); for(i=0;i<n;i++) { temp = 1; k = i; for(j=0;j<n;j++) { if(k==j) { continue; } else { temp = temp * ((p-x[j])/(x[k]-x[j])); } } f[i]=y[i]*temp; } { sum = sum + f[i]; } printf(“nn f(%.1f) = %f “,p,sum); getch(); } Problem No. 6: Repeat Problem No. 5 for constructing Lagrangian Polynomial instead of Newton’s Polynomials. Solution: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10][10],sum,p,u,temp; int i,n,j,k=0,f,m; float fact(int); clrscr(); printf(“nhow many record you will be enter:”); scanf(“%d”,&n);
  • 7.
    for(i=0; i<n; i++) { printf(“nnenterthe value of x%d: “,i); scanf(“%f”,&x[i]); printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[k][i]); } printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p); for(i=1;i<n;i++) { k=i; for(j=0;j<n-i;j++) { y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]); k++; } } printf(“n_____________________________________________________n”); printf(“n x(i)t y(i)t y1(i) y2(i) y3(i) y4(i)”); printf(“n_____________________________________________________n”); for(i=0;i<n;i++) { printf(“n %.3f”,x[i]); for(j=0;j<n-i;j++) { printf(“ “); printf(“ %.3f”,y[j][i]); } printf(“n”); } i=0; do { if(x[i]<p && p<x[i+1]) k=1; else i++; }while(k != 1); f=i; sum=0; for(i=0;i<n-1;i++) {
  • 8.
    k=f; temp=1; for(j=0;j<i;j++) { temp = temp* (p - x[k]); k++; } sum = sum + temp*(y[i][f]); } printf(“nn f(%.2f) = %f “,p,sum); getch(); } Problem No.7: Write a programme that approximates the value of a definite integral ( )fb z f(x)dx using Trapezoidal Rule, with m sample points. Find an approximate value of the integral of f(x)=3x(cos2 x) over the interval [1, 6]. Solution: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); printf(“nhow many record you will be enter: “); scanf(“%d”,&n); for(i=0; i<n; i++) { printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]); printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]); } h=x[1]-x[0]; n=n-1; sum = sum + y[0]; for(i=1;i<n;i++) { sum = sum + y[i]; sum = sum * (h/2); printf(“nn I = %f “,sum);
  • 9.
    } getch(); } Problem No. 8:Write a program that solves Initial Value Problem (IVP) of order one:y’(x)=dy/dx=f(x,y) with initial condition at x=x0 as y(x0) = y0using Improved Euler’s Method. Using your program solve the IVP: y’=dy/dx= x2 + y2 − 2, y(0)=1. Compute upto 5 places of decimal. Solution: #include<stdio.h> #include<conio.h> float fun(float,float); void main() { int i,n; float x0,y0[10],h,e; clrscr(); printf(“enter the value of x0 and y0n”); scanf(“%f%f”,&x0,&y0[0]); printf(“enter the value of hn”); scanf(“%f”,&h); printf(“enter the value at which you want to findn”); scanf(“%f”,&e); n=(int)((e-x0)/h+0.5); for(i=0;i<n;i++) { y0[i+1]=y0[i]+h*fun(x0,y0[i]); x0=x0+h; } for(i=0;i<n;i++) printf(“%f”,y0[i]); getch(); } float fun(float x,float y) { float a=x*x+y*y-2; return(a); }