Problem: ___ Date: ___ / ___ / 2018
1. WAP to find the Absolute Error, Relative Error
and Percentage Error.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float abs, rel, perc, true, appr;
clrscr();
printf("Enter True value: ");
scanf("%f",&true);
printf("Enter Approximation: ");
scanf("%f",&appr);
abs = fabs(true-appr);
rel = abs/true;
perc=rel*100;
printf("\nAbsolute Error: %f",abs);
printf("\nRelative Error: %f",rel);
printf("\nPercentage Error: %f",perc);
getch();
return 0;
}
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
Output:-
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
2. WAP to find the approximate root for the equation
x3-4x-9=0 up to 2 decimal places using Bisection
method.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
float findroot(float x)
{
return(pow(x,3)-4*x-9);
}
int main()
{
float x1, y1, x2, y2;
float root, f1, f2, f3;
int iter, i;
clrscr();
printf("Enter 1st Approximation value: ");
scanf("%f",&x1);
printf("Enter 2nd Approximation Value: ");
scanf("%f",&y1);
printf("Enter no. of Iterations: ");
scanf("%d",&iter);
x2 = x1;
y2 = y1;
if(findroot(x2)==0)
root = x2;
else if (findroot(y2)==0)
root = y2;
else
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
{
for(i=0;i<iter;i++)
{
f1 = findroot(x2);
root = (x2+y2)/2.0;
f2 = findroot(root);
f3 = findroot(y2);
if(f2==0)
{
root = f2;
break;
}
printf("\n");
printf("The root after %d iteration is
%f", i+1, root);
if(f1*f2<0)
y2 = root;
else if(f2*f3<0)
x2 = root;
}
}
printf("\n\nThe Approximation to the root is %3.4f",
root);
getch();
return 0;
}
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
Output:-
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
3. WAP to find the real root for the equation x3-
3x+1=0 up to 4 decimal places using iteration
method.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.0001
float f(float x)
{
return (pow(x,3) - 3*x + 1);
}
float g(float x)
{
return (pow(x,3)+1)/3;
}
int main()
{
float m,x1=1,x2=-1,f1,f2=0.0,f3;
int i=0;
clrscr();
m = (x1+x2)/2;
f1=f(m);
if(f1>0)
{
do
{
f1=f2;
f2=g(f1);
f3=f1;
i++;
printf("\nf1: %f\tf2: %f\tf3:
%f",f1,f2,f3);
}while(fabs(f3-f2)>e);
}
printf("\nIteration: %d, Final Root: %f",i,f3);
getch();
return 0;
}
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
Output:-
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
4. WAP to find the real root for the equation cosx-
3x+1=0 for 7 iteration using iteration method.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.0001
float f(float x)
{
return cos(x)-3*x+1;
}
float g(float x)
{
return (cos(x)+1)/3;
}
int main()
{
float m,x1=1,x2=-1,f1,f2=0.0,f3;
int i=0;
clrscr();
m = (x1+x2)/2;
f1=f(m);
if(f1>0)
{
do
{
f1=f2;
f2=g(f1);
f3=f1;
i++;
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
printf("\nIteration: %d, Root: %f",i,f3);
}while(i<7);
}
printf("\n\nIteration: %d, Final Root: %f",i,f3);
getch();
return 0;
}
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
Output:-
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
5. WAP to find the root for the equation x3-3x+1=0
up to 4 decimal places using Regula Falsi Method.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (pow(x,3)-3*x+1);
}
float g(float x1, float x2, float f1, float f2)
{
return ((x1*f2)-(x2*f1))/(f2-f1);
}
int main()
{
float a, b, x1, x2, x3, f1, f2, f3, f4;
int i, j=0;
clrscr();
for(i=1;i<10;i++)
{
a=f(i);
b=f(i+1);
if(a<0 && b>0)
{
x1=i;
x2=i+1;
break;
}
}
printf("%d %d",i,i+1);
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
do
{
f1=f(x1);
f2=f(x2);
x3=g(x1, x2, f1, f2);
f3=f(x3);
if(f3>0)
x2=x3;
else
x1=x3;
j++;
printf("\nIteration: %d, Root: %f", j, x3);
}while(fabs(f3)>0.0001);
printf("\n\nIteration: %d, Final Root: %f",j,x3);
getch();
return 0;
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
Output:-
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
6. WAP to find the root for the equation x3-5x+1=0
up to 3 decimal points between the interval
(2,2.5) using Secant method.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
float g(float, float, float, float);
int main()
{
float x1=2, x2=2.5, x3, a, b;
int i=0;
clrscr();
do
{
a=f(x1);
b=f(x2);
x3=g(x1,x2,a,b);
x1=x2;
x2=x3;
i++;
printf("\nIteration: %d, Root: %f",i,x3);
}while(fabs(x2-x1)>0.0001);
printf("\n\nIteration: %d, Final Root: %f",i,x3);
getch();
return 0;
}
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
float f(float x)
{
return (pow(x,3)-5*x+1);
}
float g(float x1, float x2, float a, float b)
{
return ((x1*b)-(x2*a))/(b-a);
}
Vibhor Singhal Page: ____ 2101097(75)
Problem: ___ Date: ___ / ___ / 2018
Output:-
Vibhor Singhal Page: ____ 2101097(75)