tsounakis

Numerical Analysis 1

Dec 31st, 2021
1,349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>                                                      // Thanasis Tsounakis 2021 NEWTON-RAPHSON METHOD
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double L, C, q_0, t;
  6.  
  7. double q(double R){                                                     // q(R)) function
  8.     return q_0 * exp(-R * t / (2 * L)) * cos(t * sqrt(1 / (L * C) - pow(R / (2 * L), 2))) - 0.02;
  9. }
  10.  
  11. double dq(double R){                                                    // q'(R) derivative function of q(R)
  12.     return (q_0 * t * R * exp(-(t * R) / (2 * L)) * sin((t * sqrt((4 * L) / C - pow(R, 2))) / (2 * fabs(L)))) / (2 * fabs(L) * sqrt((4 * L) / C - pow(R, 2))) - (q_0 * t * exp(-(t * R) / (2 * L)) * cos((t * sqrt((4 * L) / C - pow(R, 2))) / (2 * fabs(L)))) / (2 * L);
  13. }
  14.  
  15. int main(){
  16.     L = 10.0;
  17.     C = 5 * pow(10.0, -5);
  18.     q_0 = 1.0;
  19.     t = 0.04;
  20.     double R, R_0 = 10.0; // variable R and initial value of R (R_0)
  21.     double error;
  22.     int k = -6, i = 0; // iteration variable
  23.  
  24.     printf("iter.:\tR:\t\tQ(R):\t\tError:\n");
  25.  
  26.     do{
  27.         i++;
  28.         R = R_0 - q(R_0) / dq(R_0);
  29.         error = fabs(R - R_0);
  30.         printf("%3d\t%14.6f\t%14.6f\t%14.6f\n", i, R, q(R), error); // print the data of the calculations
  31.         R_0 = R;
  32.     } while (error >= 0.5 * pow(10.0, k)); // do until error is smaller than 10^-10
  33.  
  34.     getchar();
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment