0% found this document useful (0 votes)
13 views19 pages

6 To 16

Practical of nm

Uploaded by

mishraanu246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

6 To 16

Practical of nm

Uploaded by

mishraanu246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

6.

/*Finding real roots of nonlinear using newton


raphson method author*/

#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(&quot;enter tolerable error;\n&quot;);
scanf(&quot;%f&quot;,&amp;e);
do
{ printf(&quot;%d\t%0.4f\t%0.4f\t%0.4f\n&quot;,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&gt;e &amp;&amp; e2&gt;e &amp;&amp; e3&gt,e);
printf(&quot;\n solution :x=%0.3f,y=%0.3f and
z=%0.3f\n&quot;,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(&quot;enter initial guess:&quot;);
scanf(&quot;%f&quot;,&amp;x0);
printf(&quot;enter tolerable error:&quot;);
scanf(&quot;%f&quot;,&amp;e);
printf(&quot;enter maximum iteration:&quot;);
scanf(&quot;%d&quot;,&amp;N);
printf(&quot;\n step\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n&quot;);
do
{
x1=g(x0); printf(&quot;%d\t%f\t%f\t%f\t%f\n&quot;,
step,x0,f(x0),x1,f(x1));
step=step+1;
if(step&gt;N)
{ printf(&quot;Not Convergent.&quot;);
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>

/* Define function here */


#define f(x) 1/(1+x*x)

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;

/* Finding Integration Value */


integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%2==0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 4 * f(k);
}
}
integration = integration * stepSize/3;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
Output:
14./* Simpson 3/8 Rule C Program*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

/* Define function here */


#define f(x) 1/(1+x*x)

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;

/* Finding Integration Value */


integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%3 == 0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 3 * f(k);
}
}
integration = integration * stepSize*3/8;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
Output:
15./* Euler’s Method C Program*/

#include<stdio.h>
#include<conio.h>

#define f(x,y) x+y

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);

/* Calculating step size (h) */


h = (xn-x0)/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>

#define f(x,y) (y*y-x*x)/(y*y+x*x)

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);

/* Calculating step size (h) */


h = (xn-x0)/n;

/* Runge Kutta Method */


printf("\nx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
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)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
printf("%0.4f\t%0.4f\t%0.4f\n",x0,y0,yn);
x0 = x0+h;
y0 = yn;
}

/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);

getch();
return 0;
}
Output:

You might also like