Python: How to get my variables out of a function

In summary, the function g(t, a) computes the derivative of the function g(t) with respect to the variable a. The function gderiv(t) is the derivative of g(t) with respect to the variable a, and returns the value -2*a*t*G. The program g(1, a=0.5) returns the value 0.606531, and g’(1, a=0.5) returns the value -0.606531.
  • #1
MaxManus
277
1

Homework Statement



The exercise: Make a Python function for computing
g(t) = e**(-a * t**2) and gderiv(t) = -2*a*t*g(t)

Return the function values of g(t) and gderiv(t). Apply the function to write out a result in the format:
g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531 I have made a function g(t, a) and gderiv(t,a), but I don't know how to print t and a so that i can print the result in the format: "g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531"

The Attempt at a Solution



from math import e

def g(t, a):
G = e**(-a*t**2)
Gderiv = -2*a*t*G
return G, Gderiv,

G,Gderiv = g(t = 1, a = 0.5)

print "g(%g, a=%g) = %.6f" % (t, a, G)

Edit: The problem is that t and a are not defined.
 
Technology news on Phys.org
  • #2
MaxManus said:

Homework Statement



The exercise: Make a Python function for computing
g(t) = e**(-a * t**2) and gderiv(t) = -2*a*t*g(t)

Return the function values of g(t) and gderiv(t). Apply the function to write out a result in the format:
g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531


I have made a function g(t, a) and gderiv(t,a), but I don't know how to print t and a so that i can print the result in the format: "g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531"





The Attempt at a Solution



from math import e

def g(t, a):
G = e**(-a*t**2)
Gderiv = -2*a*t*G
return G, Gderiv,

G,Gderiv = g(t = 1, a = 0.5)

print "g(%g, a=%g) = %.6f" % (t, a, G)

Edit: The problem is that t and a are not defined.

I've never done any programming in python, but I assume that there is an input function which allows the user of your program to input the values of t and a... I'd try something like this:
Code:
from math import e

def g(t, a):
    G = e**(-a*t**2)
    Gderiv = -2*a*t*G
    return G, Gderiv

t=input("Please input value of t   ")  
a=input("Please input value of a   ")

G,Gderiv = g(t , a ) 

print "g(%g, a=%g) =  %.6f" % (t, a, G)
print "g'(%g, a=%g) =  %.6f" % (t, a, Gderiv)

And then when you run the program you input the values of t and a when prompted.
 
  • #3
Thank you
 

Related to Python: How to get my variables out of a function

1. What is the purpose of getting variables out of a function in Python?

The purpose of getting variables out of a function in Python is to be able to access and use those variables outside of the function. This allows for more flexibility and reusability of code.

2. How can I get my variables out of a function in Python?

To get variables out of a function in Python, you can use the return statement. This allows you to specify which variables you want to return from the function and use them outside of the function.

3. Can I use global variables to get my variables out of a function in Python?

Yes, you can use global variables to get your variables out of a function in Python. However, it is generally not recommended as it can lead to confusion and potential bugs in your code.

4. Is it possible to get multiple variables out of a function in Python?

Yes, you can get multiple variables out of a function in Python by using the return statement and separating the variables with a comma. For example, "return x, y" will return both the variables x and y from the function.

5. Do I always need to get my variables out of a function in Python?

No, you do not always need to get your variables out of a function in Python. If the variables are only needed within the function and will not be used outside of it, then there is no need to return them.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
790
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top