Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need some help using fminsearch in matlab

  1. Jul 6, 2009 #1
    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.
  2. jcsd
  3. Jul 7, 2009 #2
    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?

  4. Jul 7, 2009 #3
    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 cant 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);
  5. Jul 8, 2009 #4
    It sounds like you need a for..loop. It would look something like this:

    Code (Text):

    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));
    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!

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook