0

I tried to fit the flux of cosmic rays versus energy for AllParticles&H. I used the CRDB package to extract the data. Then, i used the power law to fit. However, the shape of x is (1236,) and the error in y is (1236,2). how do i fix the shape issue?

Here is my code:

 x = t_combined.e 
y = t_combined.value 
err = t_combined.err_sta
    lsq = LeastSquares(x, y, err, power_law) 
m = Minuit(lsq, a=1, gamma=-2.0)
    plt.errorbar(x, y, err, fmt="o", label="data") 
plt.plot(x, (x, *m.values), label="fit") # what does this line do?
    ax.scatter(x, y, label="Combined original data", marker="x")
    a_fit = minuit.values.a gamma_fit = minuit.values.gamma
    x_fit = np.logspace(np.log10(t_combined['e'].min()), np.log10(t_combined['e'].max()), 100) y_fit = power_law(x_fit, a_fit, gamma_fit) 
ax.plot(x_fit, y_fit, label="Fitted power law", linestyle='--', color='red')
    plt.xlabel(r"𝐸𝑘 [GeV]") 
plt.ylabel(r"𝐸𝑘 d𝐽/d𝐸𝑘 [1/(m2 s sr)]") 
plt.title('Power Law Fit') 
plt.legend() 
plt.xscale('log')
    plt.yscale('log') 
plt.show()
    print("Fitted parameters (a, gamma):", m)
    plt.show()
4
  • Please always provide a minimal reproducible example as a single block of formatted code that one can simply copy, paste and run. Commented Feb 11, 2024 at 13:06
  • 1
    I just indented the code. I hope it is better now! Commented Feb 11, 2024 at 13:14
  • x = t_combined.e y = t_combined.value err = t_combined.err_sta ^ SyntaxError: invalid syntax - Please copy, paste and try to run the code you posted to check that it is valid. Commented Feb 11, 2024 at 13:16
  • It's an indentation issue that i did not check while doing Ctrl+K. These variables are supposed to be on separate lines! Commented Feb 11, 2024 at 13:20

0

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.