How do I fit a function that includes an integral with a variable limit?












2














I'm very new to Python (and also Stack Overflow so sorry if I'm not doing this right!). I'm trying to fit the following equation to some data in order to extract the cosmological parameters ΩM and ΩΛ:



Equation to fit



where



Curly D equation



In my equation, capital curly D=Ho*dl so the Ho has been cancelled out. I'm currently trying to use the ΩM+ΩΛ=1 form. I have the data for m(z) and z, and I know the curly M constant.



This is the code I have at the moment:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

d=np.loadtxt("data.txt")
z=d[:,0]
m=d[:,7]
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(z,OM,OV):
return M+5*np.log10(c*(1+z)*integrate.quad(fn,0,z,args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)


When I run this, I get the error:



ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() 
or a.all()


I understand that this is because you can't use an independent array variable as a limit in the quad function, but I'm not sure how I would go about correcting this as nothing I've found or tried to implement has worked. Very much appreciate any help or hints! Thank you in advance.



UPDATE:



here is some example data that I have been using:



z=[0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079, 
0.088, 0.063, 0.071, 0.052, 0.05]
m=[16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69]









share|improve this question
























  • can you go through this with example data for z and m? print them out and paste a short version of it here -- try to put a minimally complete description of your problem
    – ehacinom
    Nov 20 at 20:35












  • Note that if you use Omega_M + Omega_Lambda = 1 you only have 1 fit parameter. The second is fixed by the sum condition
    – mikuszefski
    Nov 26 at 9:37
















2














I'm very new to Python (and also Stack Overflow so sorry if I'm not doing this right!). I'm trying to fit the following equation to some data in order to extract the cosmological parameters ΩM and ΩΛ:



Equation to fit



where



Curly D equation



In my equation, capital curly D=Ho*dl so the Ho has been cancelled out. I'm currently trying to use the ΩM+ΩΛ=1 form. I have the data for m(z) and z, and I know the curly M constant.



This is the code I have at the moment:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

d=np.loadtxt("data.txt")
z=d[:,0]
m=d[:,7]
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(z,OM,OV):
return M+5*np.log10(c*(1+z)*integrate.quad(fn,0,z,args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)


When I run this, I get the error:



ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() 
or a.all()


I understand that this is because you can't use an independent array variable as a limit in the quad function, but I'm not sure how I would go about correcting this as nothing I've found or tried to implement has worked. Very much appreciate any help or hints! Thank you in advance.



UPDATE:



here is some example data that I have been using:



z=[0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079, 
0.088, 0.063, 0.071, 0.052, 0.05]
m=[16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69]









share|improve this question
























  • can you go through this with example data for z and m? print them out and paste a short version of it here -- try to put a minimally complete description of your problem
    – ehacinom
    Nov 20 at 20:35












  • Note that if you use Omega_M + Omega_Lambda = 1 you only have 1 fit parameter. The second is fixed by the sum condition
    – mikuszefski
    Nov 26 at 9:37














2












2








2







I'm very new to Python (and also Stack Overflow so sorry if I'm not doing this right!). I'm trying to fit the following equation to some data in order to extract the cosmological parameters ΩM and ΩΛ:



Equation to fit



where



Curly D equation



In my equation, capital curly D=Ho*dl so the Ho has been cancelled out. I'm currently trying to use the ΩM+ΩΛ=1 form. I have the data for m(z) and z, and I know the curly M constant.



This is the code I have at the moment:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

d=np.loadtxt("data.txt")
z=d[:,0]
m=d[:,7]
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(z,OM,OV):
return M+5*np.log10(c*(1+z)*integrate.quad(fn,0,z,args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)


When I run this, I get the error:



ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() 
or a.all()


I understand that this is because you can't use an independent array variable as a limit in the quad function, but I'm not sure how I would go about correcting this as nothing I've found or tried to implement has worked. Very much appreciate any help or hints! Thank you in advance.



UPDATE:



here is some example data that I have been using:



z=[0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079, 
0.088, 0.063, 0.071, 0.052, 0.05]
m=[16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69]









share|improve this question















I'm very new to Python (and also Stack Overflow so sorry if I'm not doing this right!). I'm trying to fit the following equation to some data in order to extract the cosmological parameters ΩM and ΩΛ:



Equation to fit



where



Curly D equation



In my equation, capital curly D=Ho*dl so the Ho has been cancelled out. I'm currently trying to use the ΩM+ΩΛ=1 form. I have the data for m(z) and z, and I know the curly M constant.



This is the code I have at the moment:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

d=np.loadtxt("data.txt")
z=d[:,0]
m=d[:,7]
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(z,OM,OV):
return M+5*np.log10(c*(1+z)*integrate.quad(fn,0,z,args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)


When I run this, I get the error:



ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() 
or a.all()


I understand that this is because you can't use an independent array variable as a limit in the quad function, but I'm not sure how I would go about correcting this as nothing I've found or tried to implement has worked. Very much appreciate any help or hints! Thank you in advance.



UPDATE:



here is some example data that I have been using:



z=[0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079, 
0.088, 0.063, 0.071, 0.052, 0.05]
m=[16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69]






python curve-fitting integral






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:37

























asked Nov 20 at 20:21









Kousha Wrigley

183




183












  • can you go through this with example data for z and m? print them out and paste a short version of it here -- try to put a minimally complete description of your problem
    – ehacinom
    Nov 20 at 20:35












  • Note that if you use Omega_M + Omega_Lambda = 1 you only have 1 fit parameter. The second is fixed by the sum condition
    – mikuszefski
    Nov 26 at 9:37


















  • can you go through this with example data for z and m? print them out and paste a short version of it here -- try to put a minimally complete description of your problem
    – ehacinom
    Nov 20 at 20:35












  • Note that if you use Omega_M + Omega_Lambda = 1 you only have 1 fit parameter. The second is fixed by the sum condition
    – mikuszefski
    Nov 26 at 9:37
















can you go through this with example data for z and m? print them out and paste a short version of it here -- try to put a minimally complete description of your problem
– ehacinom
Nov 20 at 20:35






can you go through this with example data for z and m? print them out and paste a short version of it here -- try to put a minimally complete description of your problem
– ehacinom
Nov 20 at 20:35














Note that if you use Omega_M + Omega_Lambda = 1 you only have 1 fit parameter. The second is fixed by the sum condition
– mikuszefski
Nov 26 at 9:37




Note that if you use Omega_M + Omega_Lambda = 1 you only have 1 fit parameter. The second is fixed by the sum condition
– mikuszefski
Nov 26 at 9:37












1 Answer
1






active

oldest

votes


















1














I'm not sure that it is an answer you looking for, but it may help you to find a real solution for your (scientific) problem.



So, on the programming part, the error appears because curve_fit sending a vector of z as a first argument of the function curve However integrate.quad wants two float numbers (integration limits) as the second and third arguments. Well, integrate.quad gets an array in the third argument, and is very upset about this - your strange error message.



NOW I guess you want to integrate from 0 to a biggest z. If so your code should look like this:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

z=np.array( [0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079,
0.088, 0.063, 0.071, 0.052, 0.05] )
m=np.array( [16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69] )
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(R,OM,OV):
return M+5*np.log10(c*(1+R)*integrate.quad(fn,0,R[-1],args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

print parameter


This generates some results:



(array([-61.73621869, -42.41853305]), array([[5.43407019e+15, 2.84207191e+15],
[2.84207191e+15, 1.48643143e+15]]))


which may or may not be useful numbers. At least now you understand a source of the error and can change your code in such a way, that you will solve the biggest cosmological problem.



Good luck!






share|improve this answer





















  • Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
    – mikuszefski
    Nov 26 at 9:28













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400945%2fhow-do-i-fit-a-function-that-includes-an-integral-with-a-variable-limit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I'm not sure that it is an answer you looking for, but it may help you to find a real solution for your (scientific) problem.



So, on the programming part, the error appears because curve_fit sending a vector of z as a first argument of the function curve However integrate.quad wants two float numbers (integration limits) as the second and third arguments. Well, integrate.quad gets an array in the third argument, and is very upset about this - your strange error message.



NOW I guess you want to integrate from 0 to a biggest z. If so your code should look like this:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

z=np.array( [0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079,
0.088, 0.063, 0.071, 0.052, 0.05] )
m=np.array( [16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69] )
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(R,OM,OV):
return M+5*np.log10(c*(1+R)*integrate.quad(fn,0,R[-1],args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

print parameter


This generates some results:



(array([-61.73621869, -42.41853305]), array([[5.43407019e+15, 2.84207191e+15],
[2.84207191e+15, 1.48643143e+15]]))


which may or may not be useful numbers. At least now you understand a source of the error and can change your code in such a way, that you will solve the biggest cosmological problem.



Good luck!






share|improve this answer





















  • Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
    – mikuszefski
    Nov 26 at 9:28


















1














I'm not sure that it is an answer you looking for, but it may help you to find a real solution for your (scientific) problem.



So, on the programming part, the error appears because curve_fit sending a vector of z as a first argument of the function curve However integrate.quad wants two float numbers (integration limits) as the second and third arguments. Well, integrate.quad gets an array in the third argument, and is very upset about this - your strange error message.



NOW I guess you want to integrate from 0 to a biggest z. If so your code should look like this:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

z=np.array( [0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079,
0.088, 0.063, 0.071, 0.052, 0.05] )
m=np.array( [16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69] )
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(R,OM,OV):
return M+5*np.log10(c*(1+R)*integrate.quad(fn,0,R[-1],args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

print parameter


This generates some results:



(array([-61.73621869, -42.41853305]), array([[5.43407019e+15, 2.84207191e+15],
[2.84207191e+15, 1.48643143e+15]]))


which may or may not be useful numbers. At least now you understand a source of the error and can change your code in such a way, that you will solve the biggest cosmological problem.



Good luck!






share|improve this answer





















  • Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
    – mikuszefski
    Nov 26 at 9:28
















1












1








1






I'm not sure that it is an answer you looking for, but it may help you to find a real solution for your (scientific) problem.



So, on the programming part, the error appears because curve_fit sending a vector of z as a first argument of the function curve However integrate.quad wants two float numbers (integration limits) as the second and third arguments. Well, integrate.quad gets an array in the third argument, and is very upset about this - your strange error message.



NOW I guess you want to integrate from 0 to a biggest z. If so your code should look like this:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

z=np.array( [0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079,
0.088, 0.063, 0.071, 0.052, 0.05] )
m=np.array( [16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69] )
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(R,OM,OV):
return M+5*np.log10(c*(1+R)*integrate.quad(fn,0,R[-1],args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

print parameter


This generates some results:



(array([-61.73621869, -42.41853305]), array([[5.43407019e+15, 2.84207191e+15],
[2.84207191e+15, 1.48643143e+15]]))


which may or may not be useful numbers. At least now you understand a source of the error and can change your code in such a way, that you will solve the biggest cosmological problem.



Good luck!






share|improve this answer












I'm not sure that it is an answer you looking for, but it may help you to find a real solution for your (scientific) problem.



So, on the programming part, the error appears because curve_fit sending a vector of z as a first argument of the function curve However integrate.quad wants two float numbers (integration limits) as the second and third arguments. Well, integrate.quad gets an array in the third argument, and is very upset about this - your strange error message.



NOW I guess you want to integrate from 0 to a biggest z. If so your code should look like this:



import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

z=np.array( [0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079,
0.088, 0.063, 0.071, 0.052, 0.05] )
m=np.array( [16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61,
18.27, 19.28, 18.24, 18.33, 17.54, 17.69] )
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(R,OM,OV):
return M+5*np.log10(c*(1+R)*integrate.quad(fn,0,R[-1],args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

print parameter


This generates some results:



(array([-61.73621869, -42.41853305]), array([[5.43407019e+15, 2.84207191e+15],
[2.84207191e+15, 1.48643143e+15]]))


which may or may not be useful numbers. At least now you understand a source of the error and can change your code in such a way, that you will solve the biggest cosmological problem.



Good luck!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 23:02









rth

1,190715




1,190715












  • Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
    – mikuszefski
    Nov 26 at 9:28




















  • Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
    – mikuszefski
    Nov 26 at 9:28


















Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
– mikuszefski
Nov 26 at 9:28






Nope. First, the z list is not sorted, so assuming that R[-1] is the largest value is, in general, wrong. Second,the assumption that the integration is up to the largest z is wrong. In this case it is a function where the parameter defines the upper boundary as in f(x) = int_0^x ( k^2 dk ).
– mikuszefski
Nov 26 at 9:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400945%2fhow-do-i-fit-a-function-that-includes-an-integral-with-a-variable-limit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'