CLL113: Numerical Methods for Chemical Engineers Lab
Tutorial Number: 1
Name: Lakshya Mahajan
Entry No.: 2022CH11429
Question 1:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double new_cos(double x)
{
double cos = 1., error = 0.5e-8, term = 1.;
int i = 1;
term = (double)term * (-1 * (x * x) / ((2 * i) * (2 * i - 1)));
while (abs(term / (cos)) > error)
{
i++;
cos = cos + term;
term = (double)term * (-1 * (x * x) / ((2 * i) * (2 * i - 1)));
}
cout << "Approximated cos value: " << cos << endl;
return i;
}
int main()
{
cout << setprecision(16);
cout << new_cos(0.3 * M_PI);
return 0;
}
Output:
Important points:
• The term error has been set to _ considering the value of Scarborough number
where n is the number of significant figures required.
• Used the concept of approximation error by comparing with the _ term.
• iomanip.setprecision(16) has been used to increase the limit of stdout for doubles in C++.
• According to Google, has a value of 0.58778525229 which has values upto eight
decimal places matching with the output of the code above. Rest of the decimal point values are
visible only due to the setprecision function.
Conclusion:
The number of steps required to calculate for is 6 .
Question 2:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float one_over_i_four(float i)
{
return (float)(1 / (float)(i * i * i * i));
}
int main()
{
cout << setprecision(12);
float sum_forward = 0.;
for (int i = 1; i <= 10000; i++)
{
sum_forward += one_over_i_four((float)i);
}
float sum_backward = 0.;
for (int k = 10000; k >= 1; k--)
{
sum_backward += one_over_i_four((float)k);
}
double true_value = (pow(M_PI, 4) / 90);
cout << "Sum when i goes from 1 to 10000: " << sum_forward << endl;
cout << "Sum when i goes from 10000 to 1: " << sum_backward << endl;
cout << "True value: " << true_value << endl;
cout << "True percent relative error (forward): " << 100 * abs((true_value -
(double)sum_forward) / (true_value)) << "%" << endl;
cout << "True percent relative error (backward): " << 100 * abs((true_value -
(double)sum_backward) / (true_value)) << "%" << endl;
return 0;
}
Output:
Important points:
• The function one_over_i_four has a return type of float which conforms to the question’s
‘single precision’ requirement.
• The variables i and k are type-casted into float before passing them into the function to still
maintain the single precision.
• double data type is used to output the true value of the provided sum.
Conclusion:
Sum in both the cases (going forward and backward) are not equal. This is due to the round-off errors
occurring during the calculation using the float data points.
True percent relative errors:
1) Going forwards: 0.000102838468153%
2) Going backwards: 3.71063161242e-06%
The slight difference in the forward and reverse sums is due to the finite precision of floating-point
arithmetic.
Question 3:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double iter_sqrt(double a, double x_0, double error)
{
double true_value = sqrt((double)a);
int i = 1;
cout << "Original guess: " << x_0 << endl;
double t_p_r_e;
cout << "Iteration 0"
<< " | " << x_0 << " | " << abs((true_value - x_0) / true_value) * 100 << " %"
<< " | approximate: ------- %" << endl;
double x_new = 1. / 2. * ((x_0) + a / (x_0));
t_p_r_e = (abs((true_value - x_new) / (true_value)) * 100);
double a_p_r_e = abs((x_new - x_0) / x_new) * 100;
cout << "Iteration " << i << " | " << x_new << " | " << t_p_r_e << " %"
<< " | approximate: " << a_p_r_e << " %" << endl;
while (abs(x_new - true_value) > error || abs((x_new - x_0) > error))
{
i++;
x_0 = x_new;
x_new = 1. / 2. * ((x_new) + a / (x_new));
t_p_r_e = (abs((true_value - x_new) / (true_value)) * 100);
a_p_r_e = (abs((x_0 - x_new) / (x_new)) * 100);
cout << "Iteration " << i << " | " << x_new << " | "
<< "true: " << t_p_r_e << " %"
<< " | approximate: " << a_p_r_e << " %" << endl;
}
i = 1;
return x_new;
}
int main()
{
cout << setprecision(10);
int a = 5;
double x_0 = 0.005, tolerance = 1e-6;
cout << iter_sqrt((double)a, x_0, tolerance);
}
Output:
Important points:
• Approximate error does not exist for the first iteration numbered 0 as we do not have a
for the expression.
• As soon as tolerance value for either of the errors is met, the iterative loop is broken.
Conclusion:
12 iterative steps have to be taken to achieve an error below the tolerance value of for calculating
the value of square root of 5 using the iterative formula using initial guess as .