Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // Thanasis Tsounakis 2021 NEWTON-RAPHSON METHOD
- #include <stdlib.h>
- #include <math.h>
- double L, C, q_0, t;
- double q(double R){ // q(R)) function
- return q_0 * exp(-R * t / (2 * L)) * cos(t * sqrt(1 / (L * C) - pow(R / (2 * L), 2))) - 0.02;
- }
- double dq(double R){ // q'(R) derivative function of q(R)
- 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);
- }
- int main(){
- L = 10.0;
- C = 5 * pow(10.0, -5);
- q_0 = 1.0;
- t = 0.04;
- double R, R_0 = 10.0; // variable R and initial value of R (R_0)
- double error;
- int k = -6, i = 0; // iteration variable
- printf("iter.:\tR:\t\tQ(R):\t\tError:\n");
- do{
- i++;
- R = R_0 - q(R_0) / dq(R_0);
- error = fabs(R - R_0);
- printf("%3d\t%14.6f\t%14.6f\t%14.6f\n", i, R, q(R), error); // print the data of the calculations
- R_0 = R;
- } while (error >= 0.5 * pow(10.0, k)); // do until error is smaller than 10^-10
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment