nmc lab set



SET 1



Q. Write a C program to solve algebraic equation x4+2x3-x-1=0 between 0 and 1 by using Method of Bisection.


Solution:
#include<stdio.h>

#include<math.h>
float f(float);
main()
{
float x,a,b,err=0.00001;
printf("\nEnter values of a and b=");
scanf("%f%f",&a,&b);
if(f(a)*f(b)>0)
printf("\nRoot does not lies between %f and %f",a,b);
else
{
while(fabs(b-a)>=err)
{
x=(a+b)/2;
if(f(x)==0)
{
break;
}
else if(f(a)*f(x)<0)
b=x;
else
a=x;
}
printf("\nRoot of the equation=%f",x);
}
}

float f(float x)
{
return(x*x*x*x+2*x*x*x-x-1);
}

OUTPUT

Enter values of a and b=0 1
Root of the equation=0.866768


SET 2



Q. Write a C program to solve algebraic equation x3-5x-7=0 between 2 and 3 by using Method of False position.

Solution:
#include<stdio.h>
#include<math.h>
float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));


if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x-5*x-7);

}

OUTPUT:
Enter the value of x0: 2

Enter the value of x1:
3

App.root = 2.747346

SET 3



Q. Write a C program to solve algebraic equation 3x3-9x2+8=0 by using Newton Raphson Method.

Solution:
#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(3*x*x*x-9*x*x+8);

}



float df(float x)

{

return(9*x*x-18*x);

}

OUTPUT:
Enter initial value=1


The approximate value is 1.226074
SET 4



Q. Write a C program to solve algebraic equation x3-4x-9=0 between 2 and 3 by using Method of Bisection.

Solution:
#include<stdio.h>

#include<math.h>



float f(float);

main()

{

float x,a,b,err=0.00001;

printf("\nEnter values of a and b=");

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

if(f(a)*f(b)>0)

printf("\nRoot does not lies between %f and %f",a,b);

else

{

while(fabs(b-a)>=err)

{

x=(a+b)/2;

printf("\na=%f, f(a)=%f, b=%f, f(b)=%f, x=%f, f(x)=%f",a,f(a),b,f(b),x,f(x));

if(f(x)==0)

{

break;

}

else if(f(a)*f(x)<0)

b=x;

else

a=x;

}

printf("\nRoot of the equation=%f",x);

}


}



float f(float x)

{

return(x*x*x-4*x-9);

}

OUTPUT

Enter values of a and b=2 3


Root of the equation=2.706535


SET 5



Q. Write a C program to solve algebraic equation x3-2x-5=0 between 1 and 3 by using Method of False position.

Solution:
#include<stdio.h>

#include<math.h>


float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));

if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x-2*x-5);

}

OUTPUT:
Enter the value of x0: 1


Enter the value of x1: 3


App.root = 2.094551
SET 6



Q. Write a C program to solve algebraic equation x3+3x-1=0 by using Newton Raphson Method.

Solution:

#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(x*x*x+3*x-1);

}



float df(float x)

{

return(3*x*x+3);

}
OUTPUT:
Enter initial value=1


The approximate value is 0.322185
SET 7



Q. Write a C program to solve algebraic equation x3=8 between 2 and 3 by using Method of Bisection.


Solution:
#include<stdio.h>

#include<math.h>



float f(float);

main()

{

float x,a,b,err=0.00001;

printf("\nEnter values of a and b=");

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

if(f(a)*f(b)>0)

printf("\nRoot does not lies between %f and %f",a,b);

else

{

while(fabs(b-a)>=err)

{

x=(a+b)/2;

printf("\na=%f, f(a)=%f, b=%f, f(b)=%f, x=%f, f(x)=%f",a,f(a),b,f(b),x,f(x));

if(f(x)==0)

{

break;

}

else if(f(a)*f(x)<0)

b=x;

else

a=x;

}

printf("\nRoot of the equation=%f",x);

}


}



float f(float x)

{

return(x*x*x-8);

}

OUTPUT

Enter values of a and b=2 3

Root of the equation=2.999992


SET 8



Q. Write a C program to solve algebraic equation x4-x-1=0 between 1 and 2 by using Method of False position.

Solution:
#include<stdio.h>

#include<math.h>


float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));

if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x*x-x-1);

}

OUTPUT:
Enter the value of x0: 1


Enter the value of x1: 2

App.root = 1.220743
SET 9



Q. Write a C program to solve algebraic equation x2+2x-2=0 by using Newton Raphson Method.

Solution:
#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(x*x+2*x-2);

}



float df(float x)

{

return(2*x+2);

}
OUTPUT:
Enter initial value=1


The approximate value is 0.732051
SET 10



Q. Write a C program to solve algebraic equation x2+2x-2=0 between 0 and 1 by using Method of Bisection.


Solution:
#include<stdio.h>

#include<math.h>



float f(float);

main()

{

float x,a,b,err=0.00001;

printf("\nEnter values of a and b=");

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

if(f(a)*f(b)>0)

printf("\nRoot does not lies between %f and %f",a,b);

else

{

while(fabs(b-a)>=err)

{

x=(a+b)/2;

printf("\na=%f, f(a)=%f, b=%f, f(b)=%f, x=%f, f(x)=%f",a,f(a),b,f(b),x,f(x));

if(f(x)==0)

{

break;

}

else if(f(a)*f(x)<0)

b=x;

else

a=x;

}

printf("\nRoot of the equation=%f",x);

}


}



float f(float x)

{

return(x*x+2*x-2);

}

OUTPUT

Enter values of a and b=0 1

Root of the equation=0.732048


SET 11



Q. Write a C program to solve algebraic equation x3-2x2-5=0 between 1 and 3 by using Method of False position.

Solution:
#include<stdio.h>

#include<math.h>


float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));

if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x-2*x*x-5);

}

OUTPUT:
Enter the value of x0: 1


Enter the value of x1: 3


App.root = 2.690647
SET 12



Q. Write a C program to solve algebraic equation 3x3+5x-40=0 by using Newton Raphson Method.

Solution:
#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(3*x*x*x+5*x-40);

}



float df(float x)

{

return(9*x*x+5);

}
OUTPUT:
Enter initial value=1


The approximate value is 2.1378125

1 comment: