Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int n = 10;
- float y(float x) {
- return std::sin(x)/3 + std::sin(2*x)/3 + std::cos(2*x);
- }
- float x(int i) {
- return a + (float)i * h;
- }
- // y'' + 4y = sin(x), y(0) = y0 = 1, y'(0) = y'0 = 1
- int main() {
- {
- const float a = y(0.0f);
- const float b = y(1.0f);
- std::vector<std::vector<float>> mat(n+1);
- for (int i = 0; i < n+1; i++) {
- mat[i] = std::vector<float>(n+1);
- std::fill(mat[i].begin(), mat[i].end(), 0);
- }
- const float l = 1.0f / n;
- const float hs = h * h;
- const float k = 4 * hs - 2;
- mat[0][0] = 1; mat[0][n] = a;
- mat[n][n-1] = 1; mat[n][n] = b;
- for (int i = 1; i < n; i++) {
- mat[i][i-1] = 1;
- mat[i][i] = k;
- mat[i][i+1] = 1;
- mat[i][n] = hs * std::sin((float)i * l);
- }
- std::vector<float>* solution = solve(mat); //from lab2
- for (int i = 0; i <= n; i++) {
- std::cout << (std::fabs(y(x(i)) - (*solution)[i])) << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement