0

I am trying to minimize a function using scipy.optimize.minimize(). Below is the piece of code which I am trying to execute.

When I execute the same I am getting

NameError: name 'j' is not defined

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from sklearn.metrics import r2_score

url = 'test_data.txt'
z = pd.read_csv(url)
#
e1 = z['strain'].values
sigx = z['stress'].values
e=np.array(e1)
sig1=np.array(sigx)
#print (sig1)

def sig2(e):
    j=[1000,0.2]
    return np.mean((sig1-(j[0]*np.power(e,j[1])))*(sig1-(j[0]*np.power(e,j[1]))))
print (sig2(e))

res= minimize(sig2,j)
print(res)

What I expect as the outcome was values of j[0] & j[1] to get the function value (sig2) to be near to zero

SuperKogito
  • 2,998
  • 3
  • 16
  • 37
Jithesh Erancheri
  • 281
  • 1
  • 5
  • 14
  • Initialize 'j' outside the loop. By defining inside, it's scope is limited to that function only. – vb_rises May 02 '19 at 12:18
  • What is/are your optimization variable(s)? is it `j` or `e`; because your objective function take `e` as an argument but you say your outcome is `j` – SuperKogito May 03 '19 at 11:15

1 Answers1

0

This is because you have initialized j variable inside a function. The scope of a variable defined inside a function will be limited to the function itself.

allexiusw
  • 1,533
  • 2
  • 16
  • 23
Navya Jain
  • 31
  • 2