Sie sind auf Seite 1von 39

Linear Algebra and Numerical Methods

Madam Sania Qureshi

Linear Algebra and Numerical Methods

Madam Sania Qureshi

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 1 : Program to find errors in functions


#include<constream.h>
#include<math.h>
#define f(x) sin(x)
#define df(x) cos(x)
void main()
{
clrscr();
float x,z,e,AE,RE,PE;
cout<<"\n\n Enter the value of x = ";
cin>>x;
z=f(x);
e=0.5*pow(10,-4);
RE=fabs(e*f(x)/df(x));
AE=RE*z;
PE=RE*100;
cout<<"\n\n The value of z is = "<<z;
cout<<"\n\n The Absolute error is = "<<AE;
cout<<"\n\n The Relative error is ="<<RE;
cout<<"\n\n The Percentage error is

= "<<PE<<"%";

getch();
}

3
OUTPUT

Linear Algebra and Numerical Methods

Madam Sania Qureshi

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 2 :Program to find errors in powers and


roots
#include <constream.h>
#include<math.h>
void main()
{
clrscr();
float x1,x2,x3,e1,e2,e3,z,AE,RE,PE;
cout<<"Enter the values of x1, x2 and x3 = ";
cin>>x1>>x2>>x3;
z=sqrt(x1*x2/x3);
e1=0.5*pow(10,-3);e2=0.5*pow(10,-2);e3=0.5*pow(10,-3);
RE=(0.5)*(e1/x1+e2/x2+e3/x3);
AE=RE*z;
PE=RE*100;
cout<<"\n\n The value of z is = "<<z;
cout<<"\n\n The Absolute error is = "<<AE;
cout<<"\n\n The Relative error is ="<<RE;
cout<<"\n\n The Percentage error is

= "<<PE<<"%";

getch();
}

Linear Algebra and Numerical Methods

Madam Sania Qureshi

OUTPUT

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 3 : To find root of f(x) using Bisection


Method
#include<constream.h>
#include<math.h>
#include<stdio.h>
#define f(x)(pow(x,3)-15*pow(x,2)+300)
void main()
{
clrscr();
float a,b,xnew,xold=a,et,error;
cout<<"\n enter the value of a, b, and error tolerance value in % = ";
cin>>a>>b>>et;
int count=1;
cout<<"\n*************Bisection Method**************\n";
cout<<"\n..........................................................\n\n";
cout<<"\nit#"<<setw(10)<<"a"<<setw(15)<<"b"<<setw(17)<<"Root"<<setw(21
)<<"error in %\n";
do
{
xnew=(a+b)/2;
error=fabs((xnew-xold)/xnew)*100;
cout<<endl<<count;
printf ("\t%.6f\t%.6f\t%.6f\t%.6f",a,b,xnew,error);
cout<<"\n....................................................................\n";
if (f(a)*f(xnew)<0)
{b=xnew; xold=xnew;}

Linear Algebra and Numerical Methods

Madam Sania Qureshi

else
{a=xnew; xold=xnew;}
count++;
}
while (error>et);
cout<<"\n\n\n The approximate root containing "<<et<<" % error is = "<<xnew;
getch();
}

Computer Output

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 4 :FIXED POINT ITERATION


#include <constream.h>
#include<math.h>
#include<stdio.h>
#define f(x) (x*x*x+x-1)
#define g(x) (1/(x*x+1))
void main ()
{
clrscr();
float x0,xn,err,et;
int count=1;
cout<<"\n what is x0 and et in % ? ";cin>>x0>>et;
do
{
xn=g(x0);
err=fabs((xn-x0)/xn)*100;
cout<<endl<<count;
printf("\t\t%.5f\t\t%.5f",xn,err);
cout<<"\n................................................................\n";
x0=xn;
count++;
}
while (err>et);
cout<<"\n\n root is = "<<xn<<" containing "<<et<<" % error";
getch();}

Linear Algebra and Numerical Methods

Madam Sania Qureshi

OUTPUT

10
PRACTICAL# 5 : Regula Falsi Method

Linear Algebra and Numerical Methods

Madam Sania Qureshi

#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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 6 : To find the root of y=f(x) using Newton


Raphson Method
#include<constream.h>
#include<math.h>
#include<stdio.h>
#define f(x) (sin(x)-x+1)
#define df(x) (cos(x)-1)
void main()
{
clrscr();
float x0,et,error,xn;
int count=1;
cout<<"\n enter initial guess and error tolerance value in % = ";
cin>>x0>>et;
cout<<"\n*************NEWTON RAPHSON METHOD**********\n\n";
cout<<"\nit#"<<setw(15)<<"Root"<<setw(15)<<"Error in %\n";
cout<<"\n\n................................................\n\n";
do
{
xn=x0-f(x0)/df(x0);
error=fabs((xn-x0)/xn)*100;
cout<<endl<<count;
printf ("\t%.8f\t%.8f",xn,error);
x0=xn;
count++;

13

Linear Algebra and Numerical Methods

Madam Sania Qureshi

}
while (error>et);
cout<<"\n\n................................................\n\n";
printf ("\t%.8f",xn);
getch();
}

Computer Output

14

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 7 :To find value of a Polynomial y=f(x) using


Lagrange's Interpolation Formula
#include<constream.h>
#include<math.h>
#include<stdio.h>
void main()
{
clrscr();
float x[10],y[10],L[10],xn,p,sum=0;
int i,j,n;
cout<<"enter the number of data values = ";
cin>>n;
cout<<"\n enter the x values of the data = ";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\n enter the y values of the data = ";
for (i=0;i<n;i++)
cin>>y[i];
cout<<"\n the value of y at x = ";
cin>>xn;
for (i=0;i<n;i++)
{
p=1;
for (j=0;j<n;j++)
{

15

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

getch();
}

Computer Output

PRACTICAL# 8 : NEWTON'S DIVIDED DIFFERENCE


INTERPOLATION FORMULA
#include<constream.h>

17

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

Computer Output

PRACTICAL# 9 : Program for Simpson's 1/3RD Rule


#include<constream.h>
#include<math.h>
void main()

19

Linear Algebra and Numerical Methods

Madam Sania Qureshi

{
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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

Computer Output

PRACTICAL# 10 : Program for Simpson's 3/8th Rule


#include<constream.h>
#include<math.h>
void main()
{

21

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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:

PRACTICAL# 11 : Program of Trapezoidal Rule to perform


numerical integration
#include<constream.h>
#include<math.h>
void main()
{

23

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 12: Program for Gauss Jacobi's Method


#include<constream.h>
#include<math.h>
void main()
{
clrscr();

25

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

cout<<"\n\n z = "<<znew;
cout<<"\n\n error = "<<error;
getch();
}

COMPUTER OUTPUT

PRACTICAL# 13: Program for Gauss Seidal's Method


#include<constream.h>
#include<math.h>
void main()
{
clrscr();

27

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

PRACTICAL# 14: "Power Method to find Dominant eigenvalue


and Dominant eigenvector"
#include<constream.h>
#include <math.h>
void main()
{
clrscr();
float a[10][10],x0[10],v[10],vn[10],ibig=0.0,big,error,et;

29

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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

Linear Algebra and Numerical Methods

Madam Sania Qureshi

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]<<"

cout<<"\n\n\n Absolute relative approx. error is = "<<error;


getch();
}

31

Linear Algebra and Numerical Methods

Madam Sania Qureshi

OUTPUT

PRACTICAL# 15 : Program of Power Method to find Dominant


EigenValue and corresponding Eigen vector
#include<constream.h>
#include <math.h>
void main()
{
clrscr();
float a[10][10],b[10],c[10],d[10],ibig=0.0,big;
int i,j,n;

32

Linear Algebra and Numerical Methods

Madam Sania Qureshi

cout<<"enter order matrix";


cin>>n;
for (i=1;i<=n;i++)
b[i]=1.0;
cout<<"enter the values of matrix";
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
cin>>a[i][j];
ab:
for (i=1;i<=n;i++)
{
c[i]=0.0;
for (j=1;j<=n;j++)
c[i]+=a[i][j]*b[j];
}
for (i=1;i<=n;i++)
d[i]=fabs(c[i]);
big=d[1];
for(i=2;i<=n;i++)
if (d[i]>big)
big=d[i];
cout<<"largest value ="<<big<<endl;
for (i=1;i<=n;i++)
b[i]=(c[i]/big);
if (fabs(ibig-big)>0.001)

33

Linear Algebra and Numerical Methods

Madam Sania Qureshi

{
ibig=big;
goto ab;
}
getch();
}

Computer Output

34

Linear Algebra and Numerical Methods

Madam Sania Qureshi

35

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 16: Program for Euler's Method to solve


First Order ODE
#include<constream.h>
#include<math.h>
# define f(x,y) (1-y)
void main()
{
clrscr();
float x0,y0,xn,yn,h;
cout<<"\n enter the initial condition: x0 and y0 = ";
cin>>x0>>y0;
cout<<"\n\n enter the value of x at which the result is required: xn = ";
cin>>xn;
cout<<"\n enter the step size: h = ";
cin>>h;
while(x0<xn)
{
yn=y0+h*f(x0,y0);
x0=x0+h;
cout<<"\n X = "<<x0<<"\t Y = "<<yn;
y0=yn;
}
cout<<"\n\n The value of y at given x is = "<<yn;
getch();
}

36

Linear Algebra and Numerical Methods

Madam Sania Qureshi

OUTPUT

37

Linear Algebra and Numerical Methods

Madam Sania Qureshi

PRACTICAL# 17: Program to solve ordinary diff: equation by


Taylor's Series Method
#include<constream.h>
#include<math.h>
#define f(x,y)(x*x*y-1)
#define df(x,y)(2*x*y+x*x*x*x*y-x*x)
void main()
{
clrscr();
float x0,y0,xn,yn,h;
cout<<"\n kindly enter x0,y0,h,xn = ";
cin>>x0>>y0>>h>>xn;
while (x0<xn)
{
yn=y0+h*f(x0,y0)+(h*h/2.0)*df(x0,y0);
x0=x0+h;
cout<<"\n\n x = "<<x0<<"\t Y = "<<yn;
y0=yn;
}
getch();}

38

Linear Algebra and Numerical Methods

Madam Sania Qureshi

Computer Output

39

Das könnte Ihnen auch gefallen