-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewtonsMethod.h
66 lines (58 loc) · 2.13 KB
/
NewtonsMethod.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<functional>
using namespace std;
#define a 1 // pre-processor directive to define a and iteration
#define iteration 100
class NewtonsMethod
{
private:
function<double(double)>f; //private member of class NewtonsMethod
function<double(double)>df; // conatining function f and its derivative df
public:
NewtonsMethod(function<double(double)>F, function<double(double)>dF) :f(F), df(dF){}; //contructor
double Calculate(double t); //member function to calculate successive solution
double display(double,double,double,double); //member function to display solution
};
double F(double t,double Fr) //definition of contructor argument F returns f at t and Fr
{
return 0.55*t-0.3*sin(a*t)-Fr;
}
double dF(double t) //definition of contructor argument dF return df at time t
{
return 0.55 - 0.3*cos(a*t);
}
double NewtonsMethod::display(double t, double i,double f,double e) //definition of member function display(....)
{
cout << fixed << setprecision(6); //sets output precision to 6 decimal points
cout <<i<<"\t\t"<<t<<" sec\t\t"<<f<<" N\t\t"<<e<<endl; //prints iteration,time,force and erroe
return(0);
}
double NewtonsMethod::Calculate(double t) //member function definition
{
int i=1;
double epsilon;
double t0;
cout << "-------------------------------------------------------------------------------------"<<endl;
cout << "Itertion\t\tt\t\t\tf(x)\t\t\terror"<< endl;
cout << "-------------------------------------------------------------------------------------"<<endl;
do
{
t0 = t - f(t) / df(t);
epsilon = t0 - t;
display(t0,i,f(t0),epsilon);
t = t0;
i++;
if (i > iteration) //if counter exceeds iteration program terminates with early convergence
{
cout << "Cannot converge !";
exit(0);
}
} while (fabs(epsilon) >= 0.001);
cout << "-------------------------------------------------------------------------------------"<< endl;
cout << "The earliest time when f(x) converges is " <<t<<" sec\n";
return t0;
}