0% found this document useful (0 votes)
17 views6 pages

Adobe Scan 20-Feb-2024

The document contains multiple C programs demonstrating various numerical methods including the Newton-Raphson method, Trapezoidal rule, Simpson's rule, Linear Curve Fitting using Least Squares method, Runge-Kutta method, and Lagrange's interpolation method. Each program includes user input for parameters and outputs results based on mathematical computations. The code snippets illustrate how to implement these methods in C programming for solving equations and performing integrations.

Uploaded by

u191318phy
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)
17 views6 pages

Adobe Scan 20-Feb-2024

The document contains multiple C programs demonstrating various numerical methods including the Newton-Raphson method, Trapezoidal rule, Simpson's rule, Linear Curve Fitting using Least Squares method, Runge-Kutta method, and Lagrange's interpolation method. Each program includes user input for parameters and outputs results based on mathematical computations. The code snippets illustrate how to implement these methods in C programming for solving equations and performing integrations.

Uploaded by

u191318phy
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/ 6

(* C-Program: NEVWTON- RAPHSON METHOD *|

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define FUN(X) 3*x-cos(x)-1 /" Given equation "/
#define DFUN(x) 3+sin(x) /* First derivative of the given equation *1
void main()

int i,n;
float x0,x1,x2;
double e;
clrscr():
printf("\n ***
Newton-Raphson Method ****);
printf("nGive the starting value for iteration (x0):");
scanf("%f", &x0);
printf("n Value of the root to how many significant decimal places? i.e. n=");
scanf("%d", &n);
i=0;
e= 1/(pow(10,n));
printf("ne value = %If", e);
begin:
x1=x0-(FUN(X0))/(DFUN(X0));:
printf("n Iteration No. i=%d:The value of approximate root (x0)=%f", i,x0);
if (fabs((x1-x0)/x1) >= e)

x0 = x1;
i=i+1;
gotobegin;
printf("n The real positive root of the given equation = %f", x1):
x2=x0-(FUN(X0)W(DFUN(X0);
if (x2-x1 ==0.0)
printf("n The root Converged.");
else
printf("n The root does not converge. Try with different e value."):
getch():
*Cprogram: Trapezoidal rule */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(X) 1/(1+pow(X,2))
void main()

int n,i;
float a,b,SUM,trap,h,X;
clrscr();
printf(" ****TRAPEZOIDAL RULE*****\n");
printf("\n Enter the values of a, b, n:");
scanf("%f %f %d", &a, &b, &n);
h=(b-a)/n;
SUM=0.0;
for(i=1;i<=n-1;i++)

X=(a+ih):
SUM=SUM+F(X);:
trap=h/(2.0)"(F(a)+ F(b)+2.0 * SUM);
printf("n Value of the given integral= %f", trap);
getch():
Cprogram: Simpson's (1/3) rule *I
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define F(x) 1/(1+ pow(x, 2))
float a, b, h, sum, os, es, val, x;
void main()

int n, i:
clrscr():
printf("Simpson's (1/3) Rule"):
printf("n Give lower limit of the integral (a)=");
scanf("%f",&a);
printf("\n Give upper limit of the integral(b)=");
scanf("%f",&b);
printf("n Give number of intervals (n) [should be even number]=");
scanf("%d", &n);
h=(b-a)/n;
Sum=es=os=0.0;
Sum=F(a)+F(b);
for(i=1;i<=n-1;i=i+2)
Os=os+F(a+i*h);
for(i=2;i<=n-2;i=i+2)
es=es+F(a+i*h);
val=(h/3.0) (sum+(4*os) +(2*es)):
printf("n The value of the integral between linmits %f and %f \n", a, b);
printf("n with class interval (h)=%f is equal to %fn", h, val);
getch():
}
* C-Program for Linear Curve Fitting LEAST SQUARES METHOD */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()

int n,i;
float a,b,x[20],y[20],sumx=0,sumy=0,sumxy=0,sumx2=0;
clrscr();
printf("\n CprOgram for Linear Curve Fitting of the form Y = a + bX \n ");
printf("\n Enter the value of number of data terms n:");
Scanf("%d",&n):
printf("\n Enter the values of x:"):
for(i=0;i<=n-1;i++)
scanf(" %f",8x[i]);
printf("\n Enter the values of y:");
for(i=0;i<=n-1;i++)
Scanf("%f",&y[i));
}
for(i=0;i<=n-1;i++)
Sumx=Sumx +x[0;
Sumx2=sumx2 +X[[]*x[];
Sumy=sumy +y[0;
sumxy=sumxy +x[i]*y[i];
}
a=((sumx2*sumy -sumx*sumxy)*1.0/(n*sumx2-sumx*sumx)*1.0):
b=((n"sumxy-sumx*sumy)* 1.0/(n*sumX2-sumx*sumx)*1.0);
printf("\n\nThe line is Y=%3.3f +%3.3f X",a,b);
getch ();
* Runge Kutta Method - Fourth order formula
*|
#include<stdio.h>
#include<math.h>
float dydx(float x, float y);
void main()

float x0,y0, k1,k2,k3,k4, k, y,x,h,xn;


drscr();
printf("Runge Kutta method \n");
printf("Enter x0,y0,xn,h:");
Scanf("%f %f %f %f",&x0,&y0,&xn,&h);
X=X0;
y=y0;
printf("\n \nx\t\ty\n");
while(x<xn)

kl = h*dydx(x, y);
k2 = h*dydx((x + 0.5*h), (y +
k3 = h*dydx((x + 0.5*h), (y + 0.5*k1));
k4 = h*dydx((x + h), (y + k3)); 0.5*k2)
);

k=((k1+2*k2+2*k3+k4)*1.0/6.0);
y=y+k;
X=X+h;
1/printf("kl=%f\tk2=%f\tk3=%f\tk4=%f\n",k1, k2, k3, k4);
printf("%of\t%f\n",x, y);
getch();
}
float dydx(float x, float y)

float k:
k=(x+y); /* input the R.H.S. of the differential equation */
return k;
*C-Program to interpolate the value of afunction from a set of values using
LAGRANGE'S INTERPOLATION METHOD */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h
void main()

float x[20],y[20],z[20], s=0.0,t=0.0,a, p;


int i,j, n;
crscr():
printf("\n\t *** LAGRANGE'SINTERPOLATION METHOD ***\n");
printf("Give the number of values in the tabulated data:");
scanf("%d",&n);
printf("\n Give the values ofx and y from the table \n");
for(i=1;i<=n;i++)
scanf("%f%f",&<[i], &y[i]);
printf("\nGive the value of x for which interpolation is to be found:");
Scanf( "lof",&a);
for(i=1;i<=n,it+)
p=1.0; z(l=0.0;
for(j=1;ij<=nj+)
{
if(j!=i)
p=p*(axlil)/(«lij-xj):
S=pi

t=t+s*ylil:
printf("\n\nFor x=%f the interpolated value of y=%f"a,t);
getch):

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy