Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
progof committed Nov 13, 2024
1 parent e3a51e3 commit 6a4c6b2
Show file tree
Hide file tree
Showing 26 changed files with 31,047 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.class
*.c
dist
node_modules
.env
24 changes: 24 additions & 0 deletions computational-methods-and-simulation/lab-2/task-1/dokladnosc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <gsl/gsl_ieee_utils.h>

int main(void)
{
float f = 1.0 / 3.0;
double d = 1.0 / 3.0;

double fd = f; /* promote from float to double */

printf(" f=");
gsl_ieee_printf_float(&f);
printf("\n");

printf("fd=");
gsl_ieee_printf_double(&fd);
printf("\n");

printf(" d=");
gsl_ieee_printf_double(&d);
printf("\n");

return 0;
}
13 changes: 13 additions & 0 deletions computational-methods-and-simulation/lab-2/task-1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main(void)
{
double x = 5.0;
double y_J0 = gsl_sf_bessel_J0(x);
double y_ieee = gsl_ieee_printf_double(x);

printf("J0 (%g) = %.18e\n", x, y_J0);
printf("IEEE (%g) = %s\n", x, y_ieee);
return 0;
}
59 changes: 59 additions & 0 deletions computational-methods-and-simulation/lab-3/task-1/interpolacja.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_interp.h>

static double fun(double x)
{
return 1.0 / (1.0 + 25.0 * x * x);
}

void interpolate(const char *filename, const gsl_interp_type *method, double *x, double *y, int steps, double a, double b)
{
gsl_interp_accel *acc = gsl_interp_accel_alloc();
gsl_spline *spline = gsl_spline_alloc(method, steps + 1);
gsl_spline_init(spline, x, y, steps + 1);

FILE *output = fopen(filename, "w");
double xi;
for (xi = a; xi <= b; xi += 0.01)
{
double yi = gsl_spline_eval(spline, xi, acc);
fprintf(output, "%g %g\n", xi, yi);
}

fclose(output);
gsl_spline_free(spline);
gsl_interp_accel_free(acc);
}

int main(void)
{
const double a = -1.0;
const double b = 1.0;
const int steps = 10;
double x[100], y[100], dx;
FILE *input;
int i;

input = fopen("wartosci.txt", "w");

dx = (b - a) / (double)steps;

for (i = 0; i <= steps; ++i)
{
x[i] = a + (double)i * dx;
y[i] = fun(x[i]);
fprintf(input, "%g %g\n", x[i], y[i]);
}

fclose(input);

interpolate("inter_wielomian.txt", gsl_interp_polynomial, x, y, steps, a, b);
interpolate("inter_liniowa.txt", gsl_interp_linear, x, y, steps, a, b);
interpolate("inter_kubiczna.txt", gsl_interp_cspline, x, y, steps, a, b);

return 0;
}
66 changes: 66 additions & 0 deletions computational-methods-and-simulation/lab-4/task-1/approximation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_chebyshev.h>
#include <sys/stat.h>

double f1(double x)
{
return exp(x) * cos(0.5 * x * x);
}

double f2(double x)
{
return 1.0 / (12.0 * x * x + 1.0);
}

double func_wrapper(double x, void *params)
{
double (*f)(double) = params;
return f(x);
}

void aproksymacja(double (*f)(double), double a, double b, int n, const char *filename)
{
gsl_cheb_series *cheb = gsl_cheb_alloc(n);

gsl_function F;
F.function = &func_wrapper;
F.params = f;

gsl_cheb_init(cheb, &F, a, b);

FILE *fp = fopen(filename, "w");
if (!fp)
{
fprintf(stderr, "Error opening file: %s\n", filename);
gsl_cheb_free(cheb);
return;
}

double x, y, y_cheb;
for (x = a; x <= b; x += 0.01)
{
y = f(x);
y_cheb = gsl_cheb_eval(cheb, x);
fprintf(fp, "%g %g %g\n", x, y, y_cheb);
}

fclose(fp);
gsl_cheb_free(cheb);
}

int main()
{
const char *dirname = "data";
mkdir(dirname, 0777);

aproksymacja(f1, 0, 3, 3, "data/f1_n3.dat");
aproksymacja(f1, 0, 3, 10, "data/f1_n10.dat");
aproksymacja(f1, 0, 3, 40, "data/f1_n40.dat");

aproksymacja(f2, -2, 2, 3, "data/f2_n3.dat");
aproksymacja(f2, -2, 2, 10, "data/f2_n10.dat");
aproksymacja(f2, -2, 2, 40, "data/f2_n40.dat");

return 0;
}
59 changes: 59 additions & 0 deletions computational-methods-and-simulation/lab-4/task-2/approximation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_chebyshev.h>
#include <sys/stat.h>

double f3(double x)
{
double A = 0.05;
double B = 50.0;
return cos(x) + A * sin(B * x);
}

double func_wrapper(double x, void *params)
{
double (*f)(double) = params;
return f(x);
}

void aproksymacja(double (*f)(double), double a, double b, int n, const char *filename)
{
gsl_cheb_series *cheb = gsl_cheb_alloc(n);

gsl_function F;
F.function = &func_wrapper;
F.params = f;

gsl_cheb_init(cheb, &F, a, b);

FILE *fp = fopen(filename, "w");
if (!fp)
{
fprintf(stderr, "Error opening file: %s\n", filename);
gsl_cheb_free(cheb);
return;
}

double x, y, y_cheb;
for (x = a; x <= b; x += 0.01)
{
y = f(x);
y_cheb = gsl_cheb_eval(cheb, x);
fprintf(fp, "%g %g %g\n", x, y, y_cheb);
}

fclose(fp);
gsl_cheb_free(cheb);
}

int main()
{
const char *dirname = "data";
mkdir(dirname, 0777);

aproksymacja(f3, 0, 10, 3, "data/f3_n3.dat");
aproksymacja(f3, 0, 10, 10, "data/f3_n10.dat");
aproksymacja(f3, 0, 10, 40, "data/f3_n40.dat");

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846

// Funkcje do całkowania
double f1(double x)
{
return x * x + x + 1;
}

double f2(double x)
{
return sqrt(1 - x * x);
}

double f3(double x)
{
if (x <= 0)
return NAN;
return 1 / sqrt(x);
}

// Funkcja obliczająca całkę metodą prostokątów
double rectangle_integral(double (*f)(double), double a, double b, int n)
{
double h = (b - a) / n;
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += f(a + i * h);
}
return sum * h;
}

int main()
{
double a = 0, b = 1;

// Tolerancje błędów
double tolerances[] = {1e-3, 1e-4, 1e-5, 1e-6};

// Funkcja f1(x) = x^2 + x + 1
printf("Funkcja f1(x) = x^2 + x + 1:\n");
double exact_value_f1 = 5.0 / 3.0; // Dokładna wartość
printf("Dokładna wartość całki: %.10f\n", exact_value_f1);
for (int i = 0; i < 4; i++)
{
double tolerance = tolerances[i];
int n = 4;
double result, error;
do
{
result = rectangle_integral(f1, a, b, n);
error = fabs(exact_value_f1 - result);
if (error <= tolerance)
break;
n *= 2;
} while (n <= 1000000);
printf("Podprzedziały: %d | Przybliżona całka: %.10f | Błąd: %.10f (dla dokładności %.1e)\n", n, result, error, tolerance);
}

// Funkcja f2(x) = sqrt(1 - x^2)
printf("\nFunkcja f2(x) = sqrt(1 - x^2):\n");
double exact_value_f2 = PI / 4.0;
printf("Dokładna wartość całki: %.10f\n", exact_value_f2);
for (int i = 0; i < 4; i++)
{
double tolerance = tolerances[i];
int n = 4;
double result, error;
do
{
result = rectangle_integral(f2, a, b, n);
error = fabs(exact_value_f2 - result);
if (error <= tolerance)
break;
n *= 2;
} while (n <= 1000000);
printf("Podprzedziały: %d | Przybliżona całka: %.10f | Błąd: %.10f (dla dokładności %.1e)\n", n, result, error, tolerance);
}

// Funkcja f3(x) = 1/sqrt(x)
printf("\nFunkcja f3(x) = 1/sqrt(x):\n");
double exact_value_f3 = 2.0;
printf("Dokładna wartość całki: %.10f\n", exact_value_f3);
for (int i = 0; i < 4; i++)
{
double tolerance = tolerances[i];
int n = 4;
double result, error;
do
{
result = rectangle_integral(f3, a + 0.0001, b, n);
error = fabs(exact_value_f3 - result);
if (error <= tolerance)
break;
n *= 2;
} while (n <= 1000000);
printf("Podprzedziały: %d | Przybliżona całka: %.10f | Błąd: %.10f (dla dokładności %.1e)\n", n, result, error, tolerance);
}

return 0;
}
Loading

0 comments on commit 6a4c6b2

Please sign in to comment.