In the report, please include all the necessary steps in your solution, attach t
ID: 670590 • Letter: I
Question
In the report, please include all the necessary steps in your solution, attach the related computer code and plots. Consider ID Poisson's Equation uxx = 2 pi2 cos(2pi x), over the domain (0, 1). The boundary conditions are: u(0) = 0 and m(1) = 0. Using centered difference scheme and a mesh of 64, obtain a linear system Au = f for the problem, then solve the linear system using the following methods until a relative residual error of 10-5 is obtained. For (c), plot the analytical solution, numerical solution and the error distribution. Gauss-Seidel Method. Steepest Descent Method Conjugate Gradient MethodExplanation / Answer
1.Gauss-Seidel Method
#include<iostream>
#include<conio.h>
using namespace std;
int main(void)
{
float a[10][10], b[10], x[10], y[10];
int n = 0, m = 0, i = 0, j = 0;
cout << "Enter size of 2d array(Square matrix) : ";
cin >> n;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << "Enter values no :(" << i << ", " << j << ") ";
cin >> a[i][j];
}
}
cout << " Enter Values to the right side of equation ";
for (i = 0; i < n; i++)
{
cout << "Enter values no :(" << i << ", " << j << ") ";
cin >> b[i];
}
cout << "Enter initial values of x ";
for (i = 0; i < n; i++)
{
cout << "Enter values no. :(" << i<<"):";
cin >> x[i];
}
cout << " Enter the no. of iteration : ";
cin >> m;
while (m > 0)
{
for (i = 0; i < n; i++)
{
y[i] = (b[i] / a[i][i]);
for (j = 0; j < n; j++)
{
if (j == i)
continue;
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
x[i] = y[i];
}
printf("x%d = %f ", i + 1, y[i]);
}
cout << " ";
m--;
}
return 0;
}
2.Steepest Descent Method
#include <iostream>
#define ll long long
using namespace std;
ll modular_pow(ll base, ll exponent, int modulus)
{
ll result = 1;
while (exponent > 0)
{
if (exponent % 2 == 1)
result = (result * base) % modulus;
exponent = exponent >> 1;
base = (base * base) % modulus;
}
return result;
}
/*
* Main
*/
int main()
{
ll x, y;
int mod;
cout<<"Enter Base Value: ";
cin>>x;
cout<<"Enter Exponent: ";
cin>>y;
cout<<"Enter Modular Value: ";
cin>>mod;
cout<<modular_pow(x, y , mod);
return 0;
}
3. Conjugate Gradient Method
template < class Matrix, class Vector, class Preconditioner, class Real >
int
CGS(const Matrix &A, Vector &x, const Vector &b,
const Preconditioner &M, int &max_iter, Real &tol)
{
Real resid;
Vector rho_1(1), rho_2(1), alpha(1), beta(1);
Vector p, phat, q, qhat, vhat, u, uhat;
Real normb = norm(b);
Vector r = b - A*x;
Vector rtilde = r;
if (normb == 0.0)
normb = 1;
if ((resid = norm(r) / normb) <= tol) {
tol = resid;
max_iter = 0;
return 0;
}
for (int i = 1; i <= max_iter; i++) {
rho_1(0) = dot(rtilde, r);
if (rho_1(0) == 0) {
tol = norm(r) / normb;
return 2;
}
if (i == 1) {
u = r;
p = u;
} else {
beta(0) = rho_1(0) / rho_2(0);
u = r + beta(0) * q;
p = u + beta(0) * (q + beta(0) * p);
}
phat = M.solve(p);
vhat = A*phat;
alpha(0) = rho_1(0) / dot(rtilde, vhat);
q = u - alpha(0) * vhat;
uhat = M.solve(u + q);
x += alpha(0) * uhat;
qhat = A * uhat;
r -= alpha(0) * qhat;
rho_2(0) = rho_1(0);
if ((resid = norm(r) / normb) < tol) {
tol = resid;
max_iter = i;
return 0;
}
}
tol = resid;
return 1;
}