You don't need to build an environment to use Swift, so you can get started right away. If you have a Mac or iPad, you can use it by simply installing Playgrounds. 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 $ sinx $ is expanded by McLaughlin,
It was expressed as. In this article, we will use programming to find an approximation of $ sinx $.
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:
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 philosophy, $ x = \ pi / 2 $ up to the second term, that is,
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 += x
sum += -(x*x*x)/Double(3*2*1)
$ X $ is added to sum
, and $-\ frac {x ^ 3} {3!} $ 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 += x
sum += -(x*x*x)/Double(3*2*1)
print(sum)//Approximate value 0.9248322292886504
print(sin(Double.pi/2))//True value 1.0
Double (3 * 2 * 1)
is the conversion of Int type to Doule type, but it is not an essential part in this article, so the explanation is omitted.
Earlier we set the second term to -(x * x * x) / 3 * 2 * 1
, 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.
var x = Double.pi/2
sum += x
for i in 2 ..< 3{
x *= -(x*x)/Double((2*(i-1))*(2*i-1))
sum += x
print(x)
}
print(sum)//Approximate value 0.9248322292886504
print(sin(Double.pi/2))//True value 1.0
We used for
to associate terms with the variable ʻi. (Although it is executed only once.) In this case, only the second term is executed, so
for i in 2 .. <3` is set. Although it is generalized, the calculation is the same, so it should be the same as the answer given in 2.
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/2
var item = x
sum += item
for i in 2 ..< 6{
item *= -(x*x)/Double((2*(i-1))*(2*i-1))
sum += item
}
print(sum)//Approximate value 1.00000035425842861
print(sin(Double.pi/2))//True value 1.0
Approximation up to the sixth term, that is,