Question:
Logic c programming HELP!?
Alex
2012-03-29 09:15:24 UTC
I'm making a c program to find the route of an equation (which you will see in the code). I have compiled the code and i can enter the x1 and x2 values but then the program stops.
Can anyone see where I'm going wrong and correct it for me??? Thanks

#include
#include
main()
{
float x1,x2,x,series;
double d;

printf("enter x1(pos)\n");
scanf("%f\n",&x1);
printf("enter x2(pos)\n");
scanf("%f",&x2);

x=0;
d=1;
while(d>0.0001)
{
x=(x1+x2)/2;

series=exp(x)-exp(2*x)+exp(-x)-pow(sin(x),4);

d=fabs(series);

if(x*x1 < 0)
x2=x;
else
x1=x;
}

printf("ans=%f",x);

return 0;
}
Five answers:
John H
2012-03-29 09:36:32 UTC
Try printing out d and x in you loop. That will give you a clue as to why your series doesn't converge. Could be something as simple as a typo.
Brian
2012-03-29 10:05:54 UTC
Your root finder is incorrect and will just end up in an infinite loop most of the time. The first problem is that you don't have any way to break out of the loop if no root exists between x1 and x2. The second problem is that it won't always find a root even if one exists.



A better way to write an iterative root finder is to use a FOR loop to guarantee that the program exits. This is Newton's method...



float tolerance = 0.0001;

int maxiterations = 1000;

float dx = 0.001; // choose dx to be sufficiently small

for(int i=0; i tolerance && (x >= x1 && x <= x2); i++)

{

x = x - F(x) / ((F(x+dx) - F(x)) / dx);

}



if (x >= x1 && x <= x2 && fabs(F(x)) <= tolerance)

printf("Root = %f\n", x);

else

printf("No real root exists between %f and %f.\n", x1, x2);



.

.

.



float F(float x)

{

return exp(x)-exp(2*x)+exp(-x)-pow(sin(x…

}
2012-03-29 09:26:54 UTC
Try to use doubles instead of floats. C might have some problems with the conversions of float numbers into double numbers.



So instead of float x1, x2, try double x1, x2.



When it comes to in- and output, I prefer C++ (cout << and cin >> might be more helpful). From my own experience, I know, commands like scanf can be very annoying.
2016-12-05 08:49:01 UTC
i think of you're working into difficulty by way of fact 365 isn't divisible by using 30. 365/30 = 12.16666 months/12 months. attempt an enter of 364 days. It yields 0 12 months(suitable) 12 month(suitable?) and four days(incorrect!). Which month are the final 5 days in? A 360 day 12 months would artwork greater constructive. or you desire a 13'th month with 5 days, after which you may desire to account for that throughout the math.
?
2012-03-29 09:47:46 UTC
Do refer the following where you find complete code.

In my last response I have outline and then I have asked you to add few lines.

http://wiki.answers.com/Q/C_program_for_regula_falsi_method



I fiddled the code available at the above link. See the working solution



#include

#include

#include





/* define prototype for USER-SUPPLIED function f(x) */



double ffunction(double x);





/* EXAMPLE for "ffunction" */



double ffunction(double x)



{

return (exp(x)-exp(2*x)+exp(-x) +pow( sin(x),4));

}



/* -------------------------------------------------------- */



/* Main program for algorithm 2.3 */



int main()



{

double Delta = 1E-6; /* Closeness for consecutive iterates */

double Epsilon = 1E-6; /* Tolerance for the size of f(C) */

int Max = 199; /* Maximum number of iterations */

int Satisfied = 0; /* Condition for loop termination */



double A, B; /* INPUT endpoints of the interval [A,B] */

double YA, YB; /* Function values at the interval-borders */

int K; /* Loop Counter */

double C, YC; /* new iterate and function value there */

double DX; /* change in iterate */





printf("-----------------------------------------------------\n");

printf("Please enter endpoints A and B of the interval [A,B]\n");

printf("EXAMPLE : A = 0 and B = 2. Type: 0 2 \n");

scanf("%lf %lf", &A, &B);

printf("The interval ranges from %lf to %lf\n", A,B);



YA = ffunction(A); /* compute function values */

YB = ffunction(B);



/* Check to see if YA and YB have same SIGN */



if( ( (YA >= 0) && (YB >=0) ) || ( (YA < 0) && (YB < 0) ) ) {

printf("The values ffunction(A) and ffunction(B)\n");

printf("do not differ in sign.\n");

exit(0); /* exit program */

}



for(K = 1; K <= Max ; K++) {



if(Satisfied == 1) break;



DX = YB * (B - A)/(YB -YA); /* Change in iterate */

C = B - DX; /* New iterate */



YC = ffunction(C); /* Function value of new iterate */





if( YC == 0) { /* first 'if' */

Satisfied = 1; /* Exact root is found */

}

else if( ( (YB >= 0) && (YC >=0) ) || ( (YB < 0) && (YC < 0) ) ) {

B = C; /* Squeeze from the right */

YB = YC;

}

else {

A = C; /* Squeeze from the left */

YA = YC;

}



if( (fabs(DX) < Delta) && (fabs(YC) < Epsilon) ) Satisfied = 1;



} /* end of 'for'-loop */



printf("----------------------------------------------\n");

printf("The number of performed iterations is : %d\n",K - 1);

printf("----------------------------------------------\n");

printf("The computed root of f(x) = 0 is : %lf \n",C);

printf("----------------------------------------------\n");

printf("Consecutive iterates differ by %lf\n", DX);

printf("----------------------------------------------\n");

printf("The value of the function f(C) is %lf\n",YC);



system("PAUSE");

return 0;

} /* End of main program */


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...