6 To 16
6 To 16
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) 3*x-cos(x)-1
#define g(x) 3+sin(x)
void main() {
float x0,x1,f0,f1,g0,e, int step=1,N:
printf("\n enter initial gues:\n");
scanf("%f",&x0);
printf("enter tolerable error.\n");
scanf("%f",&e);
printf("eneter maximum iteration:\n");
scanf("%d",&N);
printf("n step\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
g0=g(x0);
fo=f(x0);
if(g0==0.0)
{
printf("mathematical error.");
exit(0);
x1=x0-f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,10,x1,f1);
x0=x1;
step=step+1;
if(step>N)
{
printf("not convergent.");
exit(0);
}
f1=f(x1);
}
while(fabs(f1)>e);
printf("\n root is: %f", x1);
getch();
}
Output:
7./*Program: finding real roots of nonlinear
equation using bisection method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) cos(x)-x*exp(x)
void main()
{ float x0,x1,x2,f0,f1, f2,e;
int step=1;
clrscr();
up:
printf("\n enter two initial guesses:\n");
scanf("%f%f",&x0,&x1);
printf("enter tolerable error:\n");
scanf("%f",&e);
f0=f(x0);
f1=f(x1);
if(f0*f1>0.0) }
printf("incorrect initial guesses.\n");
goto up:
} printf("\nstep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2= (x0+x1)/2;
f2=f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2,f2);
if(fo*f2<0) {
x1=x2;
f1=f2;
} else
{
x0=x2;
step=step+1;
}
f0=f2;
}
while(fabs(f2)>e);
printf("\n root is:%f",x2);
getch();
}
OUTPUT:
8./*Finding real roots of nonlinear equation using
regular falsi or false position method*/:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*log10(x)-1.2
int main()
float x0,x1,x2,f0,f1, f2,e;
int step=1;
clrscr();
up
printf("\n enter two initial guesses:\n");
scanf("%f%f",&x0,8&x1);
printf("enter tolerable error:\n");
scanf("%f.&e");
f0=f(x0);
f1=f(x1);
if(f0-f1>0.0)
printf("incorrect initial guesses \n");
goto up,
} printf("\nstep\t\tx0\t\tx1\t\tx2\t\tf(x2\n');
do
( x2=x0-(x0-x1)*10/(fo-f1);
f2=f(x2),
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2,12);
if(fo*f2<0)
x1-x2;
f1-f2:
}
else
{
x0=x2;
f0=f2;
}
step=step+1;
}
while(fabs(f2)>e);
printf("\n root is:%f",x2);
getch();
return 0;
}
Output:
9./*Program to find secant method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x*x-2*x-5
void main() {
float x0,x1,x2,f0,f1, f2,e;
int step=1,N;
clrscr();
printf("enter intial guesses:\n");
scanf("%f%f",&x,&x1);
printf("enter tolerable error:\n");
scanf("%f",&e);
printf("enter meximum iteration:\n");
scanf("%d",&N);
printf("\nstep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
f0=f(x0);
f1=f(x1);
if(f0==f1)
{
printf("mathematical error.");
exit(0);
} x2=x1-(x1-x0)*f1/(f1-f0);
f2=f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2,f2);
x0=x1;
f0=f1;
x1=x2;
f1=f2;
step=step+1;
if (step>N) {
exit(0);
}
printf("not convergent");
while(fabs(f2)>e);
printf("\nroot is:%f,x2");
getch();
}
Output:
10./* To demonstrate gauss seidel iteration
method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f1(x,y,z) (17-y+2*z)/20
#define f2(x,y,z) (-18-3*x+z)/20
#define f3(x,y,z) (25-2*x+3*y)/20
int main()
{ float x0=0,y0=0,z0=0,x1,y1,z1,e1,e2,e3,e; int count =1;
clrscr();
printf("enter tolerable error;\n");
scanf("%f",&e);
do
{ printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count,x1,y1,z1);
el=fabs(x0-x1); e2=fabs(y0-y1); e3=fabs(z0-z1); count ++; x0=x1;
y0=y1;
z0=z1;
} while(e1>e && e2>e && e3>,e);
printf("\n solution :x=%0.3f,y=%0.3f and
z=%0.3f\n",x1,y1,z1);
getch();
return 0;
}
Output:
11./*program for fixed point iteration*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) cos(x)-3*x+1
#define g(x) (1+cos(x))/3
int main()
{
int step=1,N; float x0,x1,e;
clrscr();
printf("enter initial guess:");
scanf("%f",&x0);
printf("enter tolerable error:");
scanf("%f",&e);
printf("enter maximum iteration:");
scanf("%d",&N);
printf("\n step\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{
x1=g(x0); printf("%d\t%f\t%f\t%f\t%f\n",
step,x0,f(x0),x1,f(x1));
step=step+1;
if(step>N)
{ printf("Not Convergent.");
exit(0);
}
Output:
12./* To demonstrate numerical iteration using
trapezoidal*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) 1/(1+pow(x,2))
int main() (
float lower,upper, integration=0.0,stepsize,k; int i,subinterval;
clrscr();
printf("enter lower limit of integration:");
scanf("%f",&lower);
printf("enter upper limit of integration:");
scanf("%f",&upper),
printf("enter number of sub intervals:"); scanf("%d", &subinterval);
stepsize=(upper-lower)/subinterval, integration=f(lower)+(upper);
for(i=1;i<=subinterval-1;i++);
k-lower+i*stepsize;
integration integration+2*f(k);
}
integration-integration*stepsize/2,
printf("\n required value of integration is: %.3f, integration);
getch();
return 0;
Output:
13./*Simpson 1/3 Rule C Program*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
#include<stdio.h>
#include<conio.h>
int main()
{
float x0, y0, xn, h, yn, slope;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Euler's Method */
printf("\nx0\ty0\tslope\tyn\n");
printf("------------------------------\n");
for(i=0; i < n; i++)
{
slope = f(x0, y0);
yn = y0 + h * slope;
printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn);
y0 = yn;
x0 = x0+h;
}
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
getch();
return 0;
}
Output:
16./* RK-4 Method C Program*/
#include<stdio.h>
#include<conio.h>
int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
getch();
return 0;
}
Output: