abjer / sds2019

Social Data Science 2019 - a summer school course
https://abjer.github.io/sds2019
46 stars 96 forks source link

Problem 0.3.3 (How does sigma sum notation translate into code?) #10

Open Toromash opened 4 years ago

Toromash commented 4 years ago

I have an issue understanding the description of problem 0.3.3. I have defined the function and tried to loop it, but there is clearly something wrong with my coding. There is something wrong with the structure, and should i return value twice?

def natural_logarithm(x, k_max): total = 0 for k in range (0, k_max): total += k total = total 2 return (1/2k_max+1)*((x-1)/((x+1))*2k_max+1) answer_033 = natural_logarithm(2.71, 1000)

Kristianuruplarsen commented 4 years ago

For a start what you have pasted is not valid python code. Read this if you don't know how to type formatted code on github.

Other than that, do you get the sum-notation (otherwise read here)? It implies to add up the terms under the sigma - which is not what you are doing right now.

Toromash commented 4 years ago

yeah, i just copy pasted the code directly in. Sorry about that.

If i use the sum-notation, it will say that ´ínt´ object is not iterable. I don´t have a list of numbers, so i don´t see how it can work?

Kristianuruplarsen commented 4 years ago

Not the python sum function, the mathematical one, sorry if that was unclear. You are right that sum expects an iterable object, so to use it you would first need a list of numbers. Creating the right list of numbers and using sum is a completely valid solution.

About

I have an issue understanding the description of problem 0.3.3.

The code you pasted doesn't correspond to what the mathematical expression is telling you to calculate. In other words your translation from math to code is wrong - this is why i gave you a link that explains the sigma sum notation. Consider beginning by coding up a far simpler sum (e.g. the sum of integers below some k_max) to get a better intuition for the relation between math and code.