Find the approximate value of log (1 + x) 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 log (x + 1)

The McLaughlin expansion of $ \ log (1 + x) $

\log(1+x) = x-\frac{x^2}{2}+\frac{x^3}{3}-\frac{x^4}{4}+⋯+\frac{(-1)^{n-1}x^n}{n}+⋯

It was expressed as. In this article, we will use programming to find the approximate value of $ \ log (1 + x) $. (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 $ \ log (x + 1) $ 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 $.

var sum = 0
let x = 1

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:

var sum = 0.0  //Since int type and double type cannot be added,0.Write 0 to make it a double type.
let x = 1.0
var item = x

2. Code up to the second term with x = 1

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, $ x = 1 $ up to the second term, that is,

\log (1+x)=x-\frac{x^2}{2}

Code up to.

var sum = 0.0  //Since int type and double type cannot be added,0.Write 0 to make it a double type.
let x = 1.0
var item = x

sum += 1.0
sum += -(x*x)/Double(2*1)

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.
let x = 1.0
var item = x

sum += 1.0
sum += -(x*x)/Double(2*1)
print(sum)//Approximate value 0.5
print(log(2.0))//True value 0.6931471805599453

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 $ and mess with the denominator as 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 item = 1.0
let x = 1.0
sum += item

for i in 2 ..< 3{
    item *= -(x*Double(i-1))/Double(i)
    sum += item
}
print(sum)//Approximate value 0.5
print(log(2.0))//True value 0.6931471805599453

Observing the McLaughlin-expanded $ \ log (x + 1) $, $ x $ and $ 1 / n $ are multiplied each time the term grows, so express it in code.

計算-12.jpg

The for statement is used, but the sum value does not change because it is executed only once.

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.
var item = 1.0
let x = 1.0
sum += item

for i in 2 ..< 10{
    item *= -(x*Double(i-1))/Double(i)
    sum += item
}
print(sum)//Approximate value 0.7456349206349205
print(log(2.0))//True value 0.6931471805599453

Even if the item 9 is added, there is a considerable difference from the true value. Since $ sinx and cosx $ were almost the same in the sixth term, it can be seen that $ \ log (x + 1) $ converges more slowly than $ sin $ and $ cos $.

Recommended Posts

Find the approximate value of log (1 + x) in Swift
Find an approximation of cosx in Swift
Format of the log output by Tomcat itself in Tomcat 8
Determine that the value is a multiple of 〇 in Ruby
[Swift] Get the timing when the value of textField is changed
Find the number of days in a month with Kotlin
802.1X authentication to the network of Bonding setting in CentOS7
[Swift] Change the textColor of UIDatePicker
Find out the list of fonts available in AWS Lambda + Java
The basic basis of Swift dialogs
Defeat the hassle of treating C arrays as Tuples in Swift
{The first consecutive 10-digit prime number in the value of e} .com
Form that receives the value of the repeating item in Spring MVC
Order of processing in the program
I want to change the value of Attribute in Selenium of Ruby
About the log level of java.util.logging.Logger
[Swift] Determine the constellation from the date of birth entered in the UIDatePicker
How to increment the value of Map in one line in Java
Procedure to make the value of the property file visible in Spring Boot
How to find the total number of pages when paging in Java
Sample program that returns the hash value of a file in Java
Find the maximum and minimum of the five numbers you entered in Java
Android development, how to check null in the value of JSON object
Get the value of enum saved in DB by Rails with attribute_before_type_cast
Get the result of POST in Java
Find an approximation of sinx using Swift
Shorten the UUID to base64 in Swift.
[Swift] Vaguely grasp the flow of Delegate
[GCD] Basics of parallel programming in Swift
The identity of params [: id] in rails
Samshin on the value of the hidden field
[Swift] Termination of the program by assertion
The story of AppClip support in Anyca
[Swift] Get the height of Safe Area
[Swift] Change the color of SCN Node
The story of writing Java in Emacs
Find the difference from a multiple of 10
Write the movement of Rakefile in the runbook
The basic basis of Swift custom cells
[Swift] How to get the number of elements in an array (super basic)
How to get the ID of a user authenticated with Firebase in Swift
Could not find coderay-1.1.3 in any of the sources (Bundler :: GemNotFound) during co-development
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
[Order method] Set the order of data in Rails
How to find the cause of the Ruby error
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
Store the AWS SDK return value in DynamoDB
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
SKStoreReviewController implementation memo in Swift UI of iOS14
How to add sound in the app (swift)
Order of modifiers used in Swift (see SwiftLint)
Specify the default value with @Builder of Lombok
Import files of the same hierarchy in Java
[Swift] Termination of the program by the fatalError function
I want to get the value in Ruby
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
Get the value from the array and find out what number it is included in