Write a C program to find the roots for the following polynomial using Newton-Ra
ID: 3822513 • Letter: W
Question
Write a C program to find the roots for the following polynomial using Newton-Raphson method.
F(x) = Ax^3 + Bx^2 + Cx + D
Where A, B, C and D are coefficients/constant of the polynomial. The program should ask the user to enter the values for each and also the initial value of estimated root value.
Use 10 decimal places for precision.
Expected exact input and output.
I keep getting 1.2500000000 in the first estimation while I need 1.000000000
Finding root for three degree polynomial Ax 3 Bx 2 Cx D using Newton-Raphson method Enter value for the following coefficients D nitial root estimate :1.0 1 1.0000000000 Estimate 2 Estimate 1.1250000000 3 Estimate 1.1149754501 4 Estimate 1.1149075446 5 Estimate 1.1149075415 Approximate solution 1.1149075415Explanation / Answer
Please find the below code :
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
int max_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;
int main()
{
printf("----------------------------------------------------------- ");
printf("----------------------Made by C code champ----------------- ");
printf("----------------------------------------------------------- ");
printf(" C PROGRAM FOR NEWTON RAPHSON METHOD");
printf(" ENTER THE MAXIMUM POWER OF X = ");
scanf("%d",&max_power);
for(i=0;i<=max_power;i++)
{
printf(" x^%d = ",i);
scanf("%d",&coef[i]);
}
printf(" ");
printf(" THE POLYNOMIAL IS = ");
for(i=max_power;i>=0;i--)/*printing coefficients*/
{
printf(" %dx^%d",coef[i],i);
}
printf(" First approximation x1 ----> ");
scanf("%f",&x1);
printf(" ----------------------------------------------------------- ");
printf(" ITERATION x1 F(x1) F'(x1) ");
printf(" ----------------------------------------------------------- ");
do
{
cnt++;
fx1=fdx1=0;
for(i=max_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ;
}
fx1+=coef[0];
for(i=max_power;i>=0;i--)
{
fdx1+=coef[i]* (i*pow(x1,(i-1)));
}
t=x2;
x2=(x1-(fx1/fdx1));
x1=x2;
printf(" %d %.3f %.3f %.3f ",cnt,x2,fx1,fdx1);
}while((fabs(t - x1))>=0.0001);
printf(" THE ROOT OF EQUATION IS = %f",x2);
getch();
//return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.