
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
REAL TIME Sleep Drowsiness Detection
Typology: Thesis
1 / 24
This page cannot be seen from the preview
Don't miss anything!
//Write a program to implement bisection method #include<stdio.h> #include<conio.h> #include<math.h> #define max 10 float f(float x); void main() { float a,b,c,d,t; int i,j,n; clrscr(); printf("\nEnter the number of iteration : "); scanf("%d",&n); for(i=-max;i<max;i++) { a=i; b=i+1; if(f(a)f(b)<=0) { if(f(a)>f(b)) {t=a;a=b;b=t;} printf("\nThe initial approximate limits are : "); printf("%f%f",a,b); printf("\n\n a b c f(c)"); for(j=0;j<n;j++) { c=(a+b)/2; printf("\n\n %10.6f %10.6f %10.6f %10.6f",a,b,c,f(c)); if(f(c)<0) a=c;else b=c; } getch(); } } } float f(float x) { return(cos(x)-3x+1);
//Write a program to implement Regular Falsi method #include<stdio.h> #include<conio.h> #include<math.h> float f(float x) { return cos(x) - x*exp(x); } void regula (float *x, float x0, float x1, float fx0, float fx1, int itr) { x = x0 - ((x1 - x0) / (fx1 - fx0))fx0; ++(itr); printf("Iteration no. %3d X = %7.5f \n", *itr, x); } int main () { int itr = 0, maxmitr; float x0,x1,x2,x3,allerr; clrscr(); printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n"); scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr); regula (&x2, x0, x1, f(x0), f(x1), &itr); do { if (f(x0)f(x2) < 0) x1=x2; else x0=x2; regula (&x3, x0, x1, f(x0), f(x1), &itr); if (fabs(x3-x2) < allerr) { printf("After %d iterations, root = %6.4f\n", itr, x3); getch(); return 0; } x2=x3; } while (itr<maxmitr); printf("Solution does not converge or iterations not sufficient:\n"); getch(); return 1;
//Write a program to implement secant method #include<stdio.h> #include<conio.h> #include<math.h> float f(float x) { return(xxx-4); } float main() { float a,b,c,d,e; int count=1,n; clrscr(); printf("\nEnter the values of a and b : "); scanf("%f%f",&a,&b); printf("Enter the values of allowed error and maximum number : "); scanf("%f%d",&e,&n); do { if(f(a)==f(b)) { printf("\nSolution cannot be found as the values of a and b"); return 0; } c=(af(b)-bf(a))/(f(b)-f(a)); a=b; b=c; printf("Iteration No-%d x=%f\n",count,c); count++; if(count==n) { break; } } while(fabs(f(c))>e); printf("\nThe Required solution is %f\n",c); getch();
//Write a program to implement Newton Rapshon method #include<stdio.h> #include<conio.h> #include<math.h> float f(float x) { return(cos(x)-3x+1); } float f1(float y) { return(-sin(y)-3); } void main() { float a,b,c,d; int i,j,n; clrscr(); printf("\nEnter the number of iterations : "); scanf("%d",&n); for(i=-8;i<8;i++) { a=1; b=i+1; if(f(a)f(b)<=0) { printf("\nThe initial approximate value is : "); c=(a+b)/2.0; printf("%f",c); printf("\nn c f(c)"); for(j=0;j<n;j++) { printf("\n%-5d%15.6f%15.6f",j+1,c,f(c)); d=c-(f(c)/f1(c)); c=d; } getch(); } }}
s += mat[i][j]out[j]; } s=mat[i] [n]-s; out[i]=s/mat[i][j]; } for(i=0;i<n;i++) { gotoxy(15i+10,21); printf("%c = %-10.3f",'z'-n+i+1,out[i]); } getch(); getch(); } void matrix(float mat[10][11],int n) { int i=0,j=0; do { if((mat[i][i]!=mat[j][i]) && (mat[i][i]==0) && (j<n)) swap(mat,n,i,j); j++; if(j%n==0) j=++i; }while(i<n); } void swap(float mat[10][11],int n,int r1,int r2) { float temp; int i; for(i=0;i<=n;i++) { temp=mat[r1][i]; mat[r1][i]=mat[r2][i]; mat[r2][i]=temp; }
//Write a program to implement Euler's method #include<stdio.h> #include<conio.h> #include<math.h> float f(float x,float y) { return(yy-xx); } void main() { float h,x,y,y0,x0,y1; clrscr(); printf("\nenter initial approximate x0,y0&interval width h:"); scanf("%f%f%f",&x0,&y0,&h); printf("enter the value of x at which y is required:"); scanf("%f",&x); printf("\n\t x \t y"); do { y=h*f(x0,y0); y1=y+y0; printf("\n%f%f",x0,y1); x0=x0+h; y0=y1; } while(x0<x); getch();
//Write a program to implement Simpson 1/3 rule #include<stdio.h> #include<conio.h> #include<math.h> float f(float x); void main() { float k=0.0,h,x,a,b; int i,n; clrscr(); printf("the function f(x):1/(1+xx)"); printf("\nenter the lower and upper limits="); printf("\na="); scanf("%f",&a); printf("b="); scanf("%f",&b); printf("enter the number of strips(in multiple of '2') = "); scanf("%d",&n); h=(b-a)/(2(n/2)); printf("\n x f(x)"); for(x=a,i=0;x<b;x=x+2h,i=i+2) { k+=f(x)+4f(x+h)+f(x+2h); printf("\n%.6f%15.6f",x,f(x)); printf("\n%.6f%15.6f",x+h,f(x+h)); } printf("\n\nthe definite integral is %15.6f",(h/3.0)k); getch(); } float f(float x) { return(1/(1+x*x));
//Write a program to implement Simpson 3/8 rule #include<stdio.h> #include<conio.h> #include<math.h> float f(float x); void main() { float k=0.0,h,x,a,b; int i,n; clrscr(); printf("the function f(x):1/(1+xx)"); printf("\nenter the lower and upper limits="); printf("\na="); scanf("%f",&a); printf("b="); scanf("%f",&b); printf("enter the number of strips(in multiple of '2') = "); scanf("%d",&n); h=(b-a)/(2(n/2)); printf("\n x f(x)"); for(x=a,i=0;x<b;x=x+3h,i=i++) { k+=f(x)+3f(x+h)+3f(x+2h)+f(x+3h); printf("\n%.6f%15.6f",x,f(x)); printf("\n%.6f%15.6f",x+h,f(x+h)); printf("\n%.6f%15.6f",x+2h,f(x+2h)); } printf("\n %f%15.6f",x,f(x)); printf("\n\nthe definite integral is %15.6f",(3h/8.0)k); getch(); } float f(float x) { return(1/(1+xx));
//Write a program to implement Gauss seidal method #include<stdio.h> #include<conio.h> #include<math.h> int check_matrix(float[10][11],int); void clear(); void main() { float mat[10][11],temp,out[10]={0},ratio,o,s; int i,j,k=0,n,ck,flag; clrscr(); gotoxy(10,3); cprintf("Enter order of matrix : "); cscanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n+1;j++) { gotoxy(10j+10,i+5); if(j<n)cprintf("%c",'z'-n+j+1); cscanf("%f",&mat[i][j]); gotoxy(16+10j,j+5); if(j<n-1)cprintf("+"); else if(j==n-1)cprintf("="); } ck=check_matrix(mat,n); if(ck==-1) { gotoxy(10,15); cprintf("The given equation are not valid for gauss seidal method"); } gotoxy(10,10); cprintf("A table for gauss seidal method:-"); for(j=0;j<n+1;j++) { gotoxy(5j+3,12); if(j<n)cprintf("%c",'z'-n+j+1); } do { flag=1; if(k%8==0)clear(); for(i=0;i<n;i++) { temp=out[i]; s=0; for(j=0;j<n;j++) { if(j!=1) { s+=out[j]*mat[i][j]; }
out[i]=(mat[i][n]-s)/mat[i][j]; gotoxy(10+15*i,13+k%8); cprintf("%-f",out[i]); if(fabs(temp-out[i])>0.000001)flag=0; } k++; } while(flag==0); getch(); } int check_matrix(float mat[10][11],int n) { int i,j; float sum=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i!=j)sum+=abs(mat[i][j]/mat[i][j]); } if(sum>1)return-1; } return 0; } void clear() { int i,j; gotoxy(15,22); cprintf("Press any key to see more..."); getch(); for(i=10;i<70;i++) for(j=13;j<21;j++) { gotoxy(i,j); printf(" "); }
for(z=0;x[z]<=x0;z++); u=(float)(x0-x[i])/(x[1]-x[0]); v=u; v1=u-1; t=v1; ans=ans+((y[i]+y[i+2]/2)); z=1; for(j=0;j<n-1;j++) { g=h=g1=h1=1; if(j==0) { if(j%2==0) { u=u(v+g); g++; t=t(v1-h1); h1++; } else { t=t(v1+g1); g1++; u=u(v-h); h++; } } ans1=(udiff[i][j]/fact(j+1)); ans2=(tdiff[z][j]/fact(j+1)); ans=ans+(ans1+ans2)/2; if(j%2==0) i--; else z--;} printf("\n\n the value of y when x=%f is y=%f",x0,ans); getch();
//Write a program to implement Newton Gregory forward interpolation formula #include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,j,n; float xy[10][11],h,p,px=1,x,y; char str[80]; clrscr(); printf("Enter the number of data : "); scanf("%d",&n); printf("\nEnter the data : \n"); for(i=0;i<n;i++) { printf("x(%d) and y(%d) : ",i+1,i+1); scanf("%f%f",&xy[i][0],&xy[i][1]); } for(j=2;j<n+1;j++) for(i=0;j<n-1;j++) xy[i][j]=xy[i+1][j-1]-xy[i][j-1]; printf("\nThe diference table is :- "); printf("\nx f(x) "); for(i=0;i<n-1;i++) printf("?^%d ",i+1); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n+1-i;j++) { printf("%.4f ",xy[i][j]); } } printf("\nEnter the value of 'x' : "); scanf("%f",&x); h=xy[i][0]-xy[0][0]; p=(x-xy[0][0])/h; y=xy[0][1]; for(i=1;i<n;i++) { px=(p-(i-1))/i; y+=xy[0][i+1]px; } printf("\n The value of function at x =%f is %f ",x,y); getch();
//Write a program to implement gauss forward interpolation formula #include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,j,n; float ax[10],ay[10],xy[20][20],diff[20][20]; float y1,y2,y3,y4,x,y=0,h,p,nr,dr; clrscr(); printf("\nEnter the number of terms : "); scanf("%d",&n); printf("\nEnter the value in the form of x and y :- \n"); for(i=0;i<n;i++) { printf("Enter the value of x%d and y%d :- ",i,i); scanf("%f %f ",&ax[i],&ay[i]); } printf("\nEnter the value of x for which you want value of y :- "); scanf("%f",&x); h=ax[1]-ax[0]; for(i=0;i<n-1;i++) { xy[i][0]=ax[i]; xy[i][0]=ay[i]; } for(i=0;i<n-1;i++) { diff[i][1]=ay[i+1]-ay[i]; } for(j=2;j<=4;j++) { for(i=0;i<n-j;i++) { diff[i][j]=diff[i+1][j-1]-diff[i][j-1]; } } for(j=2;j<=n+1;j++) for(i=0;i<=n-1;i++) xy[i][j]=xy[i+1][j-1]-xy[i][j-1]; printf("The difference table is :- "); printf("\nx f(x) "); for(i=0;i<n-1;i++) printf("^%d :",i+1); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n+1-i;j++); printf("%.4f",xy[i][j]); } i=0; do {
i++;} while(ax[i]<x); i--; p=(x-ax[i])/h; y1=pdiff[i][1]; y2=p(p-1)diff[i-1][2]/2; y3=(p+1)p(p-1)diff[i-2][3]/6; y4=(p+1)p(p-1)(p-2)diff[i-3][4]/24; y=ay[i]+y1+y2+y3+y4; printf("\n When x = %6.4f,y = %6.8f",x,y); printf("\n\n\n press enter to exit"); getch();