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

AI Thread Summary
The discussion centers on creating a Python function to compute g(t) = e^(-a * t^2) and its derivative gderiv(t) = -2*a*t*g(t). The user has successfully defined the functions but struggles with printing the results in the required format, specifically how to include the variables t and a in the output. The solution involves using the input function to allow users to enter values for t and a, which can then be passed to the function. The corrected code includes prompts for user input and ensures the output displays both the function values and their corresponding parameters correctly formatted.
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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
15
Views
2K
Replies
16
Views
2K
Replies
4
Views
2K
Replies
4
Views
5K
Replies
50
Views
6K
Replies
6
Views
3K
Back
Top