MATLAB How Can fminsearch Be Used to Optimize Parameters in MATLAB?

  • Thread starter Thread starter kjata090
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion revolves around using MATLAB to solve a problem involving data arrays z, u, and s, and applying specific equations to calculate a chi-squared value. The key equations include a function H based on parameters omega, w, and M, and the calculation of luminosity distance (distLum) through integration. The user initially struggles with integrating a non-scalar function over an array of values for z. A suggested solution involves using a for loop to iterate through each element of z, performing the integration individually, and storing the results in an array. This approach allows for the calculation of distLum while ensuring compatibility with the subsequent chi-squared calculation. The discussion emphasizes the importance of MATLAB's documentation for function usage and problem-solving.
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
 

Similar threads

Back
Top