-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton.cpp
205 lines (199 loc) · 3.56 KB
/
newton.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
the program works in the following way:
it contains a class ex which is used for creating individual expressions:
data members:
1)fn is used for storing coefficient and power/coeff. of angle
2)type is used to store the type of expression
3)exp to save +,-
member functions:
1)input : for storing the expression
2)print : for displaying the expression
3)dvt : for finding the derivative of the expression
4)solve : for solving for any value of x
5)tp & ch: returns the type and exp
6)md : chnages exp
the program inputs any equation using a linked list of epressions and the finding its derivative
functions:
pt :used for printing equation
fx :used for input of equation
dt :used for derivative of equation
sol:used for solving the equation for given value of x
the program uses newton method for finding the solution
newton method uses any random value of x, preferable if near solution
and the moves towards the solution by a jump of h
h=f(x)/f`(x)
this is done untill h starts repeating its value or f(x)-->0 i.e. h<<<<0
*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
class ex
{
private:
double fn[2][2];
char type[2],exp[2];
public:
ex* next;
char input()
{
cin>>fn[0][0]>>type[0]>>fn[0][1]>>exp[0];
if(type[0]=='y')
type[0]='x';
return exp[0];
}
void mod(char i)
{
exp[1]=i;
}
char print(double i)
{
cout<<fn[i][0]<<type[i]<<fn[i][1]<<" "<<exp[i]<<" ";
return exp[i];
}
char ch(double i)
{
return exp[i];
}
char tp()
{
return type[0];
}
void dvt()
{
exp[1]=exp[0];
fn[1][0]=fn[0][0]*fn[0][1];
fn[1][1]=fn[0][1];
type[1]=type[0];
if(type[0]=='x')
{
fn[1][1]=fn[0][1]-1;
}
else if(type[0]=='s')
{
type[1]='c';
}
else if(type[0]=='c')
{
type[1]='s';
}
else if(type[0]=='e');
}
double solve(double x, double i)
{
if(type[0]=='x')
{
if(x==0)
return 0;
else
{ if(fn[i][1]==0)
return fn[i][0];
else if(fn[i][1]<0)
return fn[i][0]*1/pow(x,fn[i][1]);
else
return fn[i][0]*pow(x,fn[i][1]);
}
}
else if(type[i]=='s')
{
return fn[i][0]*sin(fn[i][1]*x);
}
else if(type[i]=='c')
{
return fn[i][0]*cos(fn[i][1]*x);
}
else if(type[0]=='e')
{
return pow(2.718,fn[i][1])*fn[i][0];
}
return 0;
}
};
ex* fx(ex* n)
{
ex* nd;
n=nd=new ex;
char expn;
while(expn!='.')
{
expn=nd->input();
nd->next=new ex;
nd=nd->next;
}
return n;
}
void pt(ex* n)
{
ex* nd;
nd=n;
char expn;
expn=n->ch(0);
do
{
expn=nd->print(0);
nd=nd->next;
}while(expn!='.');
}
void dt(ex* n)
{
ex* nd;
ex* c;
nd=n;
char expn;
do
{
c=nd->next;
nd->dvt();
if(expn!='.')
if(c->tp()=='c')
{
if(nd->ch(0)=='+')
nd->mod('-');
else if(nd->ch(0)=='-')
nd->mod('+');
}
expn=nd->print(1);
nd=nd->next;
}while(expn!='.');
}
double sol(ex* n, double x, double i)
{
char expn;
expn=n->ch(i);
if(expn=='+')
return n->solve(x,i)+sol(n->next,x,i);
else if(expn=='-')
return n->solve(x,i)-sol(n->next,x,i);
else if(expn=='.')
return n->solve(x,i);
return 0;
}
void newton(ex* n)
{
double x0=3,h=1;
double flag;
h=sol(n,x0,0)/sol(n,x0,1);
do
{ h=h/pow(10,4);
flag=h;
h=sol(n,x0,0)/sol(n,x0,1);
x0=x0-h;
if(flag==h)
break;
h=h*pow(10,4);
}while(abs(h)>0.01);
cout<<"Ans found"<<x0<<"::"<<sol(n,x0,0);
}
void main()
{
clrscr();
ex* n;
cout<<"Enter the exp:";
n=fx(n);
pt(n);
cout<<"\n";
dt(n);
cout<<sol(n,2,0)<<":"<<sol(n,2,1)<<endl;
newton(n);
getch();
}