Computer Science Project 1
Computer Science Project 1
Mikolaj Kmin
June 5, 2024
1 The problem
We assume
...
f (x) = 4 x − 6ẍ + 4ẋ
a=1
α=0
β=4
γ = −6
δ=4
2 Initial transformations
Our system of differential equations:
z1 =x
z
2 = ẋ
z3 = ẍ
...
z4 = x
Initial conditions:
z1 (0) =0
z (0)
2 =1
z3 (0) =0
z4 (0) =0
3 Number of iterations
There is not an arbitrary best choice of the number of iterations, it depends on the desired
accuracy, and the computer specifications, as well as the problem we are trying to solve.
4 Number of iterations
There is not an arbitrary best choice of the number of iterations, it depends on the desired
accuracy, and the computer specifications, as well as the problem we are trying to solve.
Table 1: The table below shows the results of numerical integration together with their errors
in comparison with the real solution
Number of iterations Result error
2 11.534722 3.244629
4 14.374674 0.404678
8 14.742856 0.036495
16 14.775511 0.003840
32 14.777936 0.001416
64 14.778101 0.001251
Here the graph and the code for the computation of optimal number of iterations
Figure 1: The graph shows us that for our problem, the optimal number of iterations is about
32, as increasing it further doesn’t improve accuracy by an significant amount
#include <stdio.h>
#include <math.h>
int main() {
int n;
double x_0[4] = {0, 0, 1, 0}; // initial conditions
double t_0 = 0;
double t = 2; // final time
double exact = xExact(t);
printf("\n\n------The exact solution is %lf\n\n", exact);
for (int j = 1; j < 7; j++) {
printf("\n<< N = 2^ %d >>\n", j);
n = pow(2, j);
double h = (t - t_0) / n; // step size
double approx_rk4[4];
for (int i = 0; i < 4; i++) {
approx_rk4[i] = -x_0[i];
}
RungeKutta4(t_0, approx_rk4, h, n, approx_rk4);
printf("Runge-Kutta 4 method:\n");
printf("Approximate solution at t=%f: %f\n", t, approx_rk4[0]);
printf("Error: %f\n", err_rk4);
}
return 0;
}
5 Computation of x, v, and a, for t=2 together with the
graph presenting itterative process
#include <stdio.h>
#include <math.h>
return 4*x_triple_prime-6*x_double_prime+4*x_prime-x;
}
void runge_kutta_4 (double *x, double *x_prime, double *x_double_prime,
double *x_triple_prime, double h, double t, int n_steps)
{
double k1[4], k2[4], k3[4], k4[4];
k2[0] =
h * z_1 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k2[1] =
h * z_2 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k2[2] =
h * z_3 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k2[3] =
h * z_4 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[1] =
h * z_2 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[2] =
h * z_3 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[3] =
h * z_4 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
x_temp = *x + k3[0];
k4[0] =
h * z_1 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k4[1] =
h * z_2 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k4[2] =
h * z_3 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k4[3] =
h * z_4 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
double h = (t - t_0) / n;
double x = x_0[0];
double x_prime = x_0[1];
double x_double_prime = x_0[2];
double x_triple_prime = x_0[3];
//printf("Runge-Kutta 4 method:\n");
//printf("Approximate solution at t=%f: %f\n", t, x_prime);
// printf("Error: %f\n", err_rk4); // uncomment if you have defined err_rk4
return 0;
}
return 4*x_triple_prime-6*x_double_prime+4*x_prime-x;
}
void runge_kutta_4 (double *x, double *x_prime, double *x_double_prime,
double *x_triple_prime, double h, double t, int n_steps)
{
k2[0] =
h * z_1 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k2[1] =
h * z_2 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k2[2] =
h * z_3 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k2[3] =
h * z_4 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[0] =
h * z_1 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[1] =
h * z_2 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[2] =
h * z_3 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k3[3] =
h * z_4 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
x_temp = *x + k3[0];
k4[0] =
h * z_1 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k4[1] =
h * z_2 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k4[2] =
h * z_3 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
k4[3] =
h * z_4 (x_temp, x_prime_temp, x_double_prime_temp,
x_triple_prime_temp);
}
}
int main() {
int n = 32;
double x_0[4] = {0, 1, 0, 0}; // initial conditions
double t_0 = 0;
double step = 0.1;
for(double i = 0; i <= 2.01;){
double t = i; // final time
double h = (t - t_0) / n; // step size
double x = x_0[0];
double x_prime = x_0[1];
double x_double_prime = x_0[2];
double x_triple_prime = x_0[3];
return 0;
}
7.1 First we modify the initial conditions, the new initial conditions
are:
x(0) = 1
ẋ(0) = 2
ẍ(0) = 3
...
x (0) = 4
Figure 3: Results for new initial conditions
7.2 Now let’s change the range of t, returning to the orginal starting
conditions
(
t0 = 0.5
tn = 1
We also need to decrease the iteration step to 0.02, to acquire accurate results
Figure 4: Results for t ∈ ⟨ 12 , 1⟩
a=2
α=3
β = −2
γ=4
δ = −2
Figure 5: Results for new parameters