0

I am trying to define a one variable g function from a multivariable function G:

def dG(thetaf,psi,gamma) : 
    return 0.35*(cos(psi))**2*(2*sin(3*thetaf/2+2*gamma)+(1+4*sin(gamma)**2)*sin(thetaf/2)-sin(3*thetaf/2))+sin(psi)**2*sin(thetaf/2)

 g = lambda thetaf: dG(thetaf,psi,gamma)

unfortunately this is not working and the error i receive is that :

only length-1 arrays can be converted to Python scalars

2

2 Answers 2

1

You have to define some default values. If you do this by using keyword arguments, you don't even need to define a separate function.

from numpy import sin, cos, arange

def dG(thetaf,psi=0.5,gamma=1) : 
    return 0.35*(cos(psi))**2*(2*sin(3*thetaf/2+2*gamma)+(1+4*sin(gamma)**2)*sin(thetaf/2)-sin(3*thetaf/2))+sin(psi)**2*sin(thetaf/2)

thetaf = arange(10)
print dG(thetaf)
>>> [ 0.4902  0.1475  0.5077  1.6392  1.757   0.4624 -0.472  -0.2416 -0.2771 -1.3398]

You actually can define a separate function, but using keyword defaults is the cleaner alternative.

g = lambda tf: dG(tf, 0.5, 1)
g(thetaf)
array([ 0.4902,  0.1475,  0.5077,  1.6392,  1.757 ,  0.4624, -0.472 ,
       -0.2416, -0.2771, -1.3398])
Sign up to request clarification or add additional context in comments.

2 Comments

This is not what i want to do, i am defining my function dG, with 3 variables thetaf,psi,gamma, and i would like to find the root of this function, in case : gamma = linspace(0, pi/2, nt) psi = linspace(0, pi/2, np)
Here is exactly what i wrote in my script : 'def dG(thetaf,psi,gamma) : return 0.35*(cos(psi))**2*(2*sin(3*thetaf/2+2*gamma)+(1+4*sin(gamma)**2)*sin(thetaf/2)-sin(3*thetaf/2))+sin(psi)**2*sin(thetaf/2) nt = 100 np = 100 gamma = linspace(0, pi/2, nt) psi = linspace(0, pi/2, np) x = zeros((nt, np)) for i, theta in enumerate(gamma): for j, phi in enumerate(psi): print('i = %d, j = %d') %(i, j) g = lambda thetaf: dG(thetaf,psi,gamma) x[i,j] = optimize.brenth(g,-pi/2,pi/2)
0

Next time, please include the script in your original question in a nice format. It makes helping go faster.

I think it is just a simple mistake. You get theta and phi out of gamma and psi respectively, but then you never use them. Did you mean to use those as your parameters in g? If so, then it should look something like this

from numpy import sin, cos, arange, linspace, pi, zeros
import scipy.optimize as opt    

def dG(thetaf, psi, gamma):
    return 0.35*(cos(psi))**2*(2*sin(3*thetaf/2+2*gamma)+(1+4*sin(gamma)**2)*sin(thetaf/2)-sin(3*thetaf/2))+sin(psi)**2*sin(thetaf/2)    

nt = 100 
np = 100 
gamma = linspace(0, pi/2, nt) 
psi = linspace(0, pi/2, np) 
x = zeros((nt, np)) 
for i, theta in enumerate(gamma):
    for j, phi in enumerate(psi):
        print('i = %d, j = %d') %(i, j) 
        g = lambda thetaf: dG(thetaf,phi,theta) 
        x[i,j] = opt.brenth(g,-pi/2,pi/2)

2 Comments

Yeah it is my fault, Normally it's gammas = linespace and also psi linespace gamma in enemurate(gammas), psi in enumerate(psis) how can i plot the solution x in a 3d plan(gammas,psis) using mplot3d @cc7768
Well the above is the answer to your original question. Please upvote and accept. Start a new question and show what you have tried and I can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.