2 views
in Python

I am recieving the error 'only size-1 arrays can be converted to Python scalars', I know that this error means that, somewhere I'm giving an array instead of a single number. I would not know where this happens, as I'm literally just fitting. This is my code:

import matplotlib.pyplot as plt import numpy as np import scipy.optimize from math import * import matplotlib.patches as mpatches

X = [0,-0.0000959936,-0.000194899,-0.000273446,-0.000430558,-0.000366546,-0.000573153,-0.000657563,-0.000768193,0.000066904,0.000148355,0.000232717,

0.000285083,0.000427648,0.000401461,0.000526588,0.0000465422,-0.0000250891,0.0000436333,-0.0000261799,-0.0000610866,0.0000930847,-0.000101811]

Y = [0.96,0.47,0.11,0.25,0.15,0.12,0.13,0.11,0.11,0.78,0.16,0.19,0.13,0.14,0.16,0.13,0.6,2.02,1.91,1.4,0.94,0.68,0.97]

onzekerheidY = [0.0001,0.000001,-0.000101,-0.000182,-0.000344,-0.000278,-0.000491,-0.000578,-0.000692,0.000169,0.000253,0.00034,

0.000394,0.000541,0.000514,0.000643,0.000148,0.000074125,0.000145,0.000073,0.000037,0.000196,-0.000005]

def f(x, I0, theta, a, d):

k = 9666438.934

N = 1

return I0 * (  ( (np.sin(k*(d/2)*np.sin(x)))/(k*(d/2)*np.sin(x)) * (np.sin(k*a*np.sin(x) *(N/2)))/(N*np.sin((k/2)*a*sin(x))) )   )^2

waarde,matrix = scipy.optimize.curve_fit(f,X,Y)

plt.plot(X,Y,'p',label='data')

a = np.linspace(-0.0300,0.0300,1000)

b = b = f(a, waarde[0], waarde[1],waarde[2],waarde[3])

plt.plot(a,b,'r',label= 'fit')

plt.errorbar(X,Y,onzekerheidY,linestyle='None',label = 'onzekerheden')

plt.grid()

plt.legend(loc = 'lower left')

plt.title('Fit van \$R_{TM}\$ in functie van \$\Theta\$ \$_i\$')

plt.xlabel('\$\Theta\$ \$_i\$')

plt.ylabel('\$R_{TM}\$')

#plt.axis([0,1.6,0,100])

#plt.savefig('Labo_2_fit_2_meh.pdf')

by (180 points)

Here's the corrected version of your code:

import numpy as np

import scipy.optimize

import matplotlib.pyplot as plt

X = [0,-0.0000959936,-0.000194899,-0.000273446,-0.000430558,-0.000366546,-0.000573153,-0.000657563,-0.000768193,0.000066904,0.000148355,0.000232717,

0.000285083,0.000427648,0.000401461,0.000526588,0.0000465422,-0.0000250891,0.0000436333,-0.0000261799,-0.0000610866,0.0000930847,-0.000101811]

Y = [0.96,0.47,0.11,0.25,0.15,0.12,0.13,0.11,0.11,0.78,0.16,0.19,0.13,0.14,0.16,0.13,0.6,2.02,1.91,1.4,0.94,0.68,0.97]

onzekerheidY = [0.0001,0.000001,-0.000101,-0.000182,-0.000344,-0.000278,-0.000491,-0.000578,-0.000692,0.000169,0.000253,0.00034,

0.000394,0.000541,0.000514,0.000643,0.000148,0.000074125,0.000145,0.000073,0.000037,0.000196,-0.000005]

def f(x, I0, theta, a, d):

k = 9666438.934

N = 1

return I0 * (np.sin(k * (d / 2) * np.sin(x)) / (k * (d / 2) * np.sin(x)) * (np.sin(k * a * np.sin(x) * (N / 2))) / (N * np.sin((k / 2) * a * np.sin(x)))) ** 2

waarde, matrix = scipy.optimize.curve_fit(f, X, Y)

plt.plot(X, Y, 'p', label='data')

a = np.linspace(-0.0300, 0.0300, 1000)

b = f(a, waarde[0], waarde[1], waarde[2], waarde[3])

plt.plot(a, b, 'r', label='fit')

plt.errorbar(X, Y, onzekerheidY, linestyle='None', label='onzekerheden')

plt.grid()

plt.legend(loc='lower left')

plt.title('Fit van \$R_{TM}\$ in functie van \$\Theta\$ \$_i\$')

plt.xlabel('\$\Theta\$ \$_i\$')

plt.ylabel('\$R_{TM}\$')

plt.show()

Please note that you may need to import the necessary libraries (numpy, scipy, and matplotlib) before running the code. Also, ensure that you have the correct plotting options (such as saving the figure) uncommented and properly configured according to your needs.
by (220 points)

Given below is the correct code for this:

import numpy as np

import scipy.optimize

import matplotlib.pyplot as plt

X = [0,-0.0000959936,-0.000194899,-0.000273446,-0.000430558,-0.000366546,-0.000573153,-0.000657563,-0.000768193,0.000066904,0.000148355,0.000232717,

0.000285083,0.000427648,0.000401461,0.000526588,0.0000465422,-0.0000250891,0.0000436333,-0.0000261799,-0.0000610866,0.0000930847,-0.000101811]

Y = [0.96,0.47,0.11,0.25,0.15,0.12,0.13,0.11,0.11,0.78,0.16,0.19,0.13,0.14,0.16,0.13,0.6,2.02,1.91,1.4,0.94,0.68,0.97]

onzekerheidY = [0.0001,0.000001,-0.000101,-0.000182,-0.000344,-0.000278,-0.000491,-0.000578,-0.000692,0.000169,0.000253,0.00034,

0.000394,0.000541,0.000514,0.000643,0.000148,0.000074125,0.000145,0.000073,0.000037,0.000196,-0.000005]

def f(x, I0, theta, a, d):

k = 9666438.934

N = 1

return I0 * (np.sin(k * (d / 2) * np.sin(x)) / (k * (d / 2) * np.sin(x)) * (np.sin(k * a * np.sin(x) * (N / 2))) / (N * np.sin((k / 2) * a * np.sin(x)))) ** 2

waarde, matrix = scipy.optimize.curve_fit(f, X, Y)

plt.plot(X, Y, 'p', label='data')

a = np.linspace(-0.0300, 0.0300, 1000)

b = f(a, waarde[0], waarde[1], waarde[2], waarde[3])

plt.plot(a, b, 'r', label='fit')

plt.errorbar(X, Y, onzekerheidY, linestyle='None', label='onzekerheden')

plt.grid()

plt.legend(loc='lower left')

plt.title('Fit van \$R_{TM}\$ in functie van \$\Theta\$ \$_i\$')

plt.xlabel('\$\Theta\$ \$_i\$')

plt.ylabel('\$R_{TM}\$')

plt.show()

by (180 points)
import numpy as np

import scipy.optimize

import matplotlib.pyplot as plt

X = [0,-0.0000959936,-0.000194899,-0.000273446,-0.000430558,-0.000366546,-0.000573153,-0.000657563,-0.000768193,0.000066904,0.000148355,0.000232717,

0.000285083,0.000427648,0.000401461,0.000526588,0.0000465422,-0.0000250891,0.0000436333,-0.0000261799,-0.0000610866,0.0000930847,-0.000101811]

Y = [0.96,0.47,0.11,0.25,0.15,0.12,0.13,0.11,0.11,0.78,0.16,0.19,0.13,0.14,0.16,0.13,0.6,2.02,1.91,1.4,0.94,0.68,0.97]

onzekerheidY = [0.0001,0.000001,-0.000101,-0.000182,-0.000344,-0.000278,-0.000491,-0.000578,-0.000692,0.000169,0.000253,0.00034,

0.000394,0.000541,0.000514,0.000643,0.000148,0.000074125,0.000145,0.000073,0.000037,0.000196,-0.000005]

def f(x, I0, theta, a, d):

k = 9666438.934

N = 1

return I0 * (np.sin(k * (d / 2) * np.sin(x)) / (k * (d / 2) * np.sin(x)) * (np.sin(k * a * np.sin(x) * (N / 2))) / (N * np.sin((k / 2) * a * np.sin(x)))) ** 2

waarde, matrix = scipy.optimize.curve_fit(f, X, Y)

plt.plot(X, Y, 'p', label='data')

a = np.linspace(-0.0300, 0.0300, 1000)

b = f(a, waarde[0], waarde[1], waarde[2], waarde[3])

plt.plot(a, b, 'r', label='fit')

plt.errorbar(X, Y, onzekerheidY, linestyle='None', label='onzekerheden')

plt.grid()

plt.legend(loc='lower left')

plt.title('Fit van \$R_{TM}\$ in functie van \$\Theta\$ \$_i\$')

plt.xlabel('\$\Theta\$ \$_i\$')

plt.ylabel('\$R_{TM}\$')

plt.show()

Kindly take note that before running the code, you may be required to import the required libraries, namely numpy, scipy, and matplotlib. Additionally, ensure that the desired plotting options, such as saving the figure, are uncommented and correctly configured to meet your specific requirements.
by (180 points)
I just want to say that the error "only length-1 arrays can be converted to Python scalars" is raised when the function expects a single value but you pass an array instead.