Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- //wyliczanie wartości funkcji w zadanym x
- float funcArg(int r, float arg, float tab[20])
- {
- float result = 0;
- int i;
- for(i=0; i<=r; i++)
- result += pow(arg,i)*tab[i];
- return result;
- }
- //wartość pierwszej pochodnej funkcji w x
- float deriv(int r, float arg, float tab[20])
- {
- float result = 0;
- int i;
- for(i=1; i<=r; i++)
- result += pow(arg,i-1)*tab[i]*i;
- return result;
- }
- //wartość drugiej pochodnej funkcji w x
- float deriv2(int r, float arg, float tab[20])
- {
- float result = 0;
- int i;
- for(i=2; i<=r; i++)
- result += pow(arg,i-2)*tab[i]*i*(i-1);
- return result;
- }
- int main(int argc,char **argv)
- {
- float a, b, E, wsp[20], x, y, yk, y1,y2,z;
- int i, n;
- printf("Podaj rzad wielomianu: ");
- scanf("%d",&n);
- for(i=0; i<=n; i++)
- {
- printf("Podaj wspolczynnik znajdujacy sie przy %d potedze: ",i);
- scanf("%f",&wsp[i]);
- }
- printf("Podaj granice przedzialu:\n");
- printf("a = ");
- scanf("%f",&a);
- printf("b = ");
- scanf("%f",&b);
- printf("Podaj dokladnosc :");
- scanf("%f",&E);
- z = (a + b)/2;
- //obliczenie wartości pierwszej i drugiej pochodnej w punkcie z
- y1 = deriv(n,z,wsp);
- y2 = deriv2(n,z,wsp);
- //algorytm z instrukcji
- y = funcArg(n,b,wsp);
- yk = funcArg(n,a,wsp);
- x = b;
- i = 0;
- if(y1*y2 < 0)
- {
- while(fabs(y) > E)
- {
- x -= y*(a-x)/(yk-y);
- y = funcArg(n,x,wsp);
- i++;
- }
- printf("Otrzymany pierwiastek x = %0.3f\n", x);
- } else printf("Funkcja nie ma pierwiastkow w tym przedziale");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement