G2A Many GEOs
SHARE
TWEET

lab4

a guest Apr 10th, 2020 134 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int n = 10;
  2.  
  3. float y(float x) {
  4.     return std::sin(x)/3 + std::sin(2*x)/3 + std::cos(2*x);
  5. }
  6. float x(int i) {
  7.     return a + (float)i * h;
  8. }
  9.  
  10. // y'' + 4y = sin(x), y(0) = y0 = 1, y'(0) = y'0 = 1
  11. int main() {
  12.     {
  13.         const float a = y(0.0f);
  14.         const float b = y(1.0f);
  15.         std::vector<std::vector<float>> mat(n+1);
  16.         for (int i = 0; i < n+1; i++) {
  17.             mat[i] = std::vector<float>(n+1);
  18.             std::fill(mat[i].begin(), mat[i].end(), 0);
  19.         }
  20.  
  21.         const float l  = 1.0f / n;
  22.         const float hs = h * h;
  23.         const float k  = 4 * hs - 2;
  24.  
  25.         mat[0][0]   = 1; mat[0][n] = a;
  26.         mat[n][n-1] = 1; mat[n][n] = b;
  27.         for (int i = 1; i < n; i++) {
  28.             mat[i][i-1] = 1;
  29.             mat[i][i]   = k;
  30.             mat[i][i+1] = 1;
  31.             mat[i][n]   = hs * std::sin((float)i * l);
  32.         }
  33.  
  34.         std::vector<float>* solution = solve(mat); //from lab2
  35.  
  36.         for (int i = 0; i <= n; i++) {
  37.             std::cout << (std::fabs(y(x(i)) - (*solution)[i])) << std::endl;
  38.         }
  39.     }
  40.  
  41.     return 0;
  42. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top