Feat: hw2 done
This commit is contained in:
49
HW2_111550013/HW2_111550013_code/problem1.cpp
Normal file
49
HW2_111550013/HW2_111550013_code/problem1.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// Problem 1: Gaussian elimination with partial pivoting
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
const int N = 4;
|
||||
|
||||
int main() {
|
||||
double A[N][N+1] = {
|
||||
{4, 2, -2, -1, 7},
|
||||
{0, 4, 1, 2, 10},
|
||||
{3, -2, 1, 2, 2},
|
||||
{2, 0, 3, -5, 3}
|
||||
};
|
||||
|
||||
// Forward elimination with partial pivoting
|
||||
for (int k = 0; k < N - 1; k++) {
|
||||
// Find pivot
|
||||
int maxRow = k;
|
||||
for (int i = k + 1; i < N; i++)
|
||||
if (fabs(A[i][k]) > fabs(A[maxRow][k])) maxRow = i;
|
||||
|
||||
if (maxRow != k) {
|
||||
cout << "Column " << (k+1) << ": swap row " << (k+1) << " with row " << (maxRow+1) << "\n";
|
||||
for (int j = 0; j <= N; j++) swap(A[k][j], A[maxRow][j]);
|
||||
}
|
||||
|
||||
// Eliminate
|
||||
for (int i = k + 1; i < N; i++) {
|
||||
double m = A[i][k] / A[k][k];
|
||||
for (int j = k; j <= N; j++) A[i][j] -= m * A[k][j];
|
||||
}
|
||||
}
|
||||
|
||||
// Back substitution
|
||||
double x[N];
|
||||
for (int i = N - 1; i >= 0; i--) {
|
||||
x[i] = A[i][N];
|
||||
for (int j = i + 1; j < N; j++) x[i] -= A[i][j] * x[j];
|
||||
x[i] /= A[i][i];
|
||||
}
|
||||
|
||||
cout << "\nSolution:\n";
|
||||
for (int i = 0; i < N; i++)
|
||||
cout << "x" << (i+1) << " = " << fixed << setprecision(6) << x[i] << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
HW2_111550013/HW2_111550013_code/problem2.cpp
Normal file
62
HW2_111550013/HW2_111550013_code/problem2.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
// Problem 2: Scaled partial pivoting
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
double roundSig(double val, int sig) {
|
||||
if (val == 0) return 0;
|
||||
double d = ceil(log10(fabs(val)));
|
||||
double p = pow(10, sig - d);
|
||||
return round(val * p) / p;
|
||||
}
|
||||
|
||||
void solve(vector<vector<double>> A, int n, int sig) {
|
||||
cout << "\n" << sig << " significant digits:\n";
|
||||
|
||||
vector<double> s(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
for (int j = 0; j < n; j++) s[i] = max(s[i], fabs(A[i][j]));
|
||||
|
||||
vector<int> idx(n);
|
||||
for (int i = 0; i < n; i++) idx[i] = i;
|
||||
|
||||
for (int k = 0; k < n - 1; k++) {
|
||||
int maxRow = k;
|
||||
double maxRatio = fabs(A[idx[k]][k]) / s[idx[k]];
|
||||
for (int i = k + 1; i < n; i++) {
|
||||
double ratio = fabs(A[idx[i]][k]) / s[idx[i]];
|
||||
if (ratio > maxRatio) { maxRatio = ratio; maxRow = i; }
|
||||
}
|
||||
swap(idx[k], idx[maxRow]);
|
||||
|
||||
for (int i = k + 1; i < n; i++) {
|
||||
double m = roundSig(A[idx[i]][k] / A[idx[k]][k], sig);
|
||||
for (int j = k; j <= n; j++)
|
||||
A[idx[i]][j] = roundSig(A[idx[i]][j] - m * A[idx[k]][j], sig);
|
||||
}
|
||||
}
|
||||
|
||||
vector<double> x(n);
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
x[i] = A[idx[i]][n];
|
||||
for (int j = i + 1; j < n; j++)
|
||||
x[i] = roundSig(x[i] - A[idx[i]][j] * x[j], sig);
|
||||
x[i] = roundSig(x[i] / A[idx[i]][i], sig);
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << "x" << (i+1) << " = " << fixed << setprecision(6) << x[i] << "\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<vector<double>> A = {
|
||||
{4.13, -2.20, 0.95, 3.02},
|
||||
{6.14, 4.45, -1.45, -4.02},
|
||||
{1.03, 1.86, 0.44, 5.22}
|
||||
};
|
||||
solve(A, 3, 6);
|
||||
solve(A, 3, 3);
|
||||
return 0;
|
||||
}
|
||||
32
HW2_111550013/HW2_111550013_code/problem3.cpp
Normal file
32
HW2_111550013/HW2_111550013_code/problem3.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Problem 3: Symmetric tridiagonal system
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int n = 6;
|
||||
vector<double> d = {4, 4, 4, 4, 4, 4}; // diagonal
|
||||
vector<double> a = {-1, -1, -1, -1, -1}; // off-diagonal
|
||||
vector<double> b = {100, 200, 200, 200, 200, 100};
|
||||
|
||||
// Forward elimination
|
||||
for (int i = 1; i < n; i++) {
|
||||
double m = a[i-1] / d[i-1];
|
||||
d[i] -= m * a[i-1];
|
||||
b[i] -= m * b[i-1];
|
||||
}
|
||||
|
||||
// Back substitution
|
||||
vector<double> x(n);
|
||||
x[n-1] = b[n-1] / d[n-1];
|
||||
for (int i = n - 2; i >= 0; i--)
|
||||
x[i] = (b[i] - a[i] * x[i+1]) / d[i];
|
||||
|
||||
cout << "Solution:\n";
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << "x" << (i+1) << " = " << fixed << setprecision(6) << x[i] << "\n";
|
||||
|
||||
cout << "\nOperations: 7N - 6 = " << (7*n - 6) << " for N = " << n << "\n";
|
||||
return 0;
|
||||
}
|
||||
57
HW2_111550013/HW2_111550013_code/problem4.cpp
Normal file
57
HW2_111550013/HW2_111550013_code/problem4.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
// Problem 4: Jacobi and Gauss-Seidel methods
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
const double TOL = 1e-6;
|
||||
const int MAX_ITER = 100;
|
||||
|
||||
int main() {
|
||||
// Rearranged system (weakly diagonally dominant):
|
||||
// 7x1 - 3x2 + 4x3 = 6
|
||||
// 2x1 + 5x2 + 3x3 = -5
|
||||
// -3x1 + 2x2 + 6x3 = 2
|
||||
|
||||
double b[3] = {6, -5, 2};
|
||||
|
||||
// Jacobi
|
||||
cout << "Jacobi Method:\n";
|
||||
vector<double> x = {0, 0, 0};
|
||||
for (int iter = 1; iter <= MAX_ITER; iter++) {
|
||||
vector<double> x_old = x;
|
||||
x[0] = (b[0] + 3*x_old[1] - 4*x_old[2]) / 7;
|
||||
x[1] = (b[1] - 2*x_old[0] - 3*x_old[2]) / 5;
|
||||
x[2] = (b[2] + 3*x_old[0] - 2*x_old[1]) / 6;
|
||||
|
||||
double maxDiff = max({fabs(x[0]-x_old[0]), fabs(x[1]-x_old[1]), fabs(x[2]-x_old[2])});
|
||||
if (maxDiff < TOL) {
|
||||
cout << "Converged in " << iter << " iterations\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 3; i++)
|
||||
cout << "x" << (i+1) << " = " << fixed << setprecision(6) << x[i] << "\n";
|
||||
|
||||
// Gauss-Seidel
|
||||
cout << "\nGauss-Seidel Method:\n";
|
||||
x = {0, 0, 0};
|
||||
for (int iter = 1; iter <= MAX_ITER; iter++) {
|
||||
vector<double> x_old = x;
|
||||
x[0] = (b[0] + 3*x[1] - 4*x[2]) / 7;
|
||||
x[1] = (b[1] - 2*x[0] - 3*x[2]) / 5;
|
||||
x[2] = (b[2] + 3*x[0] - 2*x[1]) / 6;
|
||||
|
||||
double maxDiff = max({fabs(x[0]-x_old[0]), fabs(x[1]-x_old[1]), fabs(x[2]-x_old[2])});
|
||||
if (maxDiff < TOL) {
|
||||
cout << "Converged in " << iter << " iterations\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 3; i++)
|
||||
cout << "x" << (i+1) << " = " << fixed << setprecision(6) << x[i] << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user