NM CH
NM CH
*/ /*program to find the roots of given equation using Newton-Raphson /*program to find the roots of given equation using Secant method.*/
#include<stdio.h> method.*/ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h>
#include<math.h> #include<math.h>
#include<math.h> #include<conio.h> float f(float x) {
void main(){ float f(float x) return(x*x-4*x-10); }
clrscr(); { float main() {
int a,b,c,d; return x*x-4*x-10; float a,b,c,e;
} int count=1,n;
float r1,r2,re,im;
float df (float x) printf("\n\nEnter the values of a and b:\n"); //(a,b) must contain the solution.
printf("enter the value of a,b and c"); { scanf("%f%f",&a,&b);
scanf("%d%d%d",&a,&b,&c); return 2*x-4; printf("Enter the values of allowed error and maximun number of
d=(b*b-4*a*c); } iterations:\n");
if(d==0){ int main() scanf("%f %d",&e,&n);
{ do {
r1=r2=-b/(2*a);
int i, itr; if(f(a)==f(b))
printf("the root is r1=%f r2=%f",r1,r2); } float h, x0, x1,a; {printf("\nSolution cannot be found as the values of a and b are same.\n");
else if(d>0){ printf("\nEnter x0, allowed error and maximum iterations\n"); return 0; }
r1=(-b+sqrt(d))/(2*a); scanf("%f %f %d", &x0, &a, &itr); c=(a*f(b)-b*f(a))/(f(b)-f(a));
r2=(-b-sqrt(d))/(2*a); for (i=1; i<=itr; i++) a=b;
{ b=c;
printf("the roots are r1=%f r2=%f",r1,r2); }
h=f(x0)/df(x0); printf("Iteration No-%d x=%f\n",count,c);
else{ x1=x0-h; count++;
re=-b/(2*a); printf(" At Iteration no. %3d, x = %9.6f\n", i, x1); if(count==n) {
im=sqrt(-d)/(2*a); if (fabs(h) < a) break; }
printf("root1=%.2f+%.2fi",re,im); { } while(fabs(f(c))>e);
printf("After %3d iterations, root = %8.6f\n", i, x1); printf("\n The required solution is %f\n",c);
printf("root2=%.2f-%.2fi",re,im);
getch(); getch();
} return 0; }
getch(); } }
x0=x1;
/*program to evaluate the polynomial*/ }
/*program for Linear Regression(Least Square)*/ /*program to find Integration Using Trapezoidal Rule*/
#include<stdio.h> printf(" The required solution does not converge or iterations are
#include<stdio.h> #include<stdio.h>
insufficient\n");
#include<conio.h> #include<conio.h>
getch(); #include<math.h>
void main() #include<math.h>
return 1; #include<conio.h>
{ } main() {
int float f(float x) {
double x,p,a[12]; clrscr(); return (1/(1+x*x)); }
int i,n; int n,i,x[10],y[10],sumx=0,sumy=0,sumxy=0,sumx2=0; int main(){
clrscr(); float a,b; clrscr();
printf("enter degree of polynomial"); printf("\n Enter the value of number of terms n:"); float a,b,n,h,sum1=0,sum,y0,yn;
scanf("%d",&n); scanf("%d",&n); int i;
printf("enter coefficient of equation "); printf("\n Enter the values of x:\n"); printf("Enter the upper limit : ");
for(i=0;i<=n;i++) for(i=0;i<n;i++) { scanf("%f",&b);
scanf("%lf",&a[i]); scanf("%d",&x[i]); } printf("Enter the lower limit : ");
printf("point of evaluation value"); printf("\n Enter the values of y:"); scanf("%f",&a);
scanf("%lf",&x); for(i=0;i<n;i++) { printf("Enter the number of intervals : ");
p=a[0]; scanf("%d",&y[i]); } scanf("%f",&n);
for(i=0;i<n;i++) for(i=0;i<n;i++) { h=(b-a)/n;
{ sumx+=x[i]; y0=f(a);
p=(p*x+a[i+1]); sumx2+=(x[i]*x[i]); yn=f(b);
} sumy+=y[i]; for(i=1; i<n; i++)
printf("f(x)=%f",p); sumxy+=(x[i]*y[i]); } sum1+ =f(a+i*h);
getch(); b=(n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)*1.0; sum =(h/2)*(y0 + yn + 2*sum1);
} a=(sumy-b*sumx)/n; printf("Answer : %f",sum);
printf("\n\nThe line is Y=%.3f+%.3f X",a,b); getch();
getch(); …. Return0; } return 0; }
return 0; }
/*program to find the roots of given equation using Bisection method.*/ }/*program for Lagrange Inrerpolation*/ Eulers #include<stdio.h>
#include<stdio.h> #include<stdio.h> #include<conio.h>
#include<conio.h> #include<conio.h> #include<math.h>
#include<math.h> #include<math.h> double f(double x, double y){
double F(double x){ void main() return pow(x,3)+y*y; }
return(pow(x,2)-4*x-10);//This return the value of the function } { main(){
int main() { clrscr(); int i;
clrscr(); float x[10],y[10],temp=1,f[10],sum=0,p; double y,xi,yi,xf,h;
printf("This program illustrates the bisection method in C\n"); int i,n,j,k=0; printf("Enter the initial condition for y: ");
printf("x^2 -4*x - 10 = 0\n"); printf("\nhow many record you will be enter: "); scanf("%lf",&yi);
double x0,x1; scanf("%d",&n); printf("Enter the initial condition for x: ");
printf("Enter the first approximation to the root\n"); printf("\n\nenter the value of x and y"); scanf("%lf",&xi);
scanf("%lf",&x0); for(i=0; i<n; i++) printf("Enter the value of x for which y is required: ");
printf("Enter the second approximation to the root\n"); { scanf("%lf",&xf);
scanf("%lf",&x1); scanf("%f %f",&x[i],&y[i]); printf("Enter the step-width h: ");
int iter; } scanf("%lf",&h);
printf("Enter the number of iterations you want to perform\n"); printf("\n\nEnter X for finding f(x): "); printf("x\t\ty\t\ty'\t\thy'\t\ty+hy'\n");
scanf("%d",&iter); scanf("%f",&p); printf("______________________________________________________________\n");
int ctr=1; for(i=0;i<n;i++) //Begin Euler Routine
double l1=x0; { while(xi<xf){
double l2=x1; temp = 1; y=yi+h*f(xi,yi);
double r,f1,f2,f3; k = i; printf("%lf\t%lf\t%lf\t%lf\t%lf\n",xi,yi,f(xi,yi),h*f(xi,yi),y);
if(F(l1)==0) //We check if the initail approximations are the root or not for(j=0;j<n;j++) yi=y;
r=l1; { xi=xi+h; }
else if(k==j) printf("%lf\t%lf\n",xi,yi);
if(F(l2)==0) { printf("_____________________________________________________________\n");
r=l2; continue; printf("The value of y is %lf\n\n",y);
else{ } }
while(ctr <= iter) else
{//this is an implementation of the algorithm mentioned above { RK #include<stdio.h>
f1=F(l1); temp = temp * ((p-x[j])/(x[k]-x[j])); #include<conio.h>
r=(l1+l2)/2.0; } #include<math.h>
f2=F(r); } float f(float x,float y) {
f3=F(l2); f[i]=y[i]*temp; return (x*x+y); }
if(f2==0){ } int main() {
r=f2; for(i=0;i<n;i++) clrscr();
break; } { float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;
printf("The root after %d iteration is %lf\n",ctr,r); sum = sum + f[i]; printf("Enter x0,y0,xn,h:");
if(f1*f2<0) } scanf("%f %f %f %f",&x0,&y0,&xn,&h);
l2=r; printf("\n\n f(x)=%f",sum); x=x0;
else getch(); y=y0;
if(f2*f3<0) } printf("\n\nX\t\tY\n");
l1=r; while(x<xn) {
ctr++; m1=f(x0,y0);
} m2=f((x0+h/2.0),(y0+m1*h/2.0));
} m3=f((x0+h/2.0),(y0+m2*h/2.0));
printf("The approximation to the root is %lf",r); m4=f((x0+h),(y0+m3*h));
getch(); m=((m1+2*m2+2*m3+m4)/6);
} y=y+m*h;
x=x+h;
printf("%f\t%f\n",x,y); }
getch(); }
}