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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Created by Kyle James Hansen on 9 May 2016
// Copyright © 2016 Kyle James Hansen. All rights reserved.
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
double abs(double ABS)
{
return sqrt(ABS*ABS);
}
double chop(double CHOP)
{
if (abs(CHOP) < pow(10,-15))
{
CHOP = 0;
}
return CHOP;
}
int main()
{
int i,j,m,N,n,w=15;
// Define size of system
cout << "Solve system [A.x = b] using Jacobi Iteration"<< endl << endl << "Enter 'N', the size of the system:" << endl;
cin >> N;
n = N - 1;
double A[N][N],b[N],x[N][1000],xnorm,errtol=pow(10,-10),sum[N];
// Define matrix A (coefficient matrix)
cout << endl << "Enter coefficients of " << N << "*" << N << " system:" << endl;
for(j=0;j<=n;j=j+1)
{
for (i=0;i<=n;i=i+1)
{
cin >> A[j][i];
}
}
// Define constant vector b
cout << endl << "Enter " << N << " constants of RHS of system:" << endl;
for(j=0;j<=n;j=j+1)
{
cin >> b[j];
}
// Define initial guess for vector x that solves [A.x = b]
m=0;
for(j=0;j<=n;j=j+1)
{
x[j][m] = j + 1;
}
// Print system as check
cout << endl << "System:" << endl;
for(j=0;j<=n;j=j+1)
{
for(i=0;i<=n;i=i+1)
{
cout << A[j][i];
if (i<n)
{
cout << setw(5);
}
}
cout << " | " << b[j] << endl;
}
// Start output table
cout << endl << "m";
for(j=0;j<=n;j=j+1)
{
cout << setw(w-1) << "x" << j;
}
cout << setw(w) << "||X||2" << endl;
cout << m;
for(j=0;j<=n;j=j+1)
{
cout << setw(w) << x[j][m];
}
cout << setw(w) << "-" << endl;
// Jacobi algorithm
do
{
m = m + 1;
// Iterate one row at a time (x0,x1,...,xn)
for (j = 0; j <= n; j = j + 1)
{
sum[j] = 0;
// for x0
if ((j = 0))
{
for (i = 1; i <= n; i++)
{
sum[j] = sum[j] + (A[j][i]*x[i][m - 1]);
}
x[j][m] = (1/A[j][j])*(b[j] - sum[j]);
}
// for x1,...,xn-1
else if ((j > 0) && (j < n))
{
for (i = 0; i < j; i++)
{
sum[j] = sum[j] + (A[j][i]*x[i][m - 1]);
}
for (i = j + 1; i <= n; i++)
{
sum[j] = sum[j] + (A[j][i]*x[i][m - 1]);
}
x[j][m] = (1/A[j][j])*(b[j] - sum[j]);
}
// for xn
else if ((j = n))
{
for (i = 0; i < n; i++)
{
sum[j] = sum[j] + (A[j][i]*x[i][m - 1]);
}
x[j][m] = (1/A[j][j])*(b[j] - sum[j]);
}
}
// Calculate two norm of error vector (X[m]-X[m-1])
for(xnorm=0,j=0;j<=n;j=j+1)
{
xnorm = xnorm + pow(x[j][m]-x[j][m-1],2);
}
xnorm = sqrt(xnorm);
// Print results of current iterate
cout << m;
for(j=0;j<=n;j=j+1)
{
cout << setw(w) << x[j][m];
}
cout << setw(w) << xnorm << endl;
} while ((xnorm > errtol) && (m<1000));
// Format final results
cout << endl << "x[" << m << "] that solves [A.x = b] = {";
for (j=0;j<=n;j=j+1)
{
cout << x[j][m];
if (j<n)
{
cout << ", ";
}
}
cout << "}T" << endl;
// Print info
cout << endl << "Copyright © 2016 Kyle James Hansen. All rights reserved. ";
}
|