3

I just started using Python and can't really get the hang of it..

I wrote the code in Matlab but is hard to covert it the right way in Python.

Image

Could you please help?

 x=0; for i=1:1000 x=x+(1/((((2*i)-1)^2)*(((2*i)+1^2)))); z=sqrt((x*16)+8); error=abs(z-pi); if (error < 10^-8) i break end end 

Thank you

1
  • 1
    You forgot a parenthesis ! ((2*i+1)^2) Commented Apr 12, 2018 at 9:14

2 Answers 2

2

The following segment of code is equivalent to what you have written in Matlab.

from math import pi, sqrt x = 0 error_tolerance = 1e-8 for i in range(1, 1001): x += 1 / (((2 * i - 1) ** 2) * ((2 * i + 1) ** 2)) z = sqrt((x * 16) + 8) error = abs(z - pi) if error < error_tolerance: print(i) break 

The key differences in between Python and Matlab that can be seen in this code are:

  • Indentation: For loops, while loops, if statements, function definitions, etc. are isolated using correct indentation instead of a starting keyword and end. You can see that the for loop statement ends with a colon, and everything inside the for loop has been indented by a tab OR 4 spaces. The break keyword is further indented because it only executes when the error is less than the specified tolerance.

  • Operators: You can see that the raised power symbol ^ has been replaced with **. This is because ^ represents a bitwise XOR operation. You may also notice that x += ... has been used instead of x = x + .... These two statements are equivalent, the first way is just more concise.

  • Semicolons: Python does not require the use of semicolons to mute a variable/constant. Instead, to find out what the value of the variable is, simply use the print(...) statement.

  • For loops: Instead of just iterating over a linear sequence like Matlab does, in Python each for loop will iterate over the next item in a specified iterable sequence. In this case, we have used the built-in range function to generate a list of integers from 1 to 1000, and in each loop i will be set to the next value in this linear sequence.

  • Non built-in functions: Python's base set of built-ins does not contain a sqrt function or pi constant definition. Instead these have been separated into a separate module named math alongside many other mathematical functions such as sin, cosine, etc..

  • Brackets around if-conditions: You can use brackets around if statement conditions. However, for simple conditions such as this one, they are not necessary.

There are many more differences between the two languages, I have just highlighted the most noticeable differences between the Matlab code you have provided and its Python equivalent. To find out more about Python I suggest looking at online tutorials, and you can find plenty of answers to commonly asked questions through a Google search or on this site.

Edit: I noticed a slight error in your implementation of the mathematical sequence, and have updated it to match the formula provided in your link. I also removed unnecessary brackets

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for the clarifications and such a great detailed answer. I will watch some tutorials for a better understanding of how python works.
Glad I could help. Good luck!
1
import math 

...

x = 0 for i in range(1,1001): x = x + (1 / (((2 * i - 1) ** 2) * ((2 * i + 1) ** 2))) z = math.sqrt((x * 16) + 8) error = abs(z - math.pi) if error < 10 ** -8: print(i) break 

4 Comments

Thank you for you answer, but when I calculate it in mathlab and also by hand, I get the result as being i= 174, and in this code it results that i=999. Could you please check the image maybe I miscalculated something? thank you for your help
My bad it was range(1,1001) and not range(1,1000). But when I execute your code in mathlab, at the end of the loop I got i = 1000
I've edited my code try it, a parenthesis was missing
Thank you for your effort, I got the right answer now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.