Posts

Showing posts with the label C example

Write a C program on Newton forward interpolation method by MistarAV

#include<stdio.h> #define MAXN 100 #define ORDER 4 int main() { float ax[MAXN+1], ay [MAXN+1], diff[MAXN+1][ORDER+1], nr=1.0, dr=1.0,x,p,h,yp; int n,i,j,k; printf("\nEnter the value of n:\n"); scanf("%d",&n); printf("\nEnter the values in form x,y:\n"); for (i=0;i<=n;i++) scanf("%f %f",&ax[i],&ay[i]); printf("\nEnter the value of x for which the value of y is wanted: \n"); scanf("%f",&x); h=ax[1]-ax[0]; //now making the difference table //calculating the 1st order of differences for (i=0;i<=n-1;i++) diff[i][1] = ay[i+1]-ay[i]; //now calculating the second and higher order differences for (j=2;j<=ORDER;j++) for(i=0;i<=n-j;i++) diff[i][j] = diff[i+1][j-1] - diff[i][j-1]; //now finding x0 i=0; while (!(ax[i]>x)) i++; //now ax[i] is x0 and ay[i] is y0 i--; p = (x-ax[i])/

Pattern in C Language By MistarAV

C Examples Half pyramid of * Half pyramid of numbers Half pyramid of alphabets Inverted half pyramid of * Inverted half pyramid of numbers Full pyramid of * Full pyramid of numbers Inverted full pyramid of * Pascal's triangle Floyd's triangle Example 1: Half Pyramid of * * * * * * * * * * * * * * * * C Program Copy #include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("\n"); } return 0; } Example 2: Half Pyramid of Numbers 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 C Program #include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= row

Write a C program on Newton forward interpolation method by MistarAV

#include<stdio.h> #include<math.h> int fact(int); void main() { float arr[10][11],x,h,p,y,px=1; int i,j,n,ch=30; printf("\nEnter the number of data:"); scanf("%d",&n); printf("\nEnter the data"); for(i=0;i<n;i++) { printf("X%d=",i+1); scanf("%f",&arr[i][0]); printf("Y%d=",i+1); scanf("%f",&arr[i][1]); } //Forming difference table. for(j=2;j<=n;j++) for(i=0;i<n-1;i++) arr[i][j]=arr[i+1][j-1]-arr[i][j-1]; //Printing table printf("\nDifference table is:-"); printf("\n\tx\tY"); for(i=0;i<=n-2;i++) printf("\t%c^%dY",ch,i+1); for(i=0;i<n;i++) {printf("\n"); for(j=0;j<n+1-i;j++) {printf("\t%.4f",arr[i][j]); } } //Take the value of x for f(x) printf("\nEnter the value x for function f(x):"); scanf("%f",&x); //Calculate the value of f(x) for x h=arr[1][0]-arr[0][0]; p=(x-arr[0][0])/h; y=arr[0][1]; for(i=1;i<n

Find the root of the equation using the Bisection method by MistarAV

#include<stdio.h> #include<conio.h> #include<math.h> /* Defining equation to be solved. Change this equation to solve another problem. */ #define f(x) cos(x) - x * exp(x) void main() { float x0, x1, x2, f0, f1, f2, e; int step = 1; clrscr(); /* Inputs */ up: printf("\nEnter two initial guesses:\n"); scanf("%f%f", &x0, &x1); printf("Enter tolerable error:\n"); scanf("%f", &e); /* Calculating Functional Value */ f0 = f(x0); f1 = f(x1); /* Checking whether given guesses brackets the root or not. */ if( f0 * f1 > 0.0) { printf("Incorrect Initial Guesses.\n"); goto up; } /* Implementing Bisection Method */ 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( f0 * f2 < 0) { x1 = x2; f1 = f2; } else { x0 = x2; f0 = f2; } step = step + 1; }while(fabs(f2)>e); printf("\nRoot is: %f", x2); get

Find the root of the equation using the Regula-Falsi method by MistarAV

#include<stdio.h> #include<conio.h> #include<math.h> /* Defining equation to be solved. Change this equation to solve another problem. */ #define f(x) x*log10(x) - 1.2 int main() { float x0, x1, x2, f0, f1, f2, e; int step = 1; clrscr(); /* Inputs */ up: printf("\nEnter two initial guesses:\n"); scanf("%f%f", &x0, &x1); printf("Enter tolerable error:\n"); scanf("%f", &e); /* Calculating Functional Values */ f0 = f(x0); f1 = f(x1); /* Checking whether given guesses brackets the root or not. */ if( f0*f1 > 0.0) { printf("Incorrect Initial Guesses.\n"); goto up; } /* Implementing Regula Falsi or False Position Method */ printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do { x2 = x0 - (x0-x1) * f0/(f0-f1); f2 = f(x2); printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2); if(f0*f2 < 0) { x1 = x2; f1 = f2; } else { x0 = x2; f0 = f2; } step = step + 1; }while(fabs(f2)>e); printf("\n

Write a C program on Cubic spline interpolation method by MistarAV

#include<stdio.h> #include<conio.h> #include<math.h> void main(){ clrscr(); char choice='y'; int n,i,j,k; float h[10],a,b,c,d,sum,s[10]={0},x[10],F[10],f[10],p,m[10][10]={0},temp; printf("No of samples? "); scanf("%d",&n); printf("\nEnter all sample points: "); for(i=0;i<n;i++) scanf("%f%f",&x[i],&f[i]); for(i=n-1;i>0;i--){ F[i]=(f[i]-f[i-1])/(x[i]-x[i-1]); h[i-1]=x[i]-x[i-1]; } //*********** formation of h, s , f matrix **************// for(i=1;i<n-1;i++){ m[i][i]=2*(h[i-1]+h[i]); if(i!=1){ m[i][i-1]=h[i-1]; m[i-1][i]=h[i-1]; } m[i][n-1]=6*(F[i+1]-F[i]); } //*********** forward elimination **************// for(i=1;i<n-2;i++){ temp=(m[i+1][i]/m[i][i]); for(j=1;j<=n-1;j++) m[i+1][j]-=temp*m[i][j]; } //*********** backward substitution *********// for(i=n-2;i>0;i--){ sum=0; for(j=i;j<=n-2;j++) sum+=m[i][j]*s[j]; s[i]=(m[i][n-1]-sum)/m[i][i]; } while(choice=='y'){ printf("\nEnte

Write a C program on Lagrange's interpolation method by MistarAV

 #include<stdio.h> #include<conio.h> void main() { float x[100], y[100], xp, yp=0, p; int i,j,n; clrscr(); /* Input Section */ printf("Enter number of data: "); scanf("%d", &n); printf("Enter data:\n"); for(i=1;i<=n;i++) { printf("x[%d] = ", i); scanf("%f", &x[i]); printf("y[%d] = ", i); scanf("%f", &y[i]); } printf("Enter interpolation point: "); scanf("%f", &xp); /* Implementing Lagrange Interpolation */ for(i=1;i<=n;i++) { p=1; for(j=1;j<=n;j++) { if(i!=j) { p = p* (xp - x[j])/(x[i] - x[j]); } } yp = yp + p * y[i]; } printf("Interpolated value at %.3f is %.3f.", xp, yp); getch(); } Output Enter number of data: 5 ↲ Enter data: x[1] = 5 ↲ y[1] = 150 ↲ x[2] = 7 ↲ y[2] = 392 ↲ x[3] = 11 ↲ y[3] = 1452 ↲ x[4] = 13 ↲ y[4] = 2366 ↲ x[5] = 17 ↲ y[5] = 5202 ↲ Enter interpolation point: 9 ↲ Interpolated value at 9.000 is 810.000. Note: ↲ indicates ENTER is pr

Write a C program to numerical solution of ODE using Runge-Kutta method by MistarAV

 #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 Enter Initial Condition x0 = 0

Write a C program for numerical integration using trapezoidal rules by MistarAV

 #include<stdio.h> #include<conio.h> #include<math.h> /* Define function here */ #define f(x) 1/(1+pow(x,2)) 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; integration = integration + 2 * f(k); } integration = integration * stepSize/2; printf("\nRequired value of integration is: %.3f", integration); getch(); return 0; } Output Enter lower limit of integration: 0 Enter upper limit of integration: 1 Enter number of sub interv

Write a C program to numerical integration using Simpson's 1/3rd rule by MistarAV

 #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 Enter lower limit of integration: 0 Enter

Write a C program on Cubic spline interpolation method by MistarAV

 #include<stdio.h> #include<conio.h> #include<math.h> void main(){ clrscr(); char choice='y'; int n,i,j,k; float h[10],a,b,c,d,sum,s[10]={0},x[10],F[10],f[10],p,m[10][10]={0},temp; printf("No of samples? "); scanf("%d",&n); printf("\nEnter all sample points: "); for(i=0;i<n;i++) scanf("%f%f",&x[i],&f[i]); for(i=n-1;i>0;i--){ F[i]=(f[i]-f[i-1])/(x[i]-x[i-1]); h[i-1]=x[i]-x[i-1]; } //*********** formation of h, s , f matrix **************// for(i=1;i<n-1;i++){ m[i][i]=2*(h[i-1]+h[i]); if(i!=1){ m[i][i-1]=h[i-1]; m[i-1][i]=h[i-1]; } m[i][n-1]=6*(F[i+1]-F[i]); } //*********** forward elimination **************// for(i=1;i<n-2;i++){ temp=(m[i+1][i]/m[i][i]); for(j=1;j<=n-1;j++) m[i+1][j]-=temp*m[i][j]; } //*********** backward substitution *********// for(i=n-2;i>0;i--){ sum=0; for(j=i;j<=n-2;j++) sum+=m[i][j]*s[j]; s[i]=(m[i][n-1]-sum)/m[i][i]; } while(choice=='y'){ printf("\nEnt

Find the root of the equation using Newton reperson method by MistarAV

 #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> /* Defining equation to be solved. Change this equation to solve another problem. */ #define f(x) 3*x - cos(x) -1 /* Defining derivative of g(x). As you change f(x), change this function also. */ #define g(x) 3 + sin(x) void main() { clrscr (); float x0, x1, f0, f1, g0, e; int step = 1, N; /* Inputs */ printf("\nEnter initial guess:\n"); scanf("%f", &x0); printf("Enter tolerable error:\n"); scanf("%f", &e); printf("Enter maximum iteration:\n"); scanf("%d", &N); /* Implementing Newton Raphson Method */ printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n"); do { g0 = g(x0); f0 = 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,f0,x1,f1); x0 = x1; step = step+1; if(step > N) { printf("Not Convergent."); exit(0); }

Write a C program on Numerical solution of ODE using Euler method by MistarAV

 #include<stdio.h> #include<conio.h> #define f(x,y) x+y int main() { clrscr(); float x0, y0, xn, h, yn, slope; int i, n; 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(); } Output Enter Initial Condition x0 = 0 y0 = 1 Enter calculation point xn = 1 Enter number of steps: 10 x0 y0 slope yn -------