Solutions for FCUP's Numerical Analysis Assignments

This commit is contained in:
Diogo Cordeiro
2019-08-28 16:44:59 +01:00
commit b7b704b68b
108 changed files with 35323 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
#include <iostream>
double machine_eps() {
double epsilon_candidate = 2.0,
epsilon = epsilon_candidate,
power = 2.0;
while (epsilon_candidate != 1.0)
{
epsilon = epsilon_candidate;
epsilon_candidate -= 1/power;
power *= 2;
}
return epsilon-1.0;
}
int main() {
std::cout.precision(17);
std::cout << machine_eps() << " - macheps" << '\n';
/*for(double step = 0.0f; step < 100'000'000; step += 1.0)
{
std::cout << step << " " << machine_eps(step) << '\n';
}*/
}

View File

@@ -0,0 +1,22 @@
#include <iostream>
double
machine_eps(double start)
{
double epsilon = 1.0f;
while ((start + 0.5f * epsilon) != start)
{
epsilon = 0.5f * epsilon;
}
return epsilon;
}
int
main()
{
std::cout.precision(17);
std::cout << machine_eps(1) << " - macheps" << '\n';
}

View File

@@ -0,0 +1,56 @@
#include <iostream>
#include <cmath>
#include <cstdlib>
/**
* Compute k term of Serie 2 given the previous one
*/
double _compute_serie_2_term(unsigned long k, double ak) {
return double(k+1)/(double(4*k+6)) * ak;
}
// double c(int k) {
// return std::pow(std::tgamma(k + 1), 2.0)/std::tgamma(2*k + 2);
// }
/**
* Compute serie 2 with absole error below a given epsilon
*/
void compute_serie_2(double epsilon) {
// Constant factor
double factor = 9.0/(2.0*std::sqrt(3));
// Taken from D'Alembert criterion for given that L = 0.5 < 1
double super_L = 1.0/(1.0-0.25);
// Summation index
unsigned long k = 0;
// Acumulation for the Summation
double acc = 0;
// Previous and current value on the series
// double a_prev = 1.0f, // a with k = 0
double a = 1.0; // _compute_serie_2_term(k, a_prev); // compute next
// While our absolute error is higher than the given epsilon
while(epsilon < factor*a*super_L) {
// Accumulate with previous term
acc += a;
// Update previous term to current term
// a_prev = a;
// Compute current term
// a = c(k);
a = _compute_serie_2_term(k++, a);
}
std::cout << k << " " << std::abs(M_PI - factor*acc) << '\n';
std::cout << factor * acc << " | " << k;
}
int main(int, char **argv) {
std::cout.precision(17);
double power_of_ten = std::pow(10.0, -std::atoi(argv[1]));
compute_serie_2(power_of_ten);
}

View File

@@ -0,0 +1,31 @@
#include <iostream>
#include <cmath>
#include <numeric>
#include <ios>
#include <cstdlib>
double compute_serie_3_term(unsigned long n) {
return -((double)(2*n+1))/(double)(2*n+3);
}
double compute_serie_3(double err) {
unsigned long k = 0;
double ak = 1;
double acc = 1;
while (err < 4*std::abs(ak)) {
ak *= compute_serie_3_term(k++);
acc += ak;
}
std::cout << k << " " << 4*acc << " " << std::abs(3.14159265358979324$$ - 4*acc) << '\n';
return 4*acc;
}
int main(int, char **argv) {
const double err = std::pow(10.0, -std::atof(argv[1]));
std::cout.precision(17);
compute_serie_3(err);
}

View File

@@ -0,0 +1,16 @@
public class Question1 {
public static void main(String args[]) {
System.out.println(machine_eps());
}
public static float machine_eps() {
float epsilon = 1.0f;
while ((1.0f + 0.5f * epsilon) != 1.0f){
epsilon = 0.5f * epsilon;
}
return epsilon;
}
}

View File

@@ -0,0 +1,49 @@
public class Question2 {
public static void main(String[] args) {
double power_of_ten = Math.pow(10,-1 * Integer.parseInt(args[0]));
compute_serie_2(power_of_ten);
}
/**
* Compute serie 2 with absole error below a given epsilon
*/
public static void
compute_serie_2(double epsilon)
{
// Constant factor
double factor = 9.0f/(double)(2.0f*Math.sqrt(3));
// Taken from D'Alembert criterion for given that L = 0.5 < 1
double super_L = 1.0f/(double)(1-0.25);
// Summation index
int k = 0;
// Acumulation for the Summation
double acc = 0;
// Previous and current value on the series
double a = 1.0f; // a with k = 0
// While our absolute error is higher than the given epsilon
while(epsilon < factor*a*super_L)
{
// Accumulate with previous term
acc += a;
// Compute current term
a = _compute_serie_2_term(k++) * a;
}
// System.out.println(epsilon);
// System.out.println("Value: " + factor*acc);
// System.out.println("Error: " + (3.1415926535897932 - factor*acc));
// System.out.println("K: " +(k-1) + "\n--------------");
System.out.println(factor*acc + " " + (k - 1));
}
/**
* Compute k term of Serie 2 given the previous one
*/
public static double
_compute_serie_2_term(int k)
{
return (double)(k+1.0f)/(double)(4.0f*k+6.0f);
}
}

View File

@@ -0,0 +1,28 @@
public class Question3 {
public static void main(String[] args) {
double err = Math.pow(10, -Integer.parseInt(args[0].trim()));
compute_serie_2(err);
}
public static double compute_serie_3_term(long n)
{
return -((double)(2*n+1))/(double)(2*n+3);
}
public static void compute_serie_2(double err)
{
long k = 0;
double ak = 1;
double acc = 1;
while (err < 4 * Math.abs(ak)) {
ak = compute_serie_3_term(k++) * ak;
acc += ak;
}
System.out.println(k + " " + 4 * acc);
}
}