#include //stdio.h #include //conio.h #include //stdlib.h #include //math.h #define maxy 10 float f[maxy],f1[maxy]; int deg,i,j,k; float x[100];
float power(float value,int pwr) { int i; float t=1; for(i=0;i { t = t*value; } return t; } float eval(float fun[],float val,int deg) { double t=0;int i=0; for(i=0;i<=deg;i++) { t = t + fun[i] * power(val,i); } return (float)t; }
void main() { /* Variable declaration */ clrscr(); printf("\nEnter the degree of the function:"); scanf("%d",°); printf("\nEnter the coefficients:\n"); for(i=deg;i>=0;i--) { printf("x^%d :",i); scanf("%f",&f[i]); } for(i=deg;i>=0;i--) { f1[i-1]=f[i]*i; } clrscr(); printf("\nThe function is:\n"); for(i=deg;i>=0;i--) { if((f[i]>0&&i>0)) printf(" + %0.2f x^%d ",f[i],i); else printf(" %0.2f x^%d ",f[i],i); } printf("\nThe differenciated function is:\n"); for(i=deg;i>0;i--) { if((f1[i-1]>0)&&(i>0)) printf(" + %0.2f x^%d ",f1[i-1],i-1); else printf(" %0.2f x^%d ",f1[i-1],i-1); }
for(i=-50;i<50;i++) { float x1,x2; x1 = eval(f,i,deg); x2 = eval(f,(i+1),deg); if((int)(x2*100000)==0) { printf("\n\nThe solution is: %d",i+1); getch(); exit(0); } if((int)(x1*x2*100000)<0) { x[0]= (float)i+1.0/2; break; } } for(i=1;i<100;i++) { if(eval(f1,(float)(i-1),deg-1)) x[i] = x[i-1] - (eval(f,x[i-1],deg)/eval(f1,x[i-1],deg-1)); if((int)(x[i]*10000)==(int)(x[i-1]*10000)) break; } printf("\n\nThe solution is: %0.4f",x[i]); getch(); }
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|