Numerical analysis

41
NUMERICAL ANALYSIS AND STATISTICAL TECHNIQUES LAB ETMA -252 SUBMITTED TO SUBMITTED BY URFI SIR VISHAL SINGH MAE- IV SEMESTER 00320903613

Transcript of Numerical analysis

Page 1: Numerical analysis

NUMERICAL ANALYSIS AND

STATISTICAL TECHNIQUES LAB ETMA -252

SUBMITTED TO SUBMITTED BY

URFI SIR VISHAL SINGH

MAE- IV SEMESTER

00320903613

Page 2: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |i

INDEX

S. No. Name of the Experiment Page No.

Date Remarks

1 To write a program in C language for finding the Largest Number out of three integers.

1

2 Describe the different type of constant and variables in ‘C’.

3

3 To write a program in C language for finding Sum and Average of ‘n’ integer numbers.

5

4 To write a program in C language for finding the Roots of given Quadratic Equation.

7

5 To write a program in C language for finding sum of series of function Sin(x) up to 5th term.

10

6 To write a program in C language for finding value of function at given value of argument on the basis of given data by using Newton’s Divided Difference method.

13

7 To write a program in C language for finding value of function at given value of argument on the basis of given data by using Lagrange’s Method.

16

8 To write a program in C language for calculating value of integration within given limits and number of intervals by Trapezoidal Rule.

19

9 To write a program in C language for calculating the value of integration within given limits and number of intervals by Simpson’s 1/3rd and 3/8th Rule.

22

10 To write a program in C language for solving simultaneous equation in ‘n’ variables by using Gauss Elimination Method.

25

11 To write a program in C language for solving simultaneous equation in ‘n’ variables by using Gauss Siedal Method.

29

12 To write a program in C language for solving given differential equation by Runge-Kutta Method.

32

13 To write a program in C language for solving given differential equation (dy/dx=-0.01y) and find the amount of radioactive material left given initial amount and final time by Modified Euler Method.

35

14 To write a program in C language for solving a linear or transcendental equation by Newton-Raphson Method.

38

Page 3: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |1

Experiment 1

Aim

To write a program in C language for finding the largest number out of three integers.

Flowchart

Page 4: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |2

Program

#include<stdio.h>

#include<conio.h>

int main()

{

inta,b,c;

printf("\n enter the numbers a,b,c \n");

scanf("%d%d%d",&a,&b,&c);

if(a>b&&a>c)

printf("a=%d is greatest ",a);

else

{

if(b>c)

{

printf("b=%d is greatest ",b);

}

else

printf("c=%d is greatest ",c);

}

getch();

return 0;

}

Output

Page 5: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |3

Experiment 2

Aim

Describe the different type of constant and variables in ‘C’.

Theory

Variables Variables are memory location in computer's memory to store data. To indicate the memory location, each variable should be given a unique name called identifier. Variable names are just the symbolic representation of a memory location.

Examples of variable name: sum, car no., count etc.

In C, variables can be classified as:

Numeric variables Numeric variables can either be of the type integer (int) or of the type real (float). Integer (int) values are whole numbers (like 10 or -10). Real (float) values can have a decimal point in them. (Like 1.23 or -20.123).

Character variables Character variables are letters of the alphabet, ASCII characters or numbers 0-9. If you declare a character variable you must always put the character between single quotes (like so ‘A’). So remember a number without single quotes is not the same as a character with single quotes.

Constants Constants are the terms that can't be changed during the execution of a program. For example: 1, 2.5, "Programming is easy." etc.

In C, constants can be classified as:

Integer constants Integer constants are the numeric constants (constant associated with number) without any fractional part or exponential part. There are three types of integer constants in C language: Decimal constant (base 10), octal constant (base 8) and hexadecimal constant (base 16).

Page 6: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |4

Floating-point constants Floating point constants are the numeric constants that has either fractional form or exponent form.

For example: -2.0, 0.0000234,-0.22E-5

Character constants Character constants are the constant which use single quotation around characters. For example: 'a', 'l', 'm', 'F' etc.

String constants String constants are the constants which are enclosed in a pair of double -quote marks. For example:

"Good" //string constant

" " //string constant of six white space

"x" //string constant having single character.

"Earth is round\n" //prints string with newline

Enumeration constants Keyword enum is used to declare enumeration types. For example enum color {yellow, green, black, white};

Result

The different types variables and constant in ‘C’ programming language has been studied.

Page 7: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |5

Experiment 3

Aim

To write a program in C language for finding Sum and Average of ‘n’ integer numbers.

Flowchart

Page 8: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |6

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int n,i;

float x,sum,avg;

sum=0.00;

printf("\n Enter the number of data \n");

scanf("%d",&n);

printf("\n enter the data\n");

for(i=1;i<=n;i++)

{

scanf("%f",&x);

sum=sum+x;

}

avg=sum/n;

printf("\n Sum of %d numbers = %f",n,sum);

printf("\n Average of %d numbers = %f",n,avg);

getch();

return 0;

}

Output

Page 9: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |7

Experiment 4

Aim

To write a program in C language for finding the Roots of given Quadratic Equation

Flowchart

Page 10: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |8

Program

#include<stdio.h>

#include<math.h>

#include<conio.h>

int main()

{

int a,b,c;

float d,x1,x2;

printf("\n enter the value of coefficients a,b,c \n");

scanf("%d%d%d",&a,&b,&c);

printf("\n Quadratic equation is");

printf("\n %dx^2+%dx+%d=0",a,b,c);

d=pow(b,2)-4*a*c;

printf("\n value of d=%f",d);

if(d==0)

{

printf("Roots are real and equal");

x1=-b/(2*a);

printf("\n Roots are x1=x2=%f",x1);

}

else

{

if(d>0)

{

printf("\n Roots are real and unequal");

x1=(-b+pow(d,0.5))/2*a;

x2=(-b-pow(d,0.5))/2*a;

printf("\n Roots are x1=%f,x2=%f",x1,x2);

}

else

{

printf("\n Roots are not equal");

x1=-b/(2*a);

x2=(pow(-d,0.5))/2*a;

printf("\n Roots are \n x1=%f-%fi",x1,x2);

Page 11: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |9

printf("\n x2=%f+%fi",x1,x2);

}

}

getch();

return 0;

}

Output

Page 12: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |10

Experiment 5

Aim

To write a program in C language for finding sum of series of function Sin(x) up to 5th term.

Flowchart

Page 13: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |11

Program

#include<stdio.h>

#include<math.h>

#include<conio.h>

int main()

{

int fact(int);

int i;

float x,s;

s=0.00;

printf("\n enter the value of x\n");

scanf("%f",&x);

for(i=1;i<=5;i++)

{

s=s+(pow(-1,i+1)*pow(x,(2*i)-1))/fact((2*i)-1);

}

printf("\n sum of series = %f",s);

getch();

return 0;

}

int fact(int m)

{

int f;

f=1;

if(m==1)

return (f);

else

f=m*fact(m-1);

return (f);

}

Page 14: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |12

Output

Page 15: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |13

Experiment 6

Aim

To write a program in C language for finding value of function at given value of argument on the basis of given

data by using Newton’s Divided Difference method.

Flowchart

Page 16: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |14

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int n,i,j;

float x[100],fx[100],xp,fxd[100],fxdj,t,fact,fxp;

printf("\n Enter the number of known points\n");

scanf("%d",&n);

printf("\n Enter the points with their value\n");

for(i=0;i<n;i++)

{

printf("\n Enter the %dst term \n",i+1);

scanf("%f%f",&x[i],&fx[i]);

}

printf("\n Enter the point at which value of function is required\n");

scanf("%f",&xp);

for(i=0;i<n;i++)

{

fxd[i]=fx[i];;

}

fxp=fx[0];

fact=1.0;

for(i=1;i<n;i++)

{

t=fxd[i-1];

for(j=i;j<n;j++)

{

fxdj=(fxd[j]-t)/(x[j]-x[j-i]);

printf("\n%f",fxdj);

t=fxd[j];

fxd[j]=fxdj;

}

fact=fact*(xp-x[i-1]);

fxp=fxp+(fxd[i]*fact);

Page 17: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |15

printf("\n");

}

printf("\n The value of function at %f=%f",xp,fxp);

getch();

}

Output

Page 18: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |16

Experiment 7

Aim

To write a program in C language for finding value of function at given value of argument on the basis of given

data by using Lagrange’s Method.

Flowchart

Page 19: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |17

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int n,i,j;

float x[100],fx[100],xp,N,D,fxp;

printf("\n How many data inputs are to be entered\n");

scanf("%d",&n);

printf("\n Enter the value of\n \tx[i]\tfx[i]");

for(i=0;i<n;i++)

{

printf("\n %d \t",i+1);

scanf("%f%f",&x[i],&fx[i]);

}

fxp=0.0;

printf("\n Enter the point at which value of function is required\n");

scanf("%f",&xp);

for(i=0;i<n;i++)

{

N=D=1.0;

for(j=0;j<n;j++)

{

if(i==j)

continue;

N=N*(xp-x[j]);

D=D*(x[i]-x[j]);

}

fxp=fxp+(N/D)*fx[i];

}

printf("\n The value of function at x=%0.2f is calculated as fx=%0.4f",xp,fxp);

getch();

return 0;

}

Page 20: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |18

Output

Page 21: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |19

Experiment 8

Aim

To write a program in C language for calculating value of integration within given limits and number of intervals

by Trapezoidal Rule.

Flowchart

Page 22: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |20

Program

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

{

int n,i;

float x[100],fx[100],a,b,h,S;

printf("\n Enter the initial & final limit\n");

scanf("%f%f",&a,&b);

printf("\n Enter the number of intervals \n");

scanf("%d",&n);

h=(b-a)/n;

for(i=0;i<=n;i++)

{

x[i]=a+i*h;

fx[i]=sin(x[i]);

printf("\n x=%f\t fx=%f",x[i],fx[i]);

}

S=fx[0]+fx[n];

for(i=0;i<n;i++)

{

S=S+(2.0*fx[i]);

}

S=0.5*h*S;

printf("\n The value of integration=%f",S);

getch();

}

Page 23: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |21

Output

Page 24: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |22

Experiment 9

Aim

To write a program in C language for calculating the value of integration within given limits and number of

intervals by Simpson’s 1/3rd and 3/8th Rule.

Flowchart

Page 25: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |23

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int n,i,t;

float x[100],fx[100],a,b,h,s;

printf("\n Enter the initial & final limit\n");

scanf("%f%f",&a,&b);

printf("\n Enter the number of intervals \n");

scanf("%d",&n);

h=(b-a)/n;

printf("\n\tx[i]\t\tfx[i]");

for(i=0;i<=n;i++)

{

x[i]=a+(i*h);

fx[i]=1/(1+x[i]);

printf("\n %d\t%f\t%f",i,x[i],fx[i]);

}

s=fx[0]+fx[n];

printf("\n Enter 1 to solve by simpson's 1/3rd rule\n Any other key to

solve by simpson's 3/8th rule \n");

scanf("%d",&t);

if(t==1)

{

for(i=1;i<n;i+=2)

{

s=s+4.0*fx[i];

}

for(i=2;i<n;i+=2)

{

s=s+2.0*fx[i];

}

s=(1.0/3.0)*h*s;

printf("\n The value of integration by 1/3 rule=%f",s);

Page 26: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |24

}

else

{

for(i=1;i<n;i+=2)

{

if(i%3==0)

{

s=s+2*fx[i];

}

else

{

s=s+3*fx[i];

}

}

s=(3.0/8.0)*h*s;

printf("\n The value of integration by 3/8 rule=%f",s);

}

getch();

return 0;

}

Output

Page 27: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |25

Experiment 10

Aim

To write a program in C language for solving simultaneous equation in ‘n’ variables by using Gauss Elimination

Method.

Flowchart

Page 28: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |26

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int i,j,k,n;

float a[21][21],b[20],c,x[20],S;

printf("Enter the number of unknowns [n<=20]\n");

scanf("%d",&n);

printf("Enter the coefficient matrix A \n");

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

scanf("%f",&a[i][j]);

}

printf("\n");

}

printf("Enter the matrix B \n");

for(i=1;i<=n;i++)

{

scanf("%f",&b[i]);

printf("\n");

}

for(i=1;i<n;i++)

{

for(j=i+1;j<=n;j++)

{

c=a[j][i];

b[j]=b[j]-(b[i]*(c/a[i][i]));

for(k=1;k<=n;k++)

{

a[j][k]=a[j][k]-(a[i][k]*(c/a[i][i]));

}

}

Page 29: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |27

}

for(i=1;i<=n;i++)

{

a[i][n+1]=b[i];

}

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf("%f\t",a[i][j]);

}

printf(" | %f\n",a[i][n+1]);

}

for(i=0;i<=n;i++)

{

x[i]=0.0;

}

for(i=n;i>0;i--)

{

S=0;

for(j=i+1;j<=n;j++)

{

S=S+(a[i][j]*x[j]);

}

x[i]=a[i][n+1]-S;

x[i]=(x[i]/a[i][i]);

}

printf("The value of the variables are \n");

for(i=1;i<=n;i++)

{

printf("The value of x[%d]=%f\n",i,x[i]);

}

return 0;

}

Page 30: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |28

Output

Page 31: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |29

Experiment 11

Aim

To write a program in C language for solving simultaneous equation in ‘n’ variables by using Gauss Siedal Method.

Flowchart

Page 32: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |30

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int i,j,k,n;

float a[21][21],b[20],x[20],temp,Sum;

printf("Enter the number of unknowns [n<=20]\n");

scanf("%d",&n);

printf("Enter the coefficient matrix A \n");

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

scanf("%f",&a[i][j]);

}

printf("\n");

}

printf("Enter the matrix B \n");

for(i=1;i<=n;i++)

{

scanf("%f",&a[i][n+1]);

printf("\n");

}

printf("Enter the Initial Approximation \n");

for(i=1;i<=n;i++)

{

printf("x[%d] = ",i);

scanf("%f",&x[i]);

printf("\n");

}

for(k=1;k<=5;k++)

{

printf("\n %d Iteration : ",k);

for(i=1;i<=n;i++)

Page 33: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |31

{

Sum=0;

for(j=1;j<=n;j++)

{

if(i!=j)

{

temp=x[j]*a[i][j];

Sum=Sum+temp;

}

}

x[i]=a[i][n+1]-Sum;

x[i]=x[i]/a[i][i];

printf("x[%d] = %0.4f\t",i,x[i]);

}

}

return 0;

}

Output

Page 34: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |32

Experiment 12

Aim

To write a program in C language for solving given differential equation (dy/dx = x+y) in 5 iterations and initial

approximation by Runge-Kutta Method.

Flowchart

Page 35: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |33

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int n=0;

float k1,k2,k3,k4,x,y,S,h;

float f(float x,float y);

printf("Enter the initial approximation of x and y \n");

scanf("%f %f",&x,&y);

printf("\n");

h=0.2;

do

{

S=0;

k1=h*f(x,y);

k2=h*f(x+(h/2),y+(k1/2));

k3=h*f(x+(h/2),y+(k2/2));

k4=h*f(x+h,y+k3);

S=k1+(2*k2)+(2*k3)+k4;

S=S/6;

printf("k1=%f\t k2=%f\t k3=%f\t k4=%f\n",k1,k2,k3,k4);

x=x+h;

y=y+S;

printf("x=%f \t y=%f \n\n",x,y);

n++;

}while(n<5);

printf("\n The approximate solution of the equation dy/dx=x+y is %f \n",y);

return 0;

}

float f(float x,float y)

{

return(x+y);

}

Page 36: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |34

Output

Page 37: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |35

Experiment13

Aim

To write a program in C language for solving given differential equation (dy/dx=-0.01y) and find the amount of

radioactive material left given initial amount and final time by Modified Euler Method.

Flowchart

Page 38: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |36

Program

#include<stdio.h>

#include<conio.h>

int main()

{

int n,k;

float x,y,temp,f1,f2,S,h;

float f(float x,float y);

printf("Enter initial amount of radioactive substance and final value of

time(Sec) \n");

scanf("%f %f",&y,&x);

printf("\n");

h=25;

k=x/h;

x=0;

n=0;

printf("time=%f \t Radioactive substance=%f \n",x,y);

do

{

S=0;

f1=f(x,y);

temp=y+(h*f1);

f2=f(x+h,temp);

S=f1+f2;

S=S*h/2;

y=y+S;

x=x+h;

printf("time=%0.2f \t Radioactive substance=%f \n",x,y);

n++;

}while(n<k);

printf("\n The final amount of radioactive material after time %0.2f sec is

%f \n",x,y);

return 0;

}

Page 39: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |37

float f(float x,float y)

{

return(-0.01*y);

}

Output

Page 40: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |38

Experiment 14

Aim

To write a program in C language for solving a linear or transcendental equation by Newton-Raphson Method.

Flowchart

Page 41: Numerical analysis

V i s h a l S i n g h ( 0 0 3 2 0 9 0 3 6 1 3 ) |39

Program

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

{

int i;

float x1,x2,f1,f2;

printf("Enter the intitial approxiamtion\n");

scanf("%f",&x1);

x2=x1;

for(i=0;;i++)

{

x1=x2;

f1=pow(x1,2)-9;

f2=(2*x1);

x2=x1-(f1/f2);

if(abs(x2-x1)<0.001)

break;

}

printf("The Solution for the given equation is %f",x2);

return 0;

}

Output