# Assignment problem 2 part 2 question 5

I am not clear about the solution given for Q5 or maybe I have not got the question right.

Q5 Write a function to iteratively sum up the value of x^n/n factorial from n=1 to a chosen value of N’ such that F(x,N’)- F(x,N’-1)<e for a given real number x and positive small number e.

What I understand is that you are given x and e (say x=2 and e = 0.01) then find a N’ such that F(x,N’) - F(x,N’-1) is < 0.01 and then find the sum of x^N’/N’factorial. Where F(x,N’) is the series sum 1+ sigma of x^i/i factorial with i ranging from 1 to N’.

as per me the correct code should be

def sum_socratic_epsilon(e,x):
i =1
sum=0
while (sum_socratic(x,i)-sum_socratic(x,i-1))<e:
sum+=socratic(x,i)
i+=1
return(sum)

where sum_socratic is the function

def sum_socratic(N,x):
sum1=1
for i in range(1,N+1):
sum1+=socratic(x,i)
return sum1

and socratic is defined as
def socratic(x,n):
return (x**n)/factorial_recur(n)

However the solution given does something else.

Could you please point out where I am going wrong. Why are we not calculating the difference between F(x,N) and F(x,N-1) and then seeing if it is less than epsilon. Also the while loop condition is put as var>=epsilon whereas in the question it is <epsilon. So if there a mathematical shortcut that has been used to arrive at the same result and the code has been written for that shortcut?

BTW the function given in the solution can be further shortened as

def sum_epsilon(e,x):
i =1
sum=1
while socratic(x,i)>=e:
sum+=socratic(x,i)
i+=1
return(sum)

Even I think the question was to calculate the sum till the time result of N and N-1 is less than epsilon
What is the answer from your code?? Is it coming to be same as that of instructor’s solution?

No the answer is different. Did you also get no reply from the instructors?

The reason the solutions appears different is because of this.

Hope this clarifies?

1 Like