Please show as many comments as you can that describe what is going on. No matte
ID: 3632270 • Letter: P
Question
Please show as many comments as you can that describe what is going on. No matter how basic. I'm new to programming and struggling with these programs. Also, this program has to be written in C code not C++. Here is the question and some trial runs showing what the program must input and output exactly. Thank you all for your help.Write a program polyroot.c that uses the Newton-Raphson algorithm to find a root of a degree-3 polynomial. The program prompts the user for integer coefficients a0; a1; a2; a3 and a double precision initial point x0, and approximates a solution to the equation
f(x) = a0 + a1x + a2x^2 + a3x^3 = 0.
For example, if the coefficients are 1, 2, 2, 3, then the program approximates a solution to the equation 1 + 2x + 2x^2 + 3x^3 = 0. The program stops after 50 iterations or when |f(x)| < 10^-5,whichever comes first, and prints the result with a 5-decimal point precision.
Note: Store the coefficients a0; a1; a2; a3 in an integer array.
Sample runs:
(~)$ a.out
Coefficients: 1 2 2 3
Initial point: 0
-0.55232
(~)$ a.out
Coefficients: 2 -2 0 1
Initial point: 1
1.00000
(~)$
Explanation / Answer
#include
main(){
double est,next_est,fx,f1x;
printf("Enter the coefficients in the order of a0,a1,a2,a3");
int coeff[4];
for(int i=0;i<4;i++){
scanf("%d",&a[i]);
}
printf("Enter the intial point");
scanf("%lf",&est);
for(int i=0;i<50;i++){
fx= (a[3]*est^3)+(a[2]*est^2)+(a[1]*est)+(a[0]);
f1x= (3*a[3]*est^2)+(2*a[2]*est)+(a[1]);
next_est = est - (fx/f1x);
if(fabs(next_est)<(10^-15)){
break;}
est = next_est;
}
printf("The approximate root is %lf",next_est);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.