Swift does not need to build an environment, if you have a Mac or iPad, just install Playgrounds and use it be able to. In addition, Playgrounds has functions such as ** step execution ** and ** slow execution **, and you can execute for statements and while minutes step by step and check the change in the value when it is executed. .. Furthermore, if you use the ** viewer function **, the state of variable changes will be displayed as a graph without permission.
When $ cosx $ is expanded by McLaughlin,
It was expressed as. In this article, we will use programming to find an approximation of $ sinx $. (Language is Swift) ipad is fine.
The McLaughlin expansion approximates the function $ f (x) $, which can be differentiated many times, by the sum of the expansion formulas of the series of the power function of $ x $. In this article, the value of $ sinx $ is set as sum
, and the value is gradually added to sum
.
var sum = 0
print(sum) //0
sum += 1
print(sum) //1
You also have to prepare a constant to put the approximate $ x $. At this time, we want to use values such as $ \ pi / 6 $ and $ \ pi / 3 $ as approximate values, so write ʻimport Foundation` so that $ \ pi $ can be used.
import Foundation
var sum = 0
let x = Double.pi/2
In addition, prepare variables that are continuously updated as terms.
var item = x
The first term is the same as the constant, so enter x
.
The final code looks like this:
import Foundation
var sum = 0.0 //Since int type and double type cannot be added,0.Write 0 to make it a double type.
let x = Double.pi/2
var item = x
I think it is important to start with a small value and try and error with a concrete value when coding. (Personal view). In this article as well, according to that idea, up to the second term as $ x = \ frac {\ pi} {2} $, that is,
Code up to.
import Foundation
var sum = 0.0 //Since int type and double type cannot be added,0.Write 0 to make it a double type.
let x = Double.pi/2
var item = 1
sum += 1
sum += -(x*x)/Double(2*1)
$ 1 $ is added to sum
, and $-\ frac {x ^ 2} {2!} $ Is added on the next line.
Finally, add a print
statement to compare with the true value.
import Foundation
var sum = 0.0 //Since int type and double type cannot be added,0.Write 0 to make it a double type.
var x = Double.pi/2
sum += 1
sum += -(x*x*x)/Double(3*2*1)
print(sum)//Approximate value 0.9248322292886504
print(sin(Double.pi/2))//True value 1.0
The phrase Double (2 * 1)
is used to convert the Int type to the Doule type, but this article is not an essential part, so the explanation is omitted.
Earlier we set the second term to $-\ frac {x ^ 2} {2!} $, But with this we have to increase $ x $ each time the number of terms increases. Add some ideas so that you can code short even if the number of terms increases.
import Foundation
var sum = 0.0 //Since int type and double type cannot be added,0.Write 0 to make it a double type.
let x = Double.pi/3
var item = 1
sum += item
for i in 1 ..< 3{
item *= -(x*x)/Double((2*i)*(2*i-1))
sum += item
}
print(sum)//Approximate value 0.525479753410744
print(cos(Double.pi/3))//True value 0.5
It can be dealt with by increasing the number of terms by generalizing.
import Foundation
var sum = 0.0 //Since int type and double type cannot be added,0.Write 0 to make it a double type.
let x = Double.pi/3
var item = 1.0
sum += item
for i in 1 ..< 6{
item *= -(x*x)/Double((2*i)*(2*i-1))
sum += item
}
print(sum)//Approximate value 0.499999999639
print(cos(Double.pi/3))//True value 0.5
Approximation up to the sixth term, that is,
Then, it can be seen that there is almost no error.
When generalizing, it is easier to grasp if it is tabulated.
Recommended Posts