Find an approximation of sinx using Swift

Why Swift?

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. 2020-10-14 11.16のイメージ.jpg

McLaughlin expansion of sinx

When $ sinx $ is expanded by McLaughlin, $ sinx = x - \frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+⋯+\frac{(-1)^{n-1}x^{2n-1}}{(2n-1)!}+⋯ $

It was expressed as. In this article, we will use programming to find an approximation of $ sinx $.

1. Prepare variables

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:

2. Code up to the second term with x = π / 2

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, $ sinx=x-\frac{x^3}{3!} $ 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.
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.

3. Make it easy to generalize

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.

4. Increase the number of terms

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, $ sinx= x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\frac{x^9}{9!}-\frac{x^{11}}{11!} $ Then, it can be seen that there is almost no error.

Recommended Posts

Find an approximation of sinx using Swift
Find an approximation of cosx in Swift
An example of Moya + RxSwift API client (Swift5)
Find the approximate value of log (1 + x) in Swift
Example of using vue.config.js
Summary of using FragmentArgs
Summary of using DBFlow