Advertisement
GhostTV

newton-verfahren

Jan 21st, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. float a[6];
  5. float b[5];
  6. float sw;
  7. float lsg;
  8.  
  9. void eingabe(){
  10. for(int i = 0; i<6; i++)
  11. {
  12. printf("%i. Koeffizienten eingeben: ", i + 1);
  13. scanf("%f",&a[i]);
  14. }
  15. }
  16.  
  17. void ableitung(){
  18. for(int i = 0; i <5; i++)
  19. {
  20. b[i] = a[i] * (5-i);
  21. }
  22. }
  23.  
  24. float newton(float x){
  25. float aFktWert = 1;
  26. float bFktWert = 1;
  27.  
  28. while(abs(aFktWert) > 0.01)
  29. {
  30. aFktWert = 0;
  31. bFktWert = 0;
  32.  
  33. for(int i = 0; i<6; i++)
  34. {
  35. aFktWert += a[i]*pow(x,5-i);
  36. }
  37.  
  38. for(int i = 0; i<5; i++)
  39. {
  40. bFktWert += b[i]*pow(x,4-i);
  41. }
  42.  
  43. x = x - (aFktWert/bFktWert);
  44. }
  45.  
  46. return x;
  47. }
  48.  
  49. int main(void){
  50. eingabe();
  51. ableitung();
  52.  
  53. printf("\nFunktion: f(x) = %fx^5+%fx^4+%fx^3+%fx^2+%fx+%f\n",a[0],a[1],a[2],a[3],a[4],a[5]);
  54. printf("Ableitung: f'(x) = %fx^4+%fx^3+%fx^2+%fx+%f\n",b[0],b[1],b[2],b[3],b[4]);
  55.  
  56. printf("\nStartwert eingeben: ");
  57. scanf("%f",&sw);
  58.  
  59. lsg = newton(sw);
  60. printf("\nNulstelle bei x = %.2f",lsg);
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement