(b) It is required to solve a cubic equation of the form x3 - bx -c 0. A C progr
ID: 3908725 • Letter: #
Question
(b) It is required to solve a cubic equation of the form x3 - bx -c 0. A C program is to be developed to find one of the three roots of the cubic equation by expressing the cubic equation as follows: This form can be used to find one of the roots iteratively as follows: tn- 1 Where the subscript n has been used to denote the result at a given iterative step. Write a program to solve the equation by performing a suitable number of iterations. You need to set suitable conditions to decide when to stop iterating without causing undue computational load while obtaining a result of acceptable accuracy. The condition for stopping to iterate should also take care of situations that do not converge, and should report such cases to the user. The user should be requested to enter an initial value xo to start the iterations. [15 marks]Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <math.h>
int main()
{
double b, c, x, x0, diff, tol = 1E-4; //tolerance is 10^-4
int iter = 0, MAX_ITERATIONS = 1000;
printf("Enter ther co-efficient b: ");
scanf("%lf", &b);
printf("Enter ther co-efficient c: ");
scanf("%lf", &c);
printf("Enter the initial guess x0: ");
scanf("%lf", &x0);
diff = 1;
while(iter < MAX_ITERATIONS && fabs(diff) > tol)
{
iter++;
x = sqrt(b + c / x0);
diff = x - x0;
x0 = x;
}
if(fabs(diff) <= tol)
printf("The root is %lf ", x);
else
printf("The root could not be found to given tolerance in 1000 iterations, stopped at %lf ", x);
printf("No. of iterations is %d ", iter);
}
output
=======
Enter ther co-efficient b: 11
Enter ther co-efficient c: 6
Enter the initial guess x0: 1
The root is 3.561552
No. of iterations is 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.