SolStis
- 10
- 0
The code should solve laplace equation through an iterative technique until values change less than the specified tollerence, in this case maxdiff. I've used 3 arrays. one to store all values including initial and boundary conditions, and 2 more to store the new values and differences between the old values. The code then sums all values in the differences array, diff[j], and the calculates the average to confirm that allvalues in the array have changed by more or less than the tollerence. The code compiles ok but then performs an illegal operation, can anyone help?
Code:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main() {
//Declaration of variables
float V[20][20], V0[18][18], diff[18][18];
int i, j;
for (i=0;i<=19;i++) {
for (j=0;j<=19;j++) {
V[0][j] = 0.0;
V[19][j] = 1.0;
V[0] = (i)/19.0;
V[19] = (i)/19.0;
}
}
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V[j]=1.0;
}
}
float maxdiff, sum, diffsum;
int count=0;
for(i=1;i<=18;i++) {
for(j=1; j<=18; j++) {
diff[j]=1.0;
}
}
maxdiff=0.0001;
diffsum=1.0;
while (diffsum >= maxdiff) {
sum = 0.0;
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V0[j] = V[j];
V[j] = ((V[i+1][j] + V[i-1][j] + V[j+1] + V[j-1])/4.0);
diff[j] = (V0[j] - V[j]);
sum = sum + diff[j];
}
}
diffsum=sum/(18 * 18);
count++;
}
cout << count;
}
Code:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main() {
//Declaration of variables
float V[20][20], V0[18][18], diff[18][18];
int i, j;
for (i=0;i<=19;i++) {
for (j=0;j<=19;j++) {
V[0][j] = 0.0;
V[19][j] = 1.0;
V[0] = (i)/19.0;
V[19] = (i)/19.0;
}
}
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V[j]=1.0;
}
}
float maxdiff, sum, diffsum;
int count=0;
for(i=1;i<=18;i++) {
for(j=1; j<=18; j++) {
diff[j]=1.0;
}
}
maxdiff=0.0001;
diffsum=1.0;
while (diffsum >= maxdiff) {
sum = 0.0;
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V0[j] = V[j];
V[j] = ((V[i+1][j] + V[i-1][j] + V[j+1] + V[j-1])/4.0);
diff[j] = (V0[j] - V[j]);
sum = sum + diff[j];
}
}
diffsum=sum/(18 * 18);
count++;
}
cout << count;
}