Hi Everybody! I need help, I can't run the program. Also I need the iteration to run at maximum of 20 only and it has to be in C language, what should I change and am I doing this right?
DIRECTIONS ARE:
Algorithm:
1. Given a polynomial equation f(x), set 2 initial x values Xlower and Xupper.
2. Find the midpoint of the x values using: xr =xupper+xlower/2
3. Substitute the value of xr to f(x). If |f(x)| ≤ 0.001, you may stop the computation – this will
serve as your stopping criteria. If not, do the following:
a. If f(xlower) ∗ f(xr) < 0, then xupper = xr and xlower remains the same.
b. If f(xlower) ∗ f(xr) > 0, then xlower = xr and xupper remains the same.
c. If f(xlower) ∗ f(xr) = 0, then xr is the solution.
Implement the algorithm until the stopping criterion is satisfied or the number of iterations
reach 20. If either happens, display the current value of xr and the current value of f(xr). If the
maximum iteration is reached, display “MAXIMUM ITERATIONS REACHED”
THANK YOU FOR YOUR HELP!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
float Xl,Xr,Xu,f(xl),f(xr),f(xu);
int i=1;
printf("Bisect the interval and locate the midpoint, Xr= (Xl+Xu)/2\n");
printf("Solve for f(Xl),f(Xu) and f(Xr)\n");
a:printf("Xu: ");
scanf("%f",&f(xu));
printf("f(xr): ");
scanf("%f",&f(xr));
f(xl)=f(xu);
f(xu)=f(xr);
if((f(xl)*f(xu))>0)
else
{
printf("\t\tXl\tXr\tXu\tf(xl)\tf(xr)\tf(xu)\n");
b: Xl=(Xu+f(xr))/2;
Xr=f(Xl);
printf("\nItteration: %d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f",i,Xl,Xr,Xu,f(xl),f(xr),f(xu);
}
if((f(xl)*Xr)<0)
{
f(xr)=Xl;
f(xu)=Xr;
}
else
{
Xu=Xl;
f(xl)=Xr;
}
if(fabs((f(xr)-Xu)/f(xr))<=e)
else
{ i++;
goto b;
}
c:
{
printf("MAXIMUM ITERATIONS REACHED");
}
getch();
}
|