Fitting cosmological parameters to data with fminsearch in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter kjata090
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 9K views
kjata090
Messages
3
Reaction score
0
okay so my problem is that I have some data in some arrays z(:,1), u(:,1), s(:,1)

okay I am supposed to use this data with these equations (these are not syntax because I am not sure how to implement this into MATLAB yet)

H = (omega*(1+z)^3 + (1-omega)*(1+z)^(3*(1+w)))^.5

distLum = (1+z)*integral(1/H,0,z) (that is the integral of 1/H with respect to z from 0 to z)

ufit = 5*log(distLum) + M

basically what I need to do is find the minimum of chi^2

chi^2 = sum of data (u - ufit)^2/s^2)

I hope I explained that sort of. you can see this is quite a problem and not exaclt sure how to use MATLAB to do this, I was told fminsearch could do the trick. The values I know are z,u,s. My var/para are omega,w,M.
 
Physics news on Phys.org
In general, MATLAB has very good documentation. You can use the Help, or you can type "help fminsearch" (or "help any_function") at the command prompt and it describes the functions. Try this, and then ask some more questions.

I'm going to guess that fminsearch is not the function you want, but I could be wrong... Maybe you can provide more detail about what you're trying to do? Is this some kind of curve fitting method?

-Kerry
 
I was able to get what I need to, except for one problem.
H is some function. I need to take an integration from 0 to z where z is an array of values and I want to put the answer also into an array to be used

I am trying to take an integration quad(H,0,z), but you can't have that because its not a scalar how can I work around this within function file?

currently I have

function chisquare = chifunc(x,mu_b,z,sig,n)
omega = x(1);
w = x(2);
M = x(3);

H = @(z)hubble(x,z);
distLum = (1+z).* quad(H,0,z); <---error here
mu_fit = 5*log(distLum) + M;
chi = (mu_b-mu_fit).^2./sig.^2;
chisquare = sum(chi);
 
It sounds like you need a for..loop. It would look something like this:

Code:
integral_array = zeros(size(z));
for i = 1:1:max(size(z)) % Go from 1 to the last element of z, incrementing by 1 every time
    distLum(i) = (1+z(i)).* quad(H,0,z(i));
end

This will loop through every element of z, do the integration, and store the results in a vector called distLum, which has the same size as z.

I'm not sure if log() works on vectors or not (it probably does...) but you can easily add this to the loop, too, if you need to.

Hope this helps!

-Kerry