show error

This commit is contained in:
Elizabeth Hunt 2023-10-13 17:54:47 -06:00
parent e16dc49abc
commit cae58e90e0
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
2 changed files with 23 additions and 17 deletions

Binary file not shown.

View File

@ -1,10 +1,13 @@
#include "lizfcm.h" #include "lizfcm.h"
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
double f(double x) { return (x - 1) / (x + 1); } double f(double x) { return (x - 1) / (x + 1); }
int main() { int main() {
char s[2048];
printf("Basic Routines\n"); printf("Basic Routines\n");
printf("smaceps(): %.10e\n", smaceps()); printf("smaceps(): %.10e\n", smaceps());
printf("dmaceps(): %.10e\n", dmaceps()); printf("dmaceps(): %.10e\n", dmaceps());
@ -12,7 +15,6 @@ int main() {
printf("Norm, Distance\n"); printf("Norm, Distance\n");
Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3}); Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3});
char s[2048];
strcpy(s, ""); strcpy(s, "");
format_vector_into(v, s); format_vector_into(v, s);
printf("v: %s", s); printf("v: %s", s);
@ -65,17 +67,19 @@ int main() {
printf("========\n"); printf("========\n");
printf("LU Decomp\n"); printf("LU Decomp\n");
Matrix_double *m = InitMatrixWithSize(double, 10, 10, 0.0); uint32_t n = 10;
for (int i = 0; i < 10; i++) { Matrix_double *a = InitMatrixWithSize(double, n, n, 0.0);
for (int j = 0; j < 10; j++) for (int i = 0; i < n; i++) {
m->data[i]->data[j] = (100 - rand() % 200); for (int j = 0; j < n; j++)
a->data[i]->data[j] = (100 - rand() % 200);
} }
strcpy(s, ""); strcpy(s, "");
format_matrix_into(m, s); format_matrix_into(a, s);
printf("m = %s", s); printf("a = %s", s);
Array_double *b = InitArrayWithSize(double, 10, 100.0); uint32_t solution = 100;
Matrix_double **u_l = lu_decomp(m); Array_double *b = InitArrayWithSize(double, n, (double)solution);
Matrix_double **u_l = lu_decomp(a);
Matrix_double *u = u_l[0]; Matrix_double *u = u_l[0];
Matrix_double *l = u_l[1]; Matrix_double *l = u_l[1];
@ -89,26 +93,28 @@ int main() {
format_vector_into(b, s); format_vector_into(b, s);
printf("b = %s", s); printf("b = %s", s);
printf("========\n"); printf("========\n");
printf("Backward -> Forward Substitution\n"); printf("Forward / Backward Substitution Solution to ax=b\n");
Array_double *b_fsub = fsubst(l, b); Array_double *b_fsub = fsubst(l, b);
free_vector(b);
strcpy(s, ""); strcpy(s, "");
format_vector_into(b_fsub, s); format_vector_into(b_fsub, s);
printf("x: %s\n", s); printf("b_fsub: %s", s);
Array_double *x_bsub = bsubst(u, b_fsub); Array_double *x_bsub = bsubst(u, b_fsub);
strcpy(s, ""); strcpy(s, "");
format_vector_into(x_bsub, s); format_vector_into(x_bsub, s);
printf("x: %s", s); printf("x_bsub: %s", s);
free_vector(b_fsub); free_vector(b_fsub);
printf("Verifications (each should be approximately 100)\n"); printf("Verifications\n");
for (size_t row = 0; row < m->rows; row++) { for (size_t row = 0; row < a->rows; row++) {
double curr = 0; double curr = 0;
for (size_t col = 0; col < m->cols; col++) for (size_t col = 0; col < a->cols; col++)
curr += m->data[row]->data[col] * x_bsub->data[col]; curr += a->data[row]->data[col] * x_bsub->data[col];
printf("Substitution for row %zu = %f\n", row, curr); printf("Substituions for values in row %zu = %f, true value err=%.10e\n",
row, curr, fabs(curr - solution));
} }
return 0; return 0;