Write A Program To Find The Roots of Non-Linear Equation Using Newton-Raphson Method
Write A Program To Find The Roots of Non-Linear Equation Using Newton-Raphson Method
Write A Program To Find The Roots of Non-Linear Equation Using Newton-Raphson Method
35/ECE/10
Keshav sudial PROGRAM - 1 Write a program to find the roots of non-linear equation using Newton-Raphson method.
#include<stdio.h> #include<conio.h> #include<math.h> float f(float x) { return x*log10(x)-1.2; } float df(float x) { return log10(x)+0.4329; } void main() { int itr,maxitr; float h,x0,x1,err; printf("Enter the value of x0,allowed error,maximum iteration\n"); scanf("%f%f%d",&x0,&err,&maxitr); for(itr=1;itr<=maxitr;itr++) { h=f(x0)/df(x0); x1=x0-h; printf("Number %d,x1=%f\n",itr,x1); if(fabs(h)<err) { printf("After %d iterations,Root=%f\n",itr,x1); } x0=x1; } printf("Iteration number sufficient, solution does not converge\n"); getch(); }
Keshav sudial PROGRAM - 2 Write a program to integrate numerically using Simpsons rule.
#include<stdio.h> #include<conio.h> float y(float x) { return 1/(1+x*x); } void main() { float xo,xn,h,s; int i,n; clrscr(); printf("\nEnter the value of xo,xn & no. of intervals"); scanf("%f%f%d",&xo,&xn,&n); h=(xn-xo)/n; s=y(xo)+y(xn)+4*y(xo+h); for(i=3;i<=n-1;i+=2) s=s+4*y(xo+i*h)+2*y(xo+(i-1)*h); printf("Value of integral is %f\n",(h/3)*s); getch(); }
Keshav sudial PROGRAM - 3 Write a program to integrate numerically using Trapezoidal rule.
#include<stdio.h> #include<conio.h> float y(float x) { return 1/(1+x*x); } void main() { float x0,xn,h,s; int i,n; clrscr(); printf("Enter the values of x0,xn & number of subinterval"); scanf("%f%f%d",&x0,&xn,&n); h=(xn-x0)/n; s=y(x0)+y(xn); for(i=1;i<n;i++) s+=2*y(x0+i*h); printf("Value of integral is %f\n",(h/2)*s); getch(); }
Keshav sudial PROGRAM 4 Write a program to solve the system of linear equations using Gauss-Elimination method.
#include<stdio.h> #define N 3 main() { float a[N][N+1],x[N],t,s; int i,j,k; printf("\nEnter the elements of the augmented matrix row wise\n"); for(i=0;i<N;i++) for(j=0;j<N+1;j++) scanf("%f",&a[i][j]); for(j=0;j<N-1;j++) for(i=j+1;i<N;i++) { t=a[i][j]/a[j][j]; for(k=0;k<N+1;k++) a[i][k]-=a[j][k]*t; } printf("\nThe upper triangular matrix is : \n"); for(i=0;i<N;i++) { for(j=0;j<N+1;j++) printf(" %f",a[i][j]); printf("\n"); } for(i=N-1;i>=0;i--) { s=0; for(j=i+1;j<N;j++) s+=a[i][j]*x[j]; x[i]=(a[i][N]-s)/a[i][j]; } printf("\nThe solution is :\n"); for(i=0;i<N;i++) { printf("x[%d]=%f\n",i+1,x[i]); } }
Keshav sudial PROGRAM - 5 Write a program to solve the system of linear equations using Gauss-Jordan method.
#include<stdio.h> #include<conio.h> #define N 3 void main() { float a[N][N+1],t; int i,j,k; clrscr(); printf("\nEnter the elements of augmented matrix so wise\n"); for(i=0;i<N;i++) for(j=0;j<N+1;j++) scanf("%f",&a[i][j]); for(j=0;j<N;j++) for(i=0;i<N;i++) if(i!=j) { t=a[i][j]/a[j][j]; for(k=0;k<N+1;k++) a[i][k]-=a[j][k]*t; } printf("\nThe diagonal matrix is\n"); for(i=0;i<N;i++) { for(j=0;j<N+1;j++) printf("%f ",a[i][j]); printf("\n"); } printf("\nThe solution is\n"); for(i=0;i<N;i++) printf("x[%d]=%f\n",i+1,a[i][N]/a[i][i]); getch(); }
Keshav sudial PROGRAM - 6 Write a program to find numerical solution of ordinary differential equation by Eulers method.
#include<stdio.h> #include<conio.h> float df(float x,float y) { return (x+y); } void main() { float xo,yo,h,x,x1,y1; printf("\nEnter the value of xo,yo,h,x"); scanf("%f%f%f%f",&xo,&yo,&h,&x); x1=xo; y1=yo; while(1) { if(x1>x)return; y1+=h*df(x1,y1); x1+=h; printf("When x=%f,y=%f\n",x1,y1); } getch(); }
Keshav sudial PROGRAM - 7 Write a program to find numerical solution of ordinary differential equation by Runga-Kutta method.
#include<stdio.h> #include<conio.h> float f(float x,float y) { return x+y*y; } void main() { float xo,yo,h,xn,x,y,k1,k2,k3,k4,k; printf("\nEnter the value of xo,yo,h,xn"); scanf("%f%f%f%f",&xo,&yo,&h,&xn); x=xo; y=yo; while(6) { if(x==xn) break; k1=h*f(x,y); k2=h*f(x+h/2,y+k1/2); k3=h*f(x+h/2,y+k2/2); k4=h*f(x+h,y+k3); k=(k1+(k2+k3)*2+k4)/6; x=x+h; y=y+k; printf("\nWhen x=%f,then y=%f",x,y); } getch(); }
Keshav sudial PROGRAM - 8 Write a program to find solution using Lagranges method.
#include<stdio.h> #include<conio.h> #define MAX 100 void main() { float ax[MAX+1],ay[MAX+1],nr,dr,x,y=0; int i,j,n; clrscr(); printf("\nEnter the value of n\n"); scanf("%d",&n); printf("\nEnter the set of value of ax,ay\n"); for(i=0;i<=n;i++) scanf("%f%f",&ax[i],&ay[i]); printf("\nEnter the value of x at which y is wanted"); scanf("%f",&x); for(i=0;i<=n;i++) { nr=dr=1; for(j=0;j<=n;j++) if(j!=i) { nr*=x-ax[j]; dr*=ax[i]-ax[j]; } y+=(nr/dr)*ay[i]; } printf("\nWhen x=%f,y=%f\n",x,y); getch(); }
Keshav sudial PROGRAM 9 Write a program to solve the system of linear equations using Gauss-Seidal iteration method.
#include<stdio.h> #include<conio.h> #include<math.h> #define N 3 main() {int i,j,itr,maxitr; float a[N][N+1],x[N],maxerr,aerr,t,s,err; for(i=0;i<N;i++) x[i]=0; printf("Enter the element of the augmented matrix row wise\n"); for(i=0;i<N;i++) for(j=0;j<N+1;j++) scanf("%f",&a[i][j]); printf("Enter the value of allowed error and maximum number of iteration\n"); scanf("%f%d",&aerr,&maxitr); printf("Iteration x[1] x[2] x[3] \n"); for(itr=1;itr<=maxitr;itr++) {maxerr=0; for(i=0;i<N;i++) {s=0; for(j=0;j<N;j++) if(j!=i) s+=a[i][j]*x[j]; t=(a[i][N]-s)/a[i][i]; err=fabs(x[i]-t); if(err>maxerr) maxerr=err; x[i]=t;} printf(" %d",itr); for(i=0;i<N;i++) printf(" %f",x[i]); printf("\n"); if(maxerr<aerr) {printf("Converge in %d iteration\n",itr); for(i=0;i<N;i++) printf("x[%d]=%f\n",i+1,x[i]); return 0;}} printf("\nSolution does not converge,iteration not sufficient\n"); return 1;}
Keshav sudial PROGRAM 10 Write a program to study Newtons divided difference formula.
#include<stdio.h> #include<conio.h> void main() { int x[10],y[10],p[10]; int k,f,n,i,j=1,f1=1,f2=0; printf("Enter the number of observation\n"); scanf("%d",&n); printf("Enter the different values of x\n"); for(i=1;i<=n;i++) { scanf("%d",&x[i]); } printf("Enter the corresponding value of y\n"); for(i=1;i<=n;i++) { scanf("%d",&y[i]); } f=y[1]; printf("Enter the value of 'k' in f(k) which we want to evaluate\n"); scanf("%d",&k); do { for(i=1;i<=n;i++) { p[i]=((y[i+1]-y[i])/(x[i+j]-x[i])); y[i]=p[i]; } f1=1; for(i=1;i<=j;i++) {f1*=(k-x[i]);} f2+=(y[1]*f1); n--; j++; } while(n!=1); f+=f2; printf("f[%d]=%d",k,f); getch(); }