Python: How to get my variables out of a function

Click For Summary
SUMMARY

The discussion focuses on creating a Python function to compute the mathematical functions g(t) = e^(-a * t^2) and gderiv(t) = -2*a*t*g(t). The initial attempt to print the results in the format "g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531" failed due to undefined variables t and a. A solution was proposed using the input function to allow users to define t and a interactively, ensuring the correct output format is achieved.

PREREQUISITES
  • Understanding of Python functions and return values
  • Familiarity with mathematical expressions and their implementation in Python
  • Knowledge of the Python input function for user interaction
  • Basic understanding of the math module in Python, specifically the use of e
NEXT STEPS
  • Learn about Python function definitions and return statements
  • Explore the use of the input function in Python for user-defined variables
  • Study the math module in Python, focusing on exponential functions
  • Investigate formatting strings in Python for output display
USEFUL FOR

Beginner Python programmers, mathematics enthusiasts, and anyone looking to understand function definitions and user input in Python.

MaxManus
Messages
268
Reaction score
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
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.
 
Thank you
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 50 ·
2
Replies
50
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K