And Percentage Error.: 1. WAP To Find The Absolute Error, Relative Error
And Percentage Error.: 1. WAP To Find The Absolute Error, Relative 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;
}
Output:-
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();
x2 = x1;
y2 = y1;
if(findroot(x2)==0)
root = x2;
else if (findroot(y2)==0)
root = y2;
else
{
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;
}
Output:-
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;
}
Output:-
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++;
}
printf("\n\nIteration: %d, Final Root: %f",i,f3);
getch();
return 0;
}
Output:-
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (pow(x,3)-3*x+1);
}
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);
do
{
f1=f(x1);
f2=f(x2);
f3=f(x3);
if(f3>0)
x2=x3;
else
x1=x3;
j++;
}while(fabs(f3)>0.0001);
getch();
return 0;
Output:-
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++;
float f(float x)
{
return (pow(x,3)-5*x+1);
}
Output:-