User-Defined Function in Python

In summary: Then it will iterate through the list and calculate the corresponding value of M for each element in the list. In summary, the code within the user-defined function produced incorrect values when I attempted to change it into a user-defined function. The code works correctly when I remove it.
  • #1
K_Physics
9
0

Homework Statement


So currently I'm working with user-defined functions. Initially, the code inside the user-defined function worked but when I attempted to change it into a user-defined function, it produces incorrect values.

Homework Equations


No need for any equations

The Attempt at a Solution


If I were to remove the code within the user-defined function, the code gives a value of M around 1.7361319160639026, which is correct. But for some reason when I execute this code (as a user-defined function), I get values of M which are far too low. I'm slightly confused toward why this happens.
Python:
from math import sqrt

from matplotlib import pyplot as plt
from pylab import plot, show

def function(L):
   
    M = 0.0

    for i in range(-L,L+1):

        for j in range(-L,L+1):        

            for k in range(-L,L+1):

                if not (i==j==k==0):

                    M = ((-1)**(i+j+k+1))/sqrt(i*i +j*j +k*k)

                return M

list = []
L = 0

while int(L) < 50:
    list.append(L)
    L = int(L) + 1
    print(function(L))
 
Technology news on Phys.org
  • #2
What are you trying to do? Why bother looping on i, j, and k when you are only calculating a single value and then returning? As I read this code, it will just return the value of:
[tex] M = \frac{-1^{-3L+1}}{\sqrt{3 L^2}}[/tex]
 
  • #3
phyzguy said:
What are you trying to do? Why bother looping on i, j, and k when you are only calculating a single value and then returning? As I read this code, it will just return the value of:
[tex] M = \frac{-1^{-3L+1}}{\sqrt{3 L^2}}[/tex]
Initially I was calculating only one value. In this new code, I have to graph values of L from 1-50 with the corresponding M value. The issue is, none of the M values were correct after changing it into a user-defined function.
 
  • #4
Like @phyzguy said, it seems weird to me that you are looping through i, j, and k, where each loops all the way to L, and calculating a brand new value of M each iteration, independent of previous iterations.

Also, there seems that there is an issue with the indentation of your "return M" statement. As it is now, the function will return immediately when i, j, k, [are all -L] and M are all [is] zero. (Python code is structured through indentation, and it looks to me that is an issue here.)

Maybe it would help if we knew what exactly it is that the code is supposed to do.
 
  • #5
collinsmark said:
Like @phyzguy said, it seems weird to me that you are looping through i, j, and k, where each loops all the way to L, and calculating a brand new value of M each iteration, independent of previous iterations.

Also, there seems that there is an issue with the indentation of your "return M" statement. As it is now, the function will return immediately when i, j, k, and M are all zero. (Python code is structured through indentation, and it looks to me that is an issue here.)

Maybe it would help if we knew what exactly it is that the code is supposed to do.
The end goal of the code is to graph values of L and the corresponding value of M with the use of user-defined functions. Sorry if this was unclear. I didn't continue to plot, as the values of M produced were incorrect.
 
  • #6
K_Physics said:
The end goal of the code is to graph values of L and the corresponding value of M with the use of user-defined functions. Sorry if this was unclear. I didn't continue to plot, as the values of M produced were incorrect.
I still don't understand. In the following code
Code:
def function F
     for i in range (-10, 10):
          return i

the function will return a single value of -10. Why not just set i=-10 and forget about the loop? Why bother with the loop if the function only returns a single value independent of loop iterations?
 
  • #7
collinsmark said:
I still don't understand. In the following code
Code:
def function F
     for i in range (-10, 10):
          return i

the function will return a single value of -10. Why not just set i=-10 and forget about the loop? Why bother with the loop if the function only returns a single value independent of loop iterations?
The idea behind it was, the function will take the argument L multiple times

Code:
list = []
L = 0

while int(L) < 50:
    list.append(L)
    L = int(L) + 1
    print(function(L))

This way we can get the M values with the corresponding L value. I want to point out that I am a beginner. So I may not be correct in my approach.
 
  • #8
I think you're still not getting that the code is probably not doing what you think it does. Note that function1 and function2 below do the same thing, but function2 is a lot simpler. Can you post the code without the user defined function that worked?

Python:
def function1(L):
     for i in range(-L,L):
          return i

def function2(L):
     return -L
 
  • Like
Likes K_Physics
  • #9
phyzguy said:
I think you're still not getting that the code is probably not doing what you think it does. Note that function1 and function2 below do the same thing, but function2 is a lot simpler. Can you post the code without the user defined function that worked?

Python:
def function1(L):
     for i in range(-L,L):
          return i

def function2(L):
     return -L

This code was just designed to output the value of M, which happened to be around 1.7361319160639026
Python:
from math import sqrt

L = 50
     
M = 0.0

for i in range(-L,L+1):

    for j in range(-L,L+1):        

       for k in range(-L,L+1):

           if not (i==j==k==0):

              M += ((-1)**(i+j+k+1))/sqrt(i*i +j*j +k*k)

print("The Madelung constant is: ", M)
 
  • #10
OK, now we're getting somewhere. You're original code had M +=, which means the value of ((-1)**(i+j+k+1))/sqrt(i*i +j*j +k*k) was added to M each time through the loop. So the final value of M was the sum of a bunch of terms. When you converted it to a function, you made two mistakes:

(1) You just put M = instead of M+=, so M just has a single terms instead of a sum of terms. Note that
M += something is the same as M = M + something.
(2) You put the return inside the loop instead of at the completion of the loop.

If you modify your function so that it looks like the function below, I think it will work. Do you see the differences?

Python:
def function(L):  
    M = 0.0
    for i in range(-L,L+1):
        for j in range(-L,L+1):       
            for k in range(-L,L+1):
                if not (i==j==k==0):
                    M += ((-1)**(i+j+k+1))/sqrt(i*i +j*j +k*k)
    return M
 
  • Like
Likes K_Physics
  • #11
Python:
from math import sqrt

def function(L):
    
     M = 0.0

     for i in range(-L,L+1):

         for j in range(-L,L+1):       

             for k in range(-L,L+1):

                 if not (i==j==k==0):

                     M += ((-1)**(i+j+k+1))/sqrt(i*i +j*j +k*k)

     return M

b = function(50)
print (b)
Your original code was missing the += operator in the line that assigns a value to M. Also, the return statement needs to be aligned to the same indentation as the outermost for loop (the loop on i). In your code this wasn't the case. Python doesn't use braces as C and C++ and related languages do, so it's crucial that you get the indentation right.

With 50 as the input to the function, I get a value of 1.7361319160639026.
 
  • Like
Likes K_Physics
  • #12
I got it to work! Thanks for the help!
 

FAQ: User-Defined Function in Python

What is a user-defined function in Python?

A user-defined function in Python is a block of code that performs a specific task and can be called multiple times throughout a program. It allows for better organization and reusability of code.

How do you create a user-defined function in Python?

To create a user-defined function in Python, you use the keyword "def" followed by the function name and a set of parentheses. Inside the parentheses, you can include any parameters that the function will take. Then, you use a colon to begin the code block and indent any code that you want to be a part of the function.

How do you call a user-defined function in Python?

To call a user-defined function in Python, you simply use the function name followed by a set of parentheses. If the function takes in any parameters, you would include the necessary values inside the parentheses.

Can a user-defined function return a value?

Yes, a user-defined function in Python can return a value. To do so, you use the keyword "return" followed by the value you want to return. If the function does not have a return statement, it will automatically return "None".

What are the benefits of using user-defined functions in Python?

Using user-defined functions in Python has several benefits, including improved code organization, increased reusability, and easier debugging. It also allows for the creation of more complex programs by breaking them down into smaller, more manageable parts.

Similar threads

Replies
5
Views
2K
Replies
18
Views
1K
Replies
1
Views
1K
Replies
8
Views
1K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
15
Views
2K
Back
Top