Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. // adams1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. /*
  5. *    Adams-Bashforth four-step explicit Multistep method.
  6. *    RUNGE-KUTTA (ORDER 4) ALGORITHM is used to compute w1
  7. *
  8. *    TO APPROXIMATE THE SOLUTION TO THE INITIAL VALUE PROBLEM:
  9. *               Y' = F(T,Y), A<=T<=B, Y(A) = ALPHA,
  10. *    AT (N+1) EQUALLY SPACED NUMBERS IN THE INTERVAL [A,B].
  11. *
  12. *    INPUT:   ENDPOINTS A,B; INITIAL CONDITION ALPHA; INTEGER N.
  13. *
  14. *    OUTPUT:  APPROXIMATION W TO Y AT THE (N+1) VALUES OF T.
  15. */
  16.  
  17. #include "stdafx.h"
  18.  
  19. #include<stdio.h>
  20. #include<math.h>
  21. #include<iostream>
  22. #include <fstream>
  23. #include <string>
  24.  
  25. #define true 1
  26. #define false 0
  27. #define MAX_N 2000
  28.  
  29. static   double F(double, double, double);
  30. static   double F2(double, double, double);
  31.  
  32. using namespace std;
  33.  
  34. int main()
  35. {
  36.       string str[30][3];
  37.       ofstream myoutput("output.txt");
  38.  
  39.       double A,B,ALPHA,H,T,W[MAX_N][2], K1,K2,K3,K4,L1,L2,L3,L4;
  40.       int I,N;
  41.  
  42.       A = 0.0;
  43.       B = 1.0;
  44.       N = 100;
  45.  
  46.       cout.setf(ios::fixed,ios::floatfield);
  47.       cout.precision(5);
  48.  
  49.       /* STEP 1 */
  50.       H = (B - A) / N;
  51.       T = A;
  52.       W[0][0] = 5.0; // initial value
  53.       W[0][1] = -10.0;
  54.  
  55.       /* STEP 2 --- Use order 4 RK method to get w1, w2, w3 */
  56.       /// NOTE: The "for" loop starts with I = 1.
  57.       if (myoutput.is_open())
  58.     {
  59.       //For F1 and F2
  60.       for (I=1; I<=2; I++)
  61.       {
  62.          /* STEP 3 */
  63.          /* Compute K1, K2 RESP. */
  64.          K1 = H*F(T, W[I-1][0],W[I-1][1]);
  65.          L1 = H*F2(T, W[I-1][0],W[I-1][1]);
  66.          K2 = H*F(T + H/2.0, W[I-1][0] + K1/2.0, W[I-1][1] + L1/2.0);
  67.          L2 = H*F2(T + H/2.0, W[I-1][0] + K1/2.0, W[I-1][1] + L1/2.0);
  68.          //K3 = H*F(T + H/2.0, W[I-1] + K2/2.0);
  69.          //K4 = H*F(T + H, W[I-1] + K3);
  70.          K3 = H*F(T + H, W[I-1][0] + 2.0*K2 - K1, W[I-1][1] + 2.0*L2 - L1);
  71.          L3 = H*F2(T + H, W[I-1][0] + 2.0*K2 - K1, W[I-1][1] + 2.0*L2 - L1);
  72.  
  73.          /* STEP 4 */
  74.          /* COMPUTE W(I) */
  75.          //W[I] = W[I-1] + 1/6.0*(K1 + 2.0*K2 + 2.0*K3 + K4);
  76.          W[I][0] = W[I-1][0] + 1/6.0*(K1 + 4.0*K2 +K3);
  77.          W[I][1] = W[I-1][1] + 1/6.0*(L1 + 4.0*L2 +L3);
  78.  
  79.  
  80.          /* COMPUTE T(I) */
  81.          T = A + I * H;
  82.  
  83.          /* STEP 5 */
  84.          cout <<"F1 - F2: At time "<< T <<" the solution = "<< W[I][0] << "," << W[I][1] << endl;
  85.          
  86.              for (int b=0;b<2;b++)
  87.              {
  88.                  str[I-1][b] = W[I][b];
  89.                  myoutput << str[I-1][b]<< " ";
  90.              }
  91.       }
  92.  
  93.       /* STEP 6---Use Adam-Bashforth 4-step explicit method */
  94.       //for(I = 4; I <= N; I++)
  95.       for(I = 3; I <= N; I++)
  96.       {
  97.           //K1 = 55.0*F(T, W[I-1]) - 59.0*F(T-H, W[I-2]) + 37.0*F(T-2.0*H, W[I-3]) - 9.0*F(T-3.0*H, W[I-4]);
  98.           K1 = 23.0*F(T, W[I-1][0], W[I-1][1]) - 16.0*F(T-H, W[I-2][0], W[I-2][1]) + 5.0*F(T-2.0*H, W[I-3][0], W[I-3][1]);
  99.           L1 = 23.0*F2(T, W[I-1][0], W[I-1][1]) - 16.0*F2(T-H, W[I-2][0], W[I-2][1]) + 5.0*F2(T-2.0*H, W[I-3][0], W[I-3][1]);
  100.           //W[I] = W[I-1] + H/24.0*K1;
  101.           W[I][0] = W[I-1][0] + H/12.0*K1;
  102.           W[I][1] = W[I-1][1] + H/12.0*K1;
  103.  
  104.           /* COMPUTE T(I) */
  105.           T = A + I * H;
  106.  
  107.           /* STEP 7 */
  108.           cout <<"F1 - F2: At time "<< T <<" the solution = "<< W[I][0] << "," << W[I][1] << endl;
  109.           for (int b=0;b<2;b++)
  110.              {
  111.                  str[I-1][b] = W[I][b];
  112.                  myoutput << str[I-1][b]<< " ";
  113.              }
  114.       }
  115.  
  116.     }
  117.       system("pause");
  118.       return 0;
  119. }
  120. /*  Change function F for a new problem   */
  121. double F(double T, double Y, double Y2)
  122. {
  123.    double f;
  124.  
  125.    f = -501*Y + 500*Y2 + T*0;
  126.    return f;
  127. }
  128. double F2(double T, double Y, double Y2)
  129. {
  130.    double f;
  131.  
  132.    f = 500*Y - 501*Y2 + T*0;
  133.    return f;
  134. }
Add Comment
Please, Sign In to add comment