Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include <conio.h>
- int ccout(double xi, int i, clock_t stop, clock_t start);
- void reserveData(void);
- double reserveXi, UnlinearFuncData, reserveTime;
- int reserveI;
- double unlinear_func(double x)
- {
- double f;
- f = pow(x,3)-3;
- //f = pow(x,3)+2*x;
- //f = pow(x,3)-x+1;
- return f;
- }
- double chords(double x1, double x2)
- {
- return (unlinear_func(x2)*x1-unlinear_func(x1)*x2)/(unlinear_func(x2)-unlinear_func(x1));
- }
- double bisection(double x1, double x2)
- {
- return (x1+x2)/2;
- }
- int iter(int imax, int i, double xi, double x1, double x2, int ch, int deb, double e, clock_t start)
- {
- int ich;
- clock_t stop;
- if (i>imax)
- {
- printf("\nExceeded max number of i(iterations)!\n\nWhat we should do?\n");
- printf("1.Continue with the same number of i\n2.Run to find a solution\n3.Exit and print current result\nYour choose: ");
- scanf("%d", &ich);
- switch(ich)
- {
- case 1:
- return i-imax;
- break;
- case 2:
- if (ch==1)
- {
- do
- {
- xi=chords(x1, x2);
- if (deb==1)
- {
- stop=clock();
- ccout(xi, i, stop, start);
- printf("x1=%lf x2=%lf\n", x1, x2);
- }
- i++;
- if (unlinear_func(xi)*unlinear_func(x1)<0) x2=xi;
- else
- {
- if (unlinear_func(xi)*unlinear_func(x2)<0) x1=xi;
- }
- }
- while (e<fabs(unlinear_func(xi)));
- stop=clock();
- printf("\n(!)Final: \n");
- stop=clock();
- ccout(xi, i=0, stop, start);
- exit(0);
- }
- else
- {
- if (ch==2)
- {
- do
- {
- xi=bisection(x1, x2);
- if (deb==1)
- {
- stop=clock();
- ccout(xi, i, stop, start);
- printf("x1=%lf x2=%lf\n", x1, x2);
- }
- i++;
- if (unlinear_func(xi)<0) x1=xi;
- else
- {
- if (unlinear_func(xi)>0) x2=xi;
- }
- }
- while(e<fabs(x1-x2));
- printf("\n(!)Final: \n");
- stop=clock();
- ccout(xi, i=0, stop, start);
- exit(0);
- }
- }
- break;
- case 3:
- reserveData();
- //stop=clock(); Old
- //ccout(xi, i=0, stop, start);
- exit(0);
- break;
- default:
- printf("\nBad answer!");
- exit(0);
- break;
- }
- }
- }
- int ccout(double xi, int i, clock_t stop, clock_t start)
- {
- reserveXi = xi;
- UnlinearFuncData = unlinear_func(xi);
- reserveTime = (stop-start)/CLK_TCK;
- printf("\n\t\t\t\t.::-Result-::.");
- printf("\nRoot value(Xi) = %.8lf", xi);
- printf("\nFunction value = %.8lf", UnlinearFuncData);
- if (i==0) ;
- else
- {
- reserveI = i;
- printf("\nIterations value = %d", reserveI);
- }
- printf("\nElapsed time: %.10f seconds\n", reserveTime);
- return 101;
- }
- void reserveData(void) {
- printf("\n\t\t\t\t.::-Result-::.");
- printf("\nRoot value(Xi) = %.8lf", reserveXi);
- printf("\nFunction value = %.8lf", UnlinearFuncData);
- if (reserveI==0);
- else
- {
- printf("\nIterations value = %d", reserveI);
- }
- printf("\nElapsed time: %.10f seconds\n", reserveTime);
- }
- int repeat(int povt)
- {
- char Y_n;
- printf("\nDo you want to start program again?(Y/n)");
- fflush(stdin);
- scanf("%c", &Y_n);
- switch (Y_n)
- {
- case 'Y':
- povt=1;
- printf("Ok, press something...");
- break;
- case 'y':
- povt=1;
- printf("Ok, press something...");
- break;
- case 'N':
- return 0;
- case 'n':
- return 0;
- default:
- printf("Wrong answer. Program will finish...\n");
- return 0;
- }
- return povt;
- }
- int main()
- {
- double x1, x2, e, xi;
- int i=0, imax, ch, povt=0, prvr, deb;
- clock_t start, stop;
- printf("\t\t+++_Calculating_of_nonlinear_equation_+++\n\n");
- do
- {
- if (povt==1)
- {
- getch();
- system("cls");
- }
- povt=0;
- do
- {
- prvr=0;
- printf("Enter x1: ");
- fflush(stdin);
- scanf("%lf", &x1);
- printf("Enter x2: ");
- fflush(stdin);
- scanf("%lf", &x2);
- if (unlinear_func(x1)*unlinear_func(x2)>0)
- {
- printf("Wrong x1 or x2. Try again!\n");
- prvr=1;
- }
- }
- while(prvr>0);
- do
- {
- prvr=0;
- printf("Enter allowable error(E): ");
- fflush(stdin);
- scanf("%lf", &e);
- if (e<pow(10,-5) || e>pow(10,-3))
- {
- printf("E must be from 10^-5 to 10^-3 Try again!\n");
- prvr=1;
- }
- }
- while(prvr>0);
- do
- {
- prvr=0;
- printf("Enter max number of iterations(i): ");
- fflush(stdin);
- scanf("%d", &imax);
- if (imax<=0)
- {
- printf("Max 'i' must be more then 0! Try again\n");
- prvr=1;
- }
- }
- while(prvr>0);
- printf("\nSelect method:\n\n1.Chords\n2.Bisection\nYour choose will be: ");
- fflush(stdin);
- scanf("%d", &ch);
- switch(ch)
- {
- case 1:
- printf("\n\t\t\tSelected method of chords\n\n");
- printf("Activate debugging mod?(1-yes/2-no): ");
- scanf("%d", &deb);
- if (deb==1) printf("\n\t\t\t\tDebugging mod status: [ON]\n\n");
- else printf("\n\t\t\t\tDebugging mod status: [OFF]\n\n");
- start=clock();
- do
- {
- xi=chords(x1, x2);
- i++;
- i=iter(imax, i, xi, x1, x2, ch, deb, e, start);
- if (deb==1)
- {
- ccout(xi, i, stop, start);
- printf("x1=%lf x2=%lf\n", x1, x2);
- }
- if (unlinear_func(xi)*unlinear_func(x1)<0) x2=xi;
- else
- {
- if (unlinear_func(xi)*unlinear_func(x2)<0) x1=xi;
- }
- }
- while (e<fabs(unlinear_func(xi))); // f(xi)<=E --> End of cycle
- stop=clock();
- ccout(xi, i, stop, start);
- break;
- case 2:
- printf("\n\t\t\tSelected method of bisection\n\n");
- printf("Activate debugging mod?(1-yes/2-no): ");
- scanf("%d", &deb);
- if (deb==1) printf("\n\t\t\t\tDebugging mod status: [ON]\n\n");
- else printf("\n\t\t\t\tDebugging mod status: [OFF]\n\n");
- start=clock();
- do
- {
- xi=bisection(x1, x2);
- i++;
- i=iter(imax, i, xi, x1, x2, ch, deb, e, start);
- if (deb==1)
- {
- ccout(xi, i, stop, start);
- printf("x1=%lf x2=%lf\n", x1, x2);
- }
- if (unlinear_func(xi)<0) x1=xi;
- else
- {
- if (unlinear_func(xi)>0) x2=xi;
- }
- }
- while(e<fabs(x1-x2));
- stop=clock();
- ccout(xi, i, stop, start);
- break;
- default:
- printf("\nBad answer! Try again!");
- break;
- }
- povt=repeat(povt);
- }
- while(povt==1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement