PRACTICAL FILE
Java Lab
Submitted to: Submitted by:
Mr Gurudatt Gupta Name:Bhumika Piplani
Course: BTech(CSE)
Semester: 4th semester
Section: B
DELHI TECHNICAL CAMPUS
KP-III, Greater Noida UP-201306
(Aff. to GGSIPU, Delhi)
INDEX
S.NO. PROGRAM NAME DATE SIGN.
1 Program for finding roots of f(x)=0 by Bisection method.
2 Program for finding roots of f(x)=0 by Newton Raphson
method.
3 Program for finding roots of f(x)=0 by Secant method.
4 Program to deduce error involved in polynomial
equation.
5 Program for solving numerical integration by Trapezoidal
rule.
6 To implement Langrange’s Interpolation formula.
7 Program for solving numerical integration by Simpson’s 1/3
rule
8 To implement Numerical Integration Simpson 3/8 rule.
9 To implement Newton’s Divided Difference formula.
10 Find the Eigen values using Power method.
11 Inverse of a system of linear equations using Gauss-Jordan
method.
PROGRAM-10
Aim: To find the largest Eigen values using Power method.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#defineMaxOrder50
#define MAX(a,b) a> b ?a : b
/* --------------------------------------------------------*/
/*Main program for algorithm 11.1*/
voidmain(void)
{
int i, j; /* Loop counter */ int
N; /* Order of Matrix */
double Epsilon = 1E-7; /* Tolerance */
double Max = 100; /* Maximum numberof iterations*/ double
X[MaxOrder], Y[MaxOrder];
double A[MaxOrder][MaxOrder]; /* Matrix */
double C1, DC,DV,Lambda = 0;
int Count= 0,Iterating = 1; double Sum,
Err = 1;
double MaxElement;
printf("--------------PowerMethod---------------\n"); printf("--------- Example 11.5
on page550 -------------\n"); printf("-----------------------------------------------\n");
printf("Please enterorder of MatrixA ( < %d !)\n", MaxOrder + 1);
scanf("%d",&N);if(
N>MaxOrder ) {
printf("Number ofsteps must beless than %d\n",MaxOrder+1);
printf("Terminating.Sorry\n");
exit(0); }
printf("Please enterelementsof matrixArow by row:\n");
for (i = 0;i<N; i++) {
for (j = 0;j <N; j++) {
printf("Enter Element No.%d ofrow%d\n", j+1, i+1);
scanf("%lf",&A[i][j]);
printf("You entered A[%d][%d]= %lf\n",i+1, j+1, A[i][j]);
printf("------------------------------------------------
\n");
}}
printf("\n");
/* Initializevector X*/
for (i = 0;i<N; i++)X[i] = 1.0;
while( (Count<= Max) && (Iterating== 1)) {
/* Perform Matrix-Vector multiplication */
for (i = 0;i<N; i++) {
Y[i]= 0;
for (j = 0;j <N; j++) Y[i] +=A[i][j]* X[j]; }
/* Find largest element of vector Y */
/* Dowhatfunction MaxElement(X,N)in the book does */
MaxElement = 0;
for (j = 0;j <N; j++) {
if( fabs(Y[j]) >fabs(MaxElement) )MaxElement =Y[j]; }
C1 = MaxElement;
DC = fabs(Lambda -C1);
for (i = 0;i<N; i++) Y[i] *=1.0/C1;
/* Dowhatfunction DIST(X,Y,N)in the book does */
Sum =0;
for (i = 0;i<N; i++) Sum += pow( (Y[i] - X[i] ),2.0); DV= sqrt(Sum);
Err =MAX(DC,DV);
/* Updatevector Xand scalar Lambda*/
for (i = 0;i<N; i++) X[i] = Y[i]; Lambda = C1;
Iterating = 0;
if( Err>Epsilon)Iterating = 1;
Count++;
} /* End ofwhile loop */
/* Outputvector Xand scalar Lambda*/
printf("---------------------------------------\n");
for (j = 0;j <N; j++) printf("X[%d] =%lf\n",j, X[j]);
printf("---------------------------------------\n"); printf("Lambda =
%lf\n", Lambda);
}/* End of main program*/
PROGRAM-11
Aim: To solve the System of Linear Equations Using Gauss - Jordan Method.
Source Code:
#include<iostream.h>
#include<conio.h>
#include<math.h> void
main()
{float a[6][6],b[6],x[6],t,s; int
i,j,n,k;
clrscr();
cout<<"Enter the maximumno. of matrix"<<endl;
cin>>n;
cout<<"Enter th elements of matrix"<<endl;
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{
cin>>a[i][j]; }
}
cout<<"enter the right constant"<<endl;
for(i=0;i<n;i++)
{ cin>>a[i][n];
}
for(k=0;k<n;k++)
{for(i=0;i<n;i++)
if(i!=k)
{ for(j=k+1;j<n+1;j++)
{
a[i][j]=a[i][j]-(a[i][k]/a[k][k])*a[k][j]);
}}}
cout<<"the solutionis"<<endl;
for(i=0;i<n;i++)
{ x[i]=(a[i][n]/a[i][i]);
cout<<"x["<<i<<"]="<<x[i]<<endl; }
getch(); }
8. ALGORITHM OF SIMPSON’S 1/3rdRULE
Step-1. Start of the
program. Step-2. Input
Lower limit a Step-3. Input
Upper limit b
Step-4. Input number of sub intervals n
Step-5. h=(b–a)/n
Step-6. sum=0
Step-7. sum=fun(a)+4*fun(a+h)+fun(b)
Step-8. for i=3; i<n; i + =2
Step-9. sum + = 2*fun(a+(i – 1)*h) +4*fun(a+i*h)
Step-10. End of loop i
Step-11. result=sum*h/3
Step-12. Print Output result
Step-13. End of Program
Step-14. Start of Section fun
Step-15. temp = 1/(1+(x*x))
Step-16. Return temp
Step-17. End of Section fun
PROGRAM-8
Aim : To implement Numerical Integration Simpson 3/8 rule.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
float fun(float);
void main()
{
float result=1;
float a,b;
float sum,h;
int i,j,n;
clrscr();
printf("\n Enter the range - ");
printf("\n Lower Limit a - ");
scanf("%f",&a)
;printf("\n Upper limit b -
"); scanf("%f",&b);
printf("\n\n Enter number of subintervals - ");
scanf("%d",&n);
h=(b-a)/n;
sum=0;
sum=fun(a)+4*fun(a+h)+fun(b);
for(i=3;i<n;i+=2)
{
sum+=2*fun(a+(i-1)*h)+4*fun(a+i*h);
}
result=sum*h/3;
printf("\n\nValue of integral is %6.4f\t",result);
getch();}
float fun(float x)
{
float temp;
temp=1/(1+(x*x));
return temp;
}
Algorithm of LAGRANGE’S INTERPOLATION FORMULA.
Step-1. Start of the program
Step-2. Input number of terms
n Step-3. Input the arrayax
Step-4. Input the array
ay Step-5. for i=0; i<n; i++
Step-6. nr=1
Step-7. dr=1
Step-8. for j=0; j<n;j++
Step-9. if j!=i
a. nr=nr*(x-ax[
j]) Step-10.
b.dr*(ax[i]-ax[j]) Step-11.
End Loopj
Step-12. y+=(nr/dr)∗ay[i]
Step-13. End Loop i
Step-14. Print Output x, y
Step-15. End ofProgram