Вы находитесь на странице: 1из 10

Computer Project 1

Assignment 1.1

Newton’s method implementation program in C++ for finding the Root :

#include<bits/stdc++.h>
#define EPSILON 0.00001 //setting the error tolerance 10−5

using namespace std;

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;
}

void newton (double R)


//utility subprogram to find the root
{
double h = f (R) / df(R);
while (abs(h) >= EPSILON)
{
h = f(R) / df(R);
R = R - h;
}

cout << "The value of the root is : " << 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)

f(x) = exp(x−π) + cos x − x + π;

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

using namespace std;


double f(double x)
//initial function
{
return exp(x-pi) + cos(x)- x + pi;
}
double df(double x)
//derivative of the function
{
return exp(x-pi)-sin(x);}

void newton (double x)


//utility subprogram to find the root
{
Int k = 0;
double h = f (x) / df(x);
while (abs(h) >= EPSILON)
{
h = f(x) / df(x);
x = x - h;
}

cout << "The value of the root is : " << x;


cout <<"\nNr of interations: "<<k;
}

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)

So the improved program will look like this:

#include<bits/stdc++.h>
#define EPSILON 0.00001 //setting the error tolerance 10−5

using namespace std;

double f(double x)
//initial function
{
return exp(x-pi) + cos(x)- x + pi;
}

void newton (double x)


//utility subprogram to find the root
{
Int k = 0;
double h = f (x) / ((f (x + f(x))- f(x))/f(x));
while (abs(h) >= EPSILON)
{
h = f(x) / ((f(x + f(x))- f(x))/f(x));
x = x - h;
}

cout << "The value of the root is : " << x;


cout <<"\nNr of interations: "<<k;
}

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:

Which converges to 0 slowly.

b)
Bisection method program in C++ :

#include<bits/stdc++.h>

using namespace std;


double func(double x)
{
return cos(x)-1+x;
}

void bisection(double a, double b)


{ int i;
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}

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 :

In this case, bisection method Is preferable.

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;

//Utility function to compute Newton’s divided differences


void dividedDiffTable(float x[], float y[][10], int n)
{
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
y[j][i] = (y[j][i - 1] - y[j + 1] [i - 1]) / (x[j] -
x[i + j]);
}
}
}
//Printing the table
void printDiffTable(float y[][10],int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cout << setprecision(4) << y[i][j] << "\t ";
}
cout << "\n";
}
}

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 .

b) Using the same program as in a) we can compute Q4 :

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

q(x) = e Qx is more accurate.

Вам также может понравиться