Beruflich Dokumente
Kultur Dokumente
= "<<PE<<"%";
getch();
}
3
OUTPUT
= "<<PE<<"%";
getch();
}
OUTPUT
else
{a=xnew; xold=xnew;}
count++;
}
while (error>et);
cout<<"\n\n\n The approximate root containing "<<et<<" % error is = "<<xnew;
getch();
}
Computer Output
OUTPUT
10
PRACTICAL# 5 : Regula Falsi Method
#include<constream.h>
#include<math.h>
#include<stdio.h>
#define f(x)(exp(x)-2)
void main()
{
clrscr();
float a,b,xn,x0=a,et,error;
int count=1;
cout<<"enter the value of a, b and error tolerance value in % = ";
cin>>a>>b>>et;
cout<<"\n**************Regula Falsi Method****************\n";
cout<<"\n..................................................................\n\n";
cout<<"\nit#"<<setw(10)<<"a"<<setw(15)<<"b"<<setw(17)<<"Root"<<s
etw(20)<<"error in %\n";
do
{
xn=(a*f(b)-b*f(a))/(f(b)-f(a));
error=fabs((xn-x0)/xn)*100;
cout<<endl<<count;
printf ("\t%.6f\t%.6f\t%.6f\t%.6f",a,b,xn,error);
if (f(a)*f(xn)<0)
{
b=xn; x0=xn;
}
11
else
{
a=xn; x0=xn;
}
count++;
}
while (error>et);
cout<<"\n\n\n the approximate root containing "<<et<<" %error is =
"<<xn;
getch();
}
Computer Output
12
13
}
while (error>et);
cout<<"\n\n................................................\n\n";
printf ("\t%.8f",xn);
getch();
}
Computer Output
14
15
if (i!=j)
p=p*(xn-x[j])/(x[i]-x[j]);
}
L[i]=p;
}
for (i=0;i<n;i++)
sum=sum+y[i]*L[i];
cout<<"\n\n\n************** Lagrange's Interpolation
Formula**************\n\n\n";
cout<<"\n\n x: ";
for (i=0;i<n;i++)
{
printf ("%.4f",x[i]);
cout<<"\t\t";
}
cout<<"\n\n y: ";
for (i=0;i<n;i++)
{
printf ("%.4f",y[i]);
cout<<"\t\t";
}
cout<<"\n\n\n The required value at x = ";
printf ("%.4f",xn);
cout<<" is = ";
printf ("%.4f",sum);
16
getch();
}
Computer Output
17
void main()
{
clrscr();
float x[5],y[5],d1y[4],d2y[3],d3y[2],d4y,X,Y;
cout<<"\n enter x values of the data: ";
for(int i=0;i<=4;i++)
cin>>x[i];
cout<<"\n enter y values of the data: ";
for(i=0;i<=4;i++)
cin>>y[i];
cout<<"\n enter value of x at which y is required: ";
cin>>X;
for(i=0;i<=3;i++)
d1y[i]=(y[i+1]-y[i])/(x[i+1]-x[i]);
for(i=0;i<=2;i++)
d2y[i]=(d1y[i+1]-d1y[i])/(x[i+2]-x[i]);
for(i=0;i<=1;i++)
d3y[i]=(d2y[i+1]-d2y[i])/(x[i+3]-x[i]);
d4y=(d3y[1]-d3y[0])/(x[4]-x[0]);
Y=y[0]+(X-x[0])*d1y[0]+(X-x[0])*(X-x[1])*d2y[0]+(X-x[0])*(X-x[1])*(Xx[2])*d3y[0]+
(X-x[0])*(X-x[1])*(X-x[2])*(X-x[3])*d4y;
cout<<"\n Value of y at x = "<<X<<" is "<<Y;
getch();
}
18
Computer Output
19
{
clrscr();
float x[20],f[20],h,sum1=0.0,sum2=0.0,I;
int n,i;
cout<<"\nEnter the total no. of strips : ";
cin>>n;
cout<<"\nEnter the lower and upper limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
{
x[i]=x[0]+i*h;
cout<<"\nEnter the value of f(x) at "<<x[i]<<" : ";
cin>>f[i];
}
for(i=1;i<=n-1;i+=2)
sum1+=f[i];
for(i=2;i<=n-2;i+=2)
sum2+=f[i];
I=(h/3.0)*(f[0]+4.0*sum1+2.0*sum2+f[n]) ;
cout<<"\n\n\tx\t\ty\n\n";
cout<<"=====================================";
for(i=0;i<=n;i++)
cout<<"\n\t"<<x[i]<<"\t\t"<<f[i];
cout<<"\n\nUsing Simpson's 1/3rd rule, the required result is = "<<I;
getch();
20
Computer Output
21
clrscr();
float x[20],f[20],h,sum1=0.0,sum2=0.0,I;
int n,i,j;
cout<<"\nEnter the total no. of strips : ";
cin>>n;
cout<<"\nEnter the lower and upper limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
{
x[i]=x[0]+i*h;
cout<<"\nEnter the value of f(x) at "<<x[i]<<" : ";
cin>>f[i];
}
j=3.0;
for(i=1;i<=n-1;i++)
{
if(i==j)
{
sum2+=f[i];
j=j+3;
}
else
sum1+=f[i];
}
I=((3.0*h)/8.0)*(f[0]+3.0*sum1+2.0*sum2+f[n]);
22
cout<<"\n\tx\t\ty\n";
for(i=0;i<=n;i++)
cout<<"\n\t"<<x[i]<<setw(18)<<f[i];
cout<<"\n\n\nResult = "<<I;
getch();
}
Computer Output:
23
clrscr();
float x[20],h,sum=0.0,f[20],I;
int n,i;
cout<<"\nEnter the total no. of strips : ";
cin>>n;
cout<<"\nEnter the lower and upper limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
{ x[i]=x[0]+i*h;
cout<<"\nEnter the value of f(x) at "<<x[i]<<" : ";
cin>>f[i];
}
for(i=1;i<n;i++)
sum+=f[i];
I=(h/2.0)*(f[0]+2*sum+f[n]) ;
cout<<"\n\n\tx\t\ty\n\n";
for(i=0;i<=n;i++)
cout<<"\n\t"<<x[i]<<"\t\t"<<f[i];
cout<<"\n\n\nUsing Trapezoidal Rule, the required result is = "<<I;
getch();
}
Computer Output
24
25
float a11,a12,a13,b1,a21,a22,a23,b2,a31,a32,a33,b3,errx,erry,errz,et;
float xnew,ynew,znew,error,xold=0.0,yold=0.0,zold=0.0;
int iteration=1;
cout<<"\n Enter the coeff: of 1st equation = ";
cin>>a11>>a12>>a13>>b1;
cout<<"\n Enter the coeff: of 2nd equation = ";
cin>>a21>>a22>>a23>>b2;
cout<<"\n Enter the coeff: of 3rd equation = ";
cin>>a31>>a32>>a33>>b3;
cout<<"\n\n enter the error tolerance = ";
cin>>et;
do
{
xnew=(b1-a12*yold-a12*zold)/(a11);
ynew=(b2-a21*xold-a23*zold)/(a22);
znew=(b3-a31*xold-a32*yold)/(a33);
errx=fabs((xnew-xold)/xnew)*100;
erry=fabs((ynew-yold)/ynew)*100;
errz=fabs((znew-zold)/znew)*100;
error=errx+erry+errz;
xold=xnew;yold=ynew;zold=znew;
iteration++;
}
while (error>et);
cout<<"\n\n x = "<<xnew;
cout<<"\n\n y = "<<ynew;
26
cout<<"\n\n z = "<<znew;
cout<<"\n\n error = "<<error;
getch();
}
COMPUTER OUTPUT
27
float a11,a12,a13,b1,a21,a22,a23,b2,a31,a32,a33,b3,errx,erry,errz,et;
float xnew,ynew,znew,error,xold=0.0,yold=0.0,zold=0.0;
int iteration=1;
cout<<"\n Enter the coeff: of 1st equation = ";
cin>>a11>>a12>>a13>>b1;
cout<<"\n Enter the coeff: of 2nd equation = ";
cin>>a21>>a22>>a23>>b2;
cout<<"\n Enter the coeff: of 3rd equation = ";
cin>>a31>>a32>>a33>>b3;
cout<<"\n\n enter the error tolerance = ";
cin>>et;
do
{
xnew=(b1-a12*yold-a12*zold)/(a11);
errx=fabs((xnew-xold)/xnew)*100;
xold=xnew;
ynew=(b2-a21*xold-a23*zold)/(a22);
erry=fabs((ynew-yold)/ynew)*100;
yold=ynew;
znew=(b3-a31*xold-a32*yold)/(a33);
errz=fabs((znew-zold)/znew)*100;
zold=znew;
error=errx+erry+errz;
iteration++;
}
28
while (error>et);
cout<<"\n\n x = "<<xnew;
cout<<"\n\n y = "<<ynew;
cout<<"\n\n z = "<<znew;
cout<<"\n\n error = "<<error;
getch();
}
Computer Output
29
int i,j,n,count=1;
cout<<"enter order of the given matrix = ";
cin>>n;
cout<<"\n enter initial assumed eigenvector = ";
for (i=1;i<=n;i++)
cin>>x0[i];
cout<<"\nenter the entries of the given matrix = ";
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
cin>>a[i][j];
cout<<"\n enter the error tolerance value in % = ";
cin>>et;
cout<<"\n\n **************************Power
Method**********************************\n";
cout<<"\n============================================
================================\n";
cout<<"\nIt#\tEValue\t\t\tEVector\t\t\tError in %"<<endl;
cout<<"\n============================================
=================================\n";
xy:
for (i=1;i<=n;i++)
{
float sum=0.0;
for (j=1;j<=n;j++)
sum+=a[i][j]*x0[j];
v[i]=sum;
}
30
for (i=1;i<=n;i++)
vn[i]=fabs(v[i]);
big=vn[1];
for(i=2;i<=n;i++)
if (vn[i]>big)
big=vn[i];
cout<<"\n"<<count<<"
"<<big;
for (i=1;i<=n;i++)
x0[i]=(v[i]/big);
error=fabs((big-ibig)/big)*100;
for(i=1;i<=n;i++)
cout<<"
"<<x0[i];
cout<<"
"<<error;
if (error>et)
{
ibig=big;
count++;
goto xy;
}
cout<<"\n\n\n Dominant eigenvalue is = "<<big;
cout<<"\n\n\n Dominant eigenvector is = "<<x0[1]<<"
"<<x0[3];
"<<x0[2]<<"
31
OUTPUT
32
33
{
ibig=big;
goto ab;
}
getch();
}
Computer Output
34
35
36
OUTPUT
37
38
Computer Output
39