Computer Project 1: Assignment 1.1
Computer Project 1: Assignment 1.1
Assignment 1.1
#include<bits/stdc++.h>
#define EPSILON 0.00001 //setting the error tolerance 10−5
double f(double R)
//initial function
{
return -1/(19.01 + 273.15)+ 1.129241 * 0.001 + 2.341077 * 0.0001* log(R) +
8.775468 *0.00000001*sqrt(log(R))*log(R);
}
double df(double R)
//derivative of the function
{
return 2.341077 * 0.0001/R+ 3*2*8.775468 *0.00000001/R;
}
int main()
{
double R0 = 15000; // Initial guess
newton (R0);
return 0;
}
The value of the root is : 17773.7 .
Using the same program for the second equation, we can compute the second root;
The value of the root is : 17791.4 .
Assignment 1.2
a)
f(x) =exp(x-pi) + cos(x) - x + pi
5
f(x)
4.5
3.5
2.5
y
1.5
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x
b)
Using the program from the assignment 1.1 we can solve the f(x) = 0;
With initial guess 3:
#include<bits/stdc++.h>
#define EPSILON 0.00001 //setting the error tolerance 10−5
int main()
{
double x0 = 3; // Initial guess
newton (x0);
return 0;
}
OUTPUT :
We can see that the nr. of iterations is relatively big due to its order of convergence which is
approximately 1. This is happening because Newton’s method uses derivative. In order to
improve it we can write instead of f ’(x) :
c)
#include<bits/stdc++.h>
#define EPSILON 0.00001 //setting the error tolerance 10−5
double f(double x)
//initial function
{
return exp(x-pi) + cos(x)- x + pi;
}
int main()
{
double x0 = 3; // Initial guess
newton (x0);
return 0;
}
The nr. of iterations is much smaller than before.
d)
Fixed point iteration program impimentation in C
#define pi 3.141592
#define g(x) exp(x - pi)+cos(x)+pi
int main()
{
float x0 = 3; //initial guess
int i; //nr of iterations
for (i = 1; i<326; i++)
{
printf("x%d = %f\n",i,g(x0));
x0 = g(x0);
}
printf("The solution is %f",x0);
}
OUTPUT:
The convergence is slower than Newtons method in this case for 326 iterations.
Assignment 1.3
a)
using the precedent program for fixed point iteration with initial guess x0 = 0.1 and 100 iteration
we get:
b)
Bisection method program in C++ :
#include<bits/stdc++.h>
double c = a;
for(i = 0; i<100; i++)
{
c = (a+b)/2;
if (func(c) == 0.0)
break;
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
int main()
{
// Initial values assumed
double a = -100, b = 100;
bisection(a, b);
return 0;
}
Output :
d)
To speed up the convergence we can apply newton’s method which we used before.
Assignment 1.4
a)
C++ program for calculating Newton devided differences and finding interpolating polynomial
P4 :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n = 5;
float value, sum, y[10][10];
float x[] = { 1,2,3,4,5 }; //input of x
//input of y
y[0][0] = 1;
y[1][0] = 1;
y[2][0] = 2;
y[3][0] = 6;
y[4][0] = 24;
dividedDiffTable(x, y, n);
printDiffTable(y,n);
// interpolating polynomial P4(x)
cout<<"\nP4 = "<<y[0][0]<<"+ (x - "<<x[0]<<")"<<y[0][1]<<" + "<<"(x -
"<<x[0]<<")"<<"(x - "<<x[1]<<")"<<y[0][2]<<" + "<<"(x -
"<<x[0]<<")"<<"(x - "<<x[1]<<")(x - "<<x[2]<<")"<<y[0][3]<<" + "<<"(x
-"<<x[0]<<")(x - "<<x[1]<<")"<<"(x - "<<x[2]<<")"<<"(""x -
"<<x[3]<<")"<<y[0][4];
return 0;
}
Result:
y D1 D2 D3 D4
P4 ( x) = 1+ (x - 1)0 + (x - 1)(x - 2)0.5 + (x - 1)(x - 2)(x - 3)0.3333 + (x -1)(x - 2)(x - 3)(x - 4)0.375
can be reduced to
P4 (x) = 0.37 x 4 – 3.4 x 3 +¿11.6 x 2-16.5x + 8 .
y D1 D2 D3 D4
Q 4 (x) = 0+ (x - 1)0 + (x - 1)(x - 2)*0.3466 + (x - 1)(x - 2)(x - 3)*-0.04795 + (x -1)(x - 2)(x - 3)(x -4)*0.007079
can be simplified as :
Q 4 ( x) = 0.007079 x 4 - 0.1187 x 3 + 0.8820 x 2 – 1.9212x +1.0548 .
4 3 2
Then, q(x) = e 0.007079 x −0.1187 x +0.8820 x – 1.9212 x+1.0548
c) Graphic representation of the above functions we achieved :
P4 ( x) = 0.37 x 4 – 3.4 x 3 +¿11.6 x 2-16.5x + 8 .
4 3 2
q(x) = e 0.007079 x −0.1187 x +0.8820 x – 1.9212 x+1.0548
25
f(x)= P4
g(x)=exp(Q4)
20 h(x)=Gamma
15
10
y
-5
1 1.5 2 2.5 3 3.5 4 4.5 5
x
d)
= 2.2500
= 2.1178