REGULA FALSI METHODFormula:
C=(a*f(b)-b*f(a)/f(b)-f(a)
Program:
deff(‘d=f(x)’,’d=x^3-100′)
a=input(“Enter the value of a:”)
b=input(“Enter the value of b:”)
n=input(“Enter the number of iterations n:”)
for i=1:n
c=(a*f(b)-b*f(a))/(f(b)-f(a))
disp([i,c])
if f(a)*f(c)<0 then
b=c
end
if f(b)*f(c)<0 then
a=c
end
c1=(a*f(b)-b*f(a))/(f(b)-f(a))
if abs(c1-c)<0.00001 then
disp(“We get accurate roots”)
break;
end
end
/*PROGRAM FOR NEWTON RAPHSON METHOD*/
deff(‘y=f(x)’,’y=x^2-2*x-1′)
deff(‘dy=df(x)’,’dy=2*x-2′)
a=0,n=5;
for i=1:n
c=(a-f(a)/df(a))
disp([a,f(a),df(a)])
a=c
end
printf(“c=%f”,c)
1] Newton’s Forward Gregory Interpolation:
Program:
x=[1 2 3 4 5]
y=[1 4 9 16 25]
d1=mtlb_diff(y)
d2=mtlb_diff(d1)
d3=mtlb_diff(d2)
d4=mtlb_diff(d3)
disp([d1d2d3d4])
d=[d1(1)d2(1)d3(1)d4(1)]
disp(d)
x0=1.5;
h=1;
u=(x0-x(1))/h;
printf(“u=%f”,u)
ans=0;
for i=1:4
q=1
for j=1:i
p=4-(j-1);
q=q*p
end
t=d(i)*q/factorial(i)
ans=ans+t
end
y=y(1)+ans
printf(“\n The value of function at %f:%f”,x0,y)
Program:- (Sort a set of points w.r.t. REACTANGLE-2D)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float x[10],y[10],Xmin,Ymin,Xmax,Ymax;
clrscr();
printf(“\n\n Enter the values of Xmin & Ymin =\t”);
scanf(“%f%f”,&Xmin,&Ymin);
printf(“\n\n Enter the values of Xmax & Ymax =\t”);
scanf(“%f%f”,&Xmax,&Ymax);
printf(“\n\n Enter the number of points to be sort =\t”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n\n Enter the cordinates of point %d =\t”,i);
scanf(“%f%f”,&x[i],&y[i]);
if(((x[i]>Xmin) && (Xmax>x[i])) && ((y[i]>Ymin) && (Ymax>y[i])))
printf(“\n\n The input point is INSIDE the rectangle.”);
else
if((x[i]<Xmin) || (Xmax<x[i]) || (y[i]<Ymin) || (Ymax<x[i]))
printf(“\n\n The input point is OUTSIDE the rectangle.”);
else
printf(“\n\n The input point is ON the rectangle.\n\n”);
}
getch();
}
/*
OUTPUT:-
Enter the values of Xmin & Ymin = 1 3
Enter the values of Xmax & Ymax = 7 8
Enter the number of points to be sort = 3
Enter the cordinates of point 1 = 1 7
The input point is ON the rectangle.
Enter the cordinates of point 2 = 4 6
The input point is INSIDE the rectangle.
Enter the cordinates of point 3 = 1 2
The input point is OUTSIDE the rectangle.
*/
Program:- (Sort a set of points w.r.t. LINE)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float x[20],y[20],z[20],x1,x2,y1,y2;
clrscr();
printf(“\n Enter cordinates of points of line (x1,y1) & (x2,y2) =\t”);
scanf(“%f%f%f%f”,&x1,&x2,&y1,&y2);
printf(“\n Enter the number of points to be sort =t”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n Enter the %d co-ordinates of point(x[i],y[i]) =”,i);
scanf(“%f%f”, &x[i],&y[i]);
z[i]=((y[i]-y1)*(x2-x1)) – ((x[i]-x1)*(y2-y1));
printf(“\n z[i]=%f”,z[i]);
if(z[i]>0)
{
printf(“\n z[i] is greater than zero, therefore point(%f,%f) is ABOVE the line.”,x[i],y[i]);
}
else
if(z[i]<0)
{
printf(“\n z[i] is less than zero, therefore point(%f,%f) is BELOW the line.”,x[i],y[i]);
}
else
{
printf(“\n z[i] is equal to zero, therefore point(%f,%f) is ON the line.”,x[i],y[i]);
}
}
getch();
}
/*
OUTPUT:-
Enter cordinates of points of line (x1,y1) & (x2,y2) = 1 3 2 1
Enter the number of points to be sort =3
Enter the 1 co-ordinates of point(x[i],y[i]) = 1 2
z[i]=0.000000
z[i] is equal to zero, therefore point(1.000000,2.000000) is ON the line.
Enter the 2 co-ordinates of point(x[i],y[i]) = -1 2
z[i]=-2.000000
z[i] is less than zero, therefore point(-1.000000,2.000000) is BELOW the line.
Enter the 3 co-ordinates of point(x[i],y[i]) = 4 5
z[i]=9.000000
z[i] is greater than zero, therefore point(4.000000,5.000000) is ABOVE the line*/
Ques. Evaluate ?_0^6¦x2 dx by using Simpson’s 3/8 rule & value of n=6.
Solu.:
Program:
deff(‘y=f(x)’,’y=x^2′)
a=0;b=6;n=6;
h=(b-a)/n
printf(“h=%f”,h)
fori=0:n
x=a+i*h
y=f(x)
disp([xy])
end
sum1=0;sum2=0;
fori=1:n-1
x=a+i*h
ifmodulo(i,3)==0then
sum2=sum2+f(x)
else
sum1=sum1+f(x)
end
end
t=(3*h/8)*[(f(a)+f(b))+3*sum1+2*sum2]
printf(“t=%f”,t)
Output:
exec(‘D:\poojardbms\scilab\prat12_1.sce’, -1)
h=1.000000
0. 0.
1. 1.
2. 4.
3. 9.
4. 16.
5. 25.
6. 36.
t=72.000000
/*PROGRAM TO FIND SIMPSONS 1/3RD RULE/*
deff(‘y=f(x)’,’y=x^2′)
a=1,b=6,n=5;
h=(b-a)/n
printf(“h=%f”,h)
for i=0:n
x=a+i*h
y=f(x)
disp([x y])
end
sum1=0,sum2=0
for i=1:n-1
x=a+i*h
if modulo(i,2)==0 then
sum1=sum1+f(x)
else
sum2=sum2+f(x)
end
th=(h/3)*(f(a)+f(b)+(4*(sum1))+(2*(sum2)))
end
printf(“th=%f”,th)
OUTPUT:-
h=1.000000
1. 1.
2. 4.
3. 9.
4. 16.
5. 25.
6. 36.
th=71.000000
Ques. Eulars methode
Program:
deff(‘y=f(x,y)’,’y=(x^2+y)’)
x0=0;y0=1;h=0.02;
next=y0+h*f(x0,y0)
x0=x0+h;
y0=next;
disp([x0y0])
end
Program:- (sort a set of points w.r.t. RECTANGULAR BOX-3D)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float x[10],y[10],z[10],Xmin,Xmax,Ymin,Ymax,Zmin,Zmax;
clrscr();
printf(“\n Enter the minium & maximum values for line X=\t”);
scanf(“%f%f”,&Xmin,&Xmax);
printf(“\n Enter the minium & maximum values for line Y=\t”);
scanf(“%f%f”,&Ymin,&Ymax);
printf(“\n Enter the minium & maximum values for line Z=\t”);
scanf(“%f%f”,&Zmin,&Zmax);
printf(“\n Enter how many points to be sorts=\t”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n Enter the co-ordinates of a point (x,y,z) =\t”,i);
scanf(“%f%f%f”,&x[i],&y[i],&z[i]);
if(((x[i]>Xmin) && (Xmax>x[i])) && ((y[i]>Ymin) && (Ymax>y[i])) && ((z[i]>Zmin) && (Zmax>z[i])))
printf(“\n\n The input point is INSIDE the rectangular box.\n\n”);
else if((x[i]<Xmin) || (Xmax<x[i]) || (y[i]<Ymin) || (Ymax<y[i]) || (z[i]<Zmin) || (Zmax<z[i]))
printf(“\n\n The input point is OUTSIDE the rectangular box.\n\n”);
else
printf(“\n\n The input point is ON the rectangular box.\n\n”);
}
getch();
}
/*
OUTPUT:-
Enter the minium & maximum values for line X= 1 7
Enter the minium & maximum values for line Y= 2 8
Enter the minium & maximum values for line Z= 5 9
Enter how many points to be sorts= 3
Enter the co-ordinates of a point (x,y,z) = 4 4 6
The input point is INSIDE the rectangular box.
Enter the co-ordinates of a point (x,y,z) = 9 8 7
The input point is OUTSIDE the rectangular box.
Enter the co-ordinates of a point (x,y,z) = 3 2 5
The input point is ON the rectangular box.
*/
Output:
exec(‘D:\poojardbms\scilab\prat10_1.sce’, -1)
Warning : redefining function: f . Use funcprot(0) to avoid this message
0.02 1.020101
Ques. ?_0^6¦x2 dx using Trapezoid Rule into 6 parts.
Program:
deff(‘y=f(x)’,’y=x^2′)
a=0;b=1;h=0.5;
n=(b-a)/h
printf(“n=%f”,n)
fori=0:n
x=a+i*h
y=f(x)
disp([xy])
end
sum=0
fori=1:n-1
x=a+i*h
sum=sum+f(x)
end
printf(“sum=%f”,sum)
t=h/2*[(f(a)+f(b))+2*sum]
printf(“t=%f”,t)
Output:
exec(‘D:\poojardbms\scilab\prat9_1.sce’, -1)
n=2.000000
0. 0.
0.5 0.25
1. 1.
Warning : redefining function: sum . Use funcprot(0) to avoid this message
sum=0.250000t=0.375000
/*PROGRAM TO FIND SIMPSONS 1/3RD RULE/*
deff(‘y=f(x)’,’y=x^2′)
a=1,b=6,n=5;
h=(b-a)/n
printf(“h=%f”,h)
for i=0:n
x=a+i*h
y=f(x)
disp([x y])
end
sum1=0,sum2=0
for i=1:n-1
x=a+i*h
if modulo(i,2)==0 then
sum1=sum1+f(x)
else
sum2=sum2+f(x)
end
th=(h/3)*(f(a)+f(b)+(4*(sum1))+(2*(sum2)))
end
printf(“th=%f”,th)
OUTPUT:-
h=1.000000
1. 1.
2. 4.
3. 9.
4. 16.
5. 25.
6. 36.
th=71.000000
Ques. Use Rangakutta method of order second to solve the following using step size using h=1,
dy/dx=(5×2-y)/(ex+y)
Program:
deff(‘y=f(x,y)’,’y=(5*(x^2)-y)/(%e^(x+y))’)
x0=0;y0=1;h=0.1;n=2;
fori=1:n
k1=h*f(x0,y0)
k2=h*f(x0+h,y0+k1)
disp([k1k2])
printf(“\n k1=%f, k2=%f”,k1,k2)
t=y0+(1/2)*[k1+k2]
disp(t)
printf(“\n t=%f”,t)
x0=x0+h
y0=t
end
Output:
– 0.0367879 – 0.0315373
k1=-0.036788, k2=-0.031537
0.9658374
t=0.965837
– 0.0315450 – 0.0236185
k1=-0.031545, k2=-0.023618
0.9382556
t=0.938256
Ques. Runge kutta fourth order
dy/dx=y-x, where y(0)=2, obtain y(0.1) & y(0.2), take h=0.1
Program:
deff(‘y=f(x,y)’,’y=y-x’)
x0=0;y0=2;h=0.1;n=2;
fori=1:n
k1=h*f(x0,y0)
k2=h*f(x0+h/2,y0+k1/2)
k3=h*f(x0+h/2,y0+k2/2)
k4=h*f(x0+h,y0+k3)
disp([k1k2k3k4])
printf(“\n k1=%f, k2=%f, k3=%f, k4=%f”,k1,k2,k3,k4)
t=y0+(1/6)*[k1+2*k2+2*k3+k4]
disp(t)
printf(“\n t=%f”,t)
x0=x0+h
y0=t
end
Output:
0.2 0.205 0.20525 0.2105
k1=0.200000, k2=0.205000, k3=0.205250, k4=0.210525
2.2051708
t=2.205171
0.2105171 0.2160429 0.2163192 0.2221490
k1=0.210517, k2=0.216043, k3=0.216319, k4=0.222149
2.4214026
t=2.421403
Program:- (To find least MUTUAL DISTANCE between the points)
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int i,j,n,a;
float x[10],y[10],d[10][10],Dmax;
clrscr();
printf(“\n\n Enter the number of points to be processing :\t”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n Enter the cordinates of point %d =\t”,i);
scanf(“%f%f”,&x[i],&y[i]);
}
for(i=1;i<=n;i++)
{
Dmax=0;
for(j=1;j<=n;j++)
{
if(i!=j)
{
d[i][j]=sqrt(((x[i]-x[j] ) * (x[i]-x[j] )) + ((y[i]-y[j] ) * (y[i]-y[j])));
if(d[i][j]>Dmax)
{
Dmax=d[i][j];
a=j;
}
}
}
printf(“\n Maximum distance between the points p(%d) & p(%d)=%f”,i,a,Dmax);
printf(“\n\n”);
}
getch();
}
/*
OUTPUT:-
Enter the number of points to be processing : 4
Enter the cordinates of point 1 = 1 3
Enter the cordinates of point 2 = 6 0
Enter the cordinates of point 3 = 4 2
Enter the cordinates of point 4 = 3 1
Maximum distance between the points p(1) & p(2)=5.830952
Maximum distance between the points p(2) & p(1)=5.830952
Maximum distance between the points p(3) & p(1)=3.162278
Maximum distance between the points p(4) & p(2)=3.162278
*/
Program:- ( To find the FARTHEST DISTANCE between points)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int i,j,n,a;
float x[10],y[10],d[10][10],Dmax;
clrscr();
printf(“\n\n Enter the number of points to be processing :\t”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n Enter the cordinates of point %d =\t”,i);
scanf(“%f%f”,&x[i],&y[i]);
}
for(i=1;i<=n;i++)
{
Dmax=0;
for(j=1;j<=n;j++)
{
if(i!=j)
{
d[i][j]=sqrt(((x[i]-x[j] ) * (x[i]-x[j] )) + ((y[i]-y[j] ) * (y[i]-y[j])));
if(d[i][j]>Dmax)
{
Dmax=d[i][j];
a=j;
}
}
}
printf(“\n Maximum distance between the points p(%d) & p(%d)=%f”,i,a,Dmax);
printf(“\n\n”);
}
getch();
}
/*
OUTPUT:-
Enter the number of points to be processing : 4
Enter the cordinates of point 1 = 1 3
Enter the cordinates of point 2 = 6 0
Enter the cordinates of point 3 = 4 2
Enter the cordinates of point 4 = 3 1
Maximum distance between the points p(1) & p(2)=5.830952
Maximum distance between the points p(2) & p(1)=5.830952
Maximum distance between the points p(3) & p(1)=3.162278
Maximum distance between the points p(4) & p(2)=3.162278
*/

