#include<stdio.h>
float r0; /*initial value of root */
struct polynomial {
int number_of_terms;
int exponent[10];
float coefficient[10];
} poly;
struct differenciated_polynomial {
int number_of_terms;
int exponent[10];
float coefficient[10];
} diff_poly;
float power(float x,int n) {
int i;
float pow=1;
for(i=0;i<n;i++)
pow = pow*x;
return pow;
}
void store_poly() {
int counter=0;
printf("\tEnter the number of terms for your polynomial .... ");
scanf("%d",&poly.number_of_terms);
while(counter<poly.number_of_terms) {
printf("\tEnter the value of coefficient for term %d .... ",counter+1);
scanf("%f",&poly.coefficient[counter]);
printf("\tEnter the value of its exponent .... ");
scanf("%d",&poly.exponent[counter]);
counter++ ;
}
printf("\n\n\tEnter initial approximation of the root ......");
scanf("%f",&r0);
}
void differenciate_poly() {
int i;
diff_poly.number_of_terms=poly.number_of_terms;
for(i=0;i<poly.number_of_terms;i++) {
diff_poly.coefficient[i]=poly.coefficient[i]*poly.exponent[i];
diff_poly.exponent[i]=poly.exponent[i]-1;
}
}
float value_at_poly(float x) { /* to calculate f(x) */
int counter=0;
float value=0.00;
while(counter<poly.number_of_terms) {
value += poly.coefficient[counter] * power(x,poly.exponent[counter]);
counter++;
}
return value;
}
float value_at_diff(float x) { /* to calculate f'(x) */
int counter=0;
float value=0.00;
while(counter<diff_poly.number_of_terms) {
value += diff_poly.coefficient[counter] * power(x,diff_poly.exponent[counter]);
counter++;
}
return value;
}
void print_poly() {
int length=0;
printf("\t----------------------------------------------------------------------------------------\n");
printf("\n\tThe Entered polynomial is as follows ..... ");
while(length<poly.number_of_terms) {
if(poly.coefficient[length]!=0.0)
printf("(%f * X^ %d) ",poly.coefficient[length],poly.exponent[length]);
if(length!= poly.number_of_terms-1)
printf("+ ");
length++ ;
}
printf(" = 0 \n");
printf("\t------------------------------------------------------------------------------------------\n");
}
void print_diff_poly() {
int length=0;
printf("\t----------------------------------------------------------------------------------------\n");
printf("\n\tThe 1st order differenciation polynomial is as follows ..... ");
while(length<diff_poly.number_of_terms) {
if(diff_poly.coefficient[length]!=0.0)
printf("(%f * X^ %d) ",diff_poly.coefficient[length],diff_poly.exponent[length]);
if(length!=diff_poly.number_of_terms-1)
printf("+ ");
length++ ;
}
printf(" = 0 \n");
printf("\t------------------------------------------------------------------------------------------\n");
}
float find_root() {
int i;
float prev_r0;
for(i=0;i<20;i++) {
prev_r0 = r0;
r0 = prev_r0 - value_at_poly(prev_r0)/value_at_diff(prev_r0);
}
return r0;
}
main() {
printf("\t********************************************************************\n");
printf("\t DEMONSTRATION OF NEWTON-RAPHSON METHOD\n");
printf("\t********************************************************************\n");
printf("\t ENTER THE POLYNOMIAL ........\n\n");
store_poly();
differenciate_poly();
print_poly();
print_diff_poly();
printf("\n\t================================================================\n");
printf("\t Calculated value of Root (Newton-Raphson) = %f\n",find_root());
printf("\t============================================================\n");
}
No comments:
Post a Comment
Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.