Find an approximation of cosx in Swift

Why Swift?

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

McLaughlin expansion of cosx

When $ cosx $ is expanded by McLaughlin,

cosx=1-\frac{x^2}{2!}+\frac{x^4}{4!}-⋯+\frac{(-1)^nx^{2n}}{(2n)!}+⋯

It was expressed as. In this article, we will use programming to find an approximation of $ sinx $. (Language is Swift) ipad is fine.

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:

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

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 idea, up to the second term as $ x = \ frac {\ pi} {2} $, that is,

cosx=1-\frac{x^2}{2!}

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.

3. Make it easy to generalize

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

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/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,

cosx=1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\frac{x^8}{8!}-\frac{x^{10}}{10!}

Then, it can be seen that there is almost no error.

bonus

When generalizing, it is easier to grasp if it is tabulated.

Recommended Posts

Find an approximation of cosx in Swift
Find an approximation of sinx using Swift
Find the approximate value of log (1 + x) in Swift
Make an FPS counter in Swift
[GCD] Basics of parallel programming in Swift
[Swift] How to get the number of elements in an array (super basic)
Stuck in an enum in front of a blacksmith
SKStoreReviewController implementation memo in Swift UI of iOS14
An example of Moya + RxSwift API client (Swift5)
Order of modifiers used in Swift (see SwiftLint)
The story of an Illegal State Exception in Jetty.
I wrote an overview of Chef in an easy-to-understand manner
Division becomes 0 in Swift
Output in multiples of 3
Multidimensional array in Swift
[Swift 5] Recognize ON/OFF of Switch in custom cell for each cell
List of devices that can be previewed in Swift UI
How to change BackgroundColor etc. of NavigationBar in Swift UI
Find the number of days in a month with Kotlin
Generate Stream from an array of primitive types in Java