@@ -65,7 +65,7 @@ <h2>Newton's method</h2>
65
65
$$\begin{equation*}
66
66
x_{n+1} = x_{n} - \frac{f(x_n)}{f'(x_n)}
67
67
\end{equation*}$$
68
- < p > If the initial guess $x_0$ is close enough to the solution and $f'(x_0 ) \neq 0$, the method usually converges.</ p >
68
+ < p > If the initial guess $x_0$ is close enough to the solution and $f'(x_n ) \neq 0$, the method usually converges.</ p >
69
69
</ td > </ tr >
70
70
</ table >
71
71
@@ -121,114 +121,94 @@ <h2>Newton's method</h2>
121
121
{
122
122
return x - f ( x ) / grad_f ( x ) ;
123
123
}
124
-
125
- plotFunctions ( )
126
- {
127
- let x = - 3 ;
128
- let y = 0.5 ;
129
- let num_steps = 5000 ;
124
+
125
+ computeData ( fct , grad_fct , x0 , data )
126
+ {
130
127
let xValues = [ ] ;
131
- let yValues_quadratic = [ ] ;
132
- let yValues_cubic = [ ] ;
133
- let yValues_trigonometric = [ ] ;
128
+ let yValues = [ ] ;
129
+ let x_newton = [ ]
130
+ let y_newton = [ ]
131
+ let current_x = x0
134
132
135
-
133
+ let x = - 3 ;
134
+ let num_steps = 5000 ;
136
135
for ( let i = 0 ; i <= num_steps ; i ++ )
137
136
{
138
137
xValues . push ( x ) ;
139
- yValues_quadratic . push ( this . quadratic_function ( x ) ) ;
140
- yValues_cubic . push ( this . cubic_function ( x ) ) ;
141
- yValues_trigonometric . push ( this . trigonometric_function ( x , y ) ) ;
142
-
138
+ yValues . push ( fct ( x ) ) ;
143
139
x += 6 / num_steps ;
144
- y += 1.0 / num_steps ;
145
140
}
146
141
147
- let current_x = 3.0
142
+ x_newton . push ( current_x )
143
+ y_newton . push ( 0 )
144
+ for ( let i = 0 ; i < this . num_newton_steps ; i ++ )
145
+ {
146
+ x_newton . push ( current_x )
147
+ y_newton . push ( fct ( current_x ) )
148
+ current_x = this . newton_step ( fct , grad_fct , current_x )
149
+ x_newton . push ( current_x )
150
+ y_newton . push ( 0 )
151
+ }
152
+
153
+ var trace_quadratic = {
154
+ x : xValues ,
155
+ y : yValues ,
156
+ name : "function" ,
157
+ showlegend : true
158
+ } ;
159
+
160
+ data . push ( trace_quadratic ) ;
161
+ for ( let i = 0 ; i < this . num_newton_steps + 1 ; i ++ )
162
+ {
163
+ if ( i < this . num_newton_steps )
164
+ data . push ( {
165
+ type : 'line' ,
166
+ x : [ x_newton [ 2 * i + 1 ] , x_newton [ 2 * i + 2 ] ] ,
167
+ y : [ y_newton [ 2 * i + 1 ] , y_newton [ 2 * i + 2 ] ] ,
168
+ line : {
169
+ color : 'rgb(0, 127, 0)' ,
170
+ width : 2 ,
171
+ } ,
172
+ name : "tangent" ,
173
+ showlegend : i == 0
174
+ } )
175
+
176
+
177
+ data . push ( {
178
+ type : 'line' ,
179
+ x : [ x_newton [ 2 * i ] , x_newton [ 2 * i + 1 ] ] ,
180
+ y : [ y_newton [ 2 * i ] , y_newton [ 2 * i + 1 ] ] ,
181
+ line : {
182
+ color : 'rgb(0, 0, 0)' ,
183
+ width : 2 ,
184
+ dash : "dash" ,
185
+ } ,
186
+ text : [ "x_" + i . toString ( ) , "" ] ,
187
+ textposition : "bottom center" ,
188
+ mode :'lines+markers+text' ,
189
+ name : "x_n" ,
190
+ showlegend : i == 0
191
+ } )
192
+ }
193
+ }
194
+
195
+ plotFunctions ( )
196
+ {
148
197
var data = [ ] ;
149
198
if ( this . fct == "Quadratic function" )
150
199
{
151
- let x_newton = [ ]
152
- let y_newton = [ ]
153
- for ( let i = 0 ; i < this . num_newton_steps ; i ++ )
154
- {
155
- x_newton . push ( current_x )
156
- y_newton . push ( this . quadratic_function ( current_x ) )
157
- current_x = this . newton_step ( this . quadratic_function , this . grad_quadratic_function , current_x )
158
- x_newton . push ( current_x )
159
- y_newton . push ( 0 )
160
- }
161
-
162
- var trace_quadratic = {
163
- x : xValues ,
164
- y : yValues_quadratic ,
165
- name : "quadr. fct."
166
- } ;
167
-
168
- var trace_quadratic_newton = {
169
- x : x_newton ,
170
- y : y_newton ,
171
- name : "Newton - quadr. fct."
172
- } ;
173
- data = [ trace_quadratic , trace_quadratic_newton ] ;
200
+ this . computeData ( this . quadratic_function , this . grad_quadratic_function , 3.0 , data )
174
201
}
175
202
176
203
if ( this . fct == "Cubic function" )
177
204
{
178
- let x_newton = [ ]
179
- let y_newton = [ ]
180
- for ( let i = 0 ; i < this . num_newton_steps ; i ++ )
181
- {
182
- x_newton . push ( current_x )
183
- y_newton . push ( this . cubic_function ( current_x ) )
184
- current_x = this . newton_step ( this . cubic_function , this . grad_cubic_function , current_x )
185
- x_newton . push ( current_x )
186
- y_newton . push ( 0 )
187
- }
188
-
189
- var trace_cubic = {
190
- x : xValues ,
191
- y : yValues_cubic ,
192
- name : "cubic fct."
193
- } ;
194
-
195
- var trace_cubic_newton = {
196
- x : x_newton ,
197
- y : y_newton ,
198
- name : "Newton - cubic fct."
199
- } ;
200
- data = [ trace_cubic , trace_cubic_newton ] ;
205
+ this . computeData ( this . cubic_function , this . grad_cubic_function , 3.0 , data )
201
206
}
202
207
203
208
if ( this . fct == "Trigonometric function" )
204
209
{
205
- current_x = 0.465
206
- let x_newton_trig = [ ]
207
- let y_newton_trig = [ ]
208
- console . log ( this . num_newton_steps )
209
- for ( let i = 0 ; i < this . num_newton_steps ; i ++ )
210
- {
211
- x_newton_trig . push ( current_x )
212
- y_newton_trig . push ( this . trigonometric_function ( current_x ) )
213
- current_x = this . newton_step ( this . trigonometric_function , this . grad_trigonometric_function , current_x )
214
- x_newton_trig . push ( current_x )
215
- y_newton_trig . push ( 0 )
216
- }
217
-
218
- var trace_trigonometric = {
219
- x : xValues ,
220
- y : yValues_trigonometric ,
221
- name : "trig. fct."
222
- } ;
223
-
224
- var trace_trigonometric_newton = {
225
- x : x_newton_trig ,
226
- y : y_newton_trig ,
227
- name : "Newton - trig. fct."
228
- } ;
229
- data = [ trace_trigonometric , trace_trigonometric_newton ] ;
230
- }
231
-
210
+ this . computeData ( this . trigonometric_function , this . grad_trigonometric_function , 0.465 , data )
211
+ }
232
212
233
213
var layout = {
234
214
title : 'Functions' ,
0 commit comments