-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (57 loc) · 1.3 KB
/
main.cpp
File metadata and controls
67 lines (57 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright(c) 2021, Yi-En Chou
#include <iostream>
#include <math.h>
#include <fstream>
#define PI 3.1415926
#define N 81 //mesh size
using namespace std;
void write(double* a, int x, int y);
int main() {
/*Implementation*/
//Problem Description
int m_no = N;
double m_grid[N][N] = { 0 };
int i = 0, j = 0;
double residual = 1;
double residual_after = 1;
double tol = pow(10, -7);
double temp = 0;
int iterations = 0;
//Initialization
for (i = 0; i < m_no; i++) {
m_grid[i][0] = 0;
m_grid[i][m_no - 1] = 0;
m_grid[m_no - 1][i] = 0;
m_grid[0][i] = sin(PI * i / (m_no - 1));
}
//main
while (residual > tol) {
iterations++;
residual = 0;
for (i = 1; i < m_no - 1; i++) {
for (j = 1; j < m_no - 1; j++) {
if (i == 0)
break;
temp = m_grid[i][j];
m_grid[i][j] = (m_grid[i - 1][j] + m_grid[i + 1][j] + m_grid[i][j - 1] + m_grid[i][j + 1]) * 0.25;
residual = residual + abs(m_grid[i][j] - temp);
}
}
cout << "iterations: " << iterations ;
cout << " residual: " << residual << endl;
}
write(&m_grid[0][0], m_no, m_no);
return 0;
}
void write(double* a, int x, int y) {
ofstream myfile("result.csv");
int i, j;
for (i = 0; i < y; i++) {
for (j = 0; j < x; j++) {
myfile << *(a + i * x + j) << ",";
}
myfile << endl;
}
myfile.close();
return;
}