Professional Documents
Culture Documents
START
Define F(x)
Initialize i =1
Is
B=X F(mid)< A=X
0
Is Abs
(XI- X
X)<Aer
r
I<iter
Y
No
Print iter, XI
STOP
Subroutine Bisect
X= (A+B)/2
Iter ++
Print ITER, XI
RETURN
START
h = f(x0)/df(x0)
x1 = x0-h
Print itr,x1
No
Is
fabs(h) X0 =x1
<aerr
STOP
STOP
START
Initialize itr
Call function regulafalsi with x3, x0, x1, f(x1), f(x0), itr
Yes No
Is
X1 = x2 X0 = x1
f(x0)*f(x1)<0
Yes
Is fabs(x3-
C
x2)<aerr
No
x2 = x3
Yes
Is itr < maxitr
B
No
STOP
X = x0-((x1-x0)/(f(x1)-f(x0))*f(x0)
Print itr, x
Return
Print “Solution”
RETURN
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
// Declaration of Functions
void bisection();
void regulafalsi();
void raphson();
void main()
int c;
scanf("%d",&c);
switch(c)
case 1:
bisection();
break;
case 2:
regulafalsi();
break;
case 3:
raphson();
break;
getch();
// Function Definition
void bisection()
int count=1,n;
float root=1;
float x0,x1,x2;
float f0,f1,f2;
int i=0;
clrscr();
scanf("%d",&n);
for(x2=1;;x2++)
f2=F(x2);
if(f2>0)
break;
for(x1=x2-1;;x2--)
f1=F(x1);
if(f1<0)
break;
printf("\t\t----------------------------------------------------------");
printf("\t\t----------------------------------------------------------");
for(;count<=n;count++)
x0=(x1+x2)/2.0;
f0=F(x0);
if(f0==0)
root=x0;
if(f0*f1<0)
x2=x0;
else
x1=x0;
f1=f0;
if(fabs((x1-x2)/x1)<EPS)
{printf("\n\t\t------------------------------------------------------");
printf("\t\t--------------------------------------------------------");
getch();
exit(0);
printf("\n\t\t--------------------------------------------------------");
printf("\t\t----------------------------------------------------------");
getch();
//Function Definition
void regulafalsi()
float f0,f1,f2;
float x0,x1,x2;
int itr;
int i;
clrscr();
printf("\n\t\t\t 3*x+sin(x)-exp(x)=0\n\n");
scanf("%d",&itr);
for(x1=0.0;;)
f1=f(x1);
if(f1>0)
break;
else
x1=x1+0.1;
x0=x1-0.1;
f0=f(x0);
printf("\n\t\t----------------------------------------------------------");
printf("\t\t------------------------------------------------------------");
for(i=0;i<itr;i++)
x2=x0-((x1-x0)/(f1-f0))*f0;
f2=f(x2);
if(f0*f2>0)
x1=x2;
f1=f2;
else
x0=x2;
f0=f2;
if(fabs(f2)>EPS)
printf("\n\t\t %d\t%f\t%f\n",i+1,x2,f2);
continue;
printf("\t\t------------------------------------------------------------");
printf("\n\t\t\t\tRoot = %f\n",x2);
printf("\t\t-----------------------------------------------------------");
getch();
//Function definition
void raphson()
//......defining formulae
float x1,x0;
float f0,f1;
float df0;
float eps;
float error;
int i=1;
int itr;
clrscr();
for(x1=0; ;x1+=0.01)
f1=f(x1);
if(f1>0)
break;
x0=x1-0.01;
f0=f(x0);
scanf(" %d",&itr);
scanf("%f",&eps);
if(fabs(f0)>f1)
if (f1>fabs(f(x0)))
x0=(x0+x1)/2;
for(;i<=itr;i++)
f0=f(x0);
df0=df(x0);
x1=x0-(f0/df0);
error=fabs(x1-x0);
if(error<eps)
{break;}
x0=x1;
if(error>eps)
printf("\n\n\n\t\t\t-----------------------------------");
printf("\n\t\t\t------------------------------------"); }
--------------------------------------------------------**************************------------------------------------------------------
( 1 ). Bisection Method
----------------------------------------------------------
ITERATIONS ROOTS
----------------------------------------------------------
ITERATION 1 : 2.500000
ITERATION 2 : 2.750000
ITERATION 3 : 2.625000
ITERATION 4 : 2.687500
ITERATION 5 : 2.718750
--------------------------------------------------------
Root = 2.7188
Iterations = 5
----------------------------------------------------------
Equation is :
3*x+sin(x)-exp(x)=0
----------------------------------------------------------
ITERATION x2 F(x)
------------------------------------------------------------
1 0.361262 0.002101
------------------------------------------------------------
Root = 0.361262
-----------------------------------------------------------
-----------------------------------
------------------------------------
START
Input n
Get value of x
h = ax[1] – ax[0]
End loop i
Start loop j = 2 to 4
Start loop I = 0 to n - j
End loop i
End loop j
I=0
Is No
B ax[i]>x I=I+1 B
I = i-1
P = (x – ax[i])/h
Y1 = p* diff[I - 1][1]
Y2 = p * (p + 1) * diff[I – 2][2]/2
Y3 = ( p + 2 ) * P * (p -1 )*diff[I – 2][3]/6
Y4 = (p + 2) * (p + 1) * p * (p – 1) * diff[I – 3][4]/24
Ay[i] + y1 + y2 + y3 + y4
STOP
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
void main()
int n,i,j;
float ax[10],ay[10],x,h,p,diff[20][20],y1,y2,y3,y4,y=0;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&ax[i]);
for(i=0;i<n;i++)
scanf("%f",&ay[i]);
scanf("%f",&x);
h=ax[1]-ax[0];
for(i=0;i<n-1;i++){
diff[i][1]=ay[i+1]-ay[i];}
for(j=2;j<=4;j++)
{for(i=0;i<n-j;i++){
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
} }
i=0;
do
i++;
}while(ax[i]<x);
i--;
p=(x-ax[i])/h;
y1=p*diff[i-1][1];
y2=p*(p+1)*diff[i-1][2]/2;
y3=p*(p+1)*(p-1)*diff[i-2][3]/6;
y2=(p+2)*(p+1)*p*(p-1)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
getch(); }
--------------------------------------*********************-----------------------------------------------
when x=2.3000,y=2.62999988
START
h = ax[1] – ax[0]
End loop i
Start loop j = 2 to 4
Start loop I = 0 to n - j
End loop i
End loop j
I=0
Is ! ax[i] < x
A
I = I +1
A
E
X0 = mx[i]
Sum = 0
Y0 = my[i]
Fun = 1
P = (x – x0)/n
Sum = y0
Start loop k = 1 to 4
End loop k
STOP
VIVEK GUPTA Roll no: 0905210064
P a g e | 23
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
void main()
int n,i,j,k;
float mx[10],my[10],x,x0=0,y0,sum,h,fun,p,diff[20][20],y1,y2,y3,y4;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&mx[i]);
for(i=0;i<n;i++)
scanf("%f",&my[i]);
scanf("%f",&x);
h=mx[1]-mx[0];
for(i=0;i<n-1;i++)
diff[i][1]=my[i+1]-my[i]; }
for(j=2;j<=4;j++)
for(i=0;i<n-j;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}}
i=0;
while(!mx[i]>x)
{ i++; }
x0=mx[i];
sum=0;
y0=my[i];
fun=1;
p=(x-x0)/h;
sum=y0;
for(k=1;k<=4;k++)
fun=(fun*(p-(k-1))/k);
sum=sum+fun*diff[i][k];
--------------------------------------------------**********************-----------------------------------------
when x=2.3000,y=2.62999988
START
Y=0
Loop for I = 0 to n
Nr = dr = 1
Loop for j to n
Is j ! = i B
Nr* = x – ax[j]
B
End loop [j]
Print x, y as solution
STOP
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
void main()
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&ax[i]);
}
VIVEK GUPTA Roll no: 0905210064
P a g e | 28
for(i=0;i<n;i++)
scanf("%f",&ay[i]); }
scanf("%f",&x);
for(i=0;i<n;i++)
nr=1;
dr=1;
for(j=0;j<n;j++)
{ if(j!=i)
{ nr=nr*(x-ax[j]);
dr=dr*(ax[i]-ax[j]); }
y+=(nr/dr)*ay[i];
}}
getch();
------------------------------------------------*****************************------------------------------------------
When x= 2.30,y=13.47
START
H = (Xn- x0)/n
S = Y0 + Yn + 4Y1
Loop for I = 3 to n -1
S + = 4 * y1 + 2 * Yi+1
p = (h/3 ) * s
Print “ solution”, p
STOP
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
float fun(float);
void main()
float result=1;
float a,b;
float sum,h;
int i,j,n;
clrscr();
scanf("%f",&a);
scanf("%f",&b);
scanf("%d",&n);
h=(b-a)/n;
sum=0;
VIVEK GUPTA Roll no: 0905210064
P a g e | 32
sum=fun(a)+4*fun(a+h)+fun(b);
for(i=3;i<n;i+=2)
sum+=2*fun(a+(i-1)*h)+4*fun(a+i*h);
result=sum*h/3;
getch();
//...Function body
float fun(float x)
float temp;
temp=1/(1+(x*x));
return temp;
------------------------------------*******************************-------------------------
Lower Limit a - 2
Upper limit b - 8
START
H = (Xn – x0)/n
Sum = 0
Loop for I = 1 to n
Is I % 3 Sum + = 2 * f(a + I * h)
=0
Sum + = 3 * f(a + I * h)
End loop i
Print sum
STOP
#include<conio.h>
#include<math.h>
#include<string.h>
float fun(float);
void main()
float result=1;
float a,b;
float h,sum;
int i,j,n;
clrscr();
//...Input Range
scanf("%f",&a);
scanf("%f",&b);
scanf("%f",&n);
h=(b-a)/n;
VIVEK GUPTA Roll no: 0905210064
P a g e | 36
sum=0;
sum=fun(a)+fun(b);
for(i=0;i<n;i++)
if(i%3==0) {
sum+=2*fun(a+i*h); }
else {
sum+=3*fun(a+(i)*h); }
result=sum*3*h/8;
getch();
//...Function body
float fun(float x)
float temp;
temp=1/(1+(x*x));
return temp;
---------------------------------************************----------------------------------
START
H = (Xn- x0)/n
S = y(x0) + y(Xn)
Loop for I = 1 to n -1
S + = 2 * y * (x0 + I * h)
T = (h/2 ) * s
Print “ solution”, t
STOP
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
float fun(float);
void main()
float result=1;
float a,b;
float h,sum;
int i,j,n;
clrscr();
//...Input range
scanf("%f",&a);
scanf("%f",&b);
scanf("%d",&n);
h=(b-a)/n;
VIVEK GUPTA Roll no: 0905210064
P a g e | 39
sum=0;
sum=fun(a)+fun(b);
for(i=1;i<n;i++)
sum+=2*fun(a+i);
result=sum*h/2;
getch();
float fun(float x)
float temp;
temp=1/(1+(x*x));
return temp;
-----------------------------***********************----------------------
Lower limit a - 2
Upper Limit b - 8
#include<conio.h>
#include<math.h>
void main()
Int i=0;
Int observ;
float x[10];
float y[10];
float xy[10];
float x2[10];
float sum1=0.0;
float sum2=0.0;
float sum3=0.0;
float sum4=0.0;
double a;
double b;
clrscr();
scanf(“%d”,&observ);
for(;i<observ;i++)
{
VIVEK GUPTA Roll no: 0905210064
P a g e | 42
Scanf(“%f”,&x*i+);
Sum1+=x[i];
for(i=0;i<observ;i++)
Scanf(“%f”,&y*i+);
Sum2+=y[i];
for(i=0;i<observ;i++):
xy[i]=x[i]*y[i];
Sum3+=xy[i];
for(i=0;i<observ;i++);
x2[i]=x[i]*x[i];
Sum4+=x2[i];
a=(sum2*sum4-sum3*sum1)/(observ*sum4-sum1*sum1);
b=(sum2-observ*a)/sum1;
getch();