How Can fminsearch Be Used to Optimize Parameters in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter kjata090
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around using the fminsearch function in MATLAB to optimize parameters for a model based on provided data arrays. Participants explore the implementation of equations involving integration and parameter fitting, specifically focusing on minimizing chi-squared values.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes their problem involving data arrays and equations that need to be implemented in MATLAB, specifically mentioning the need to minimize chi-squared.
  • Another participant suggests that fminsearch may not be the appropriate function for the task and asks for clarification on whether the problem is related to curve fitting.
  • A participant expresses difficulty in performing integration over an array of values and seeks advice on how to implement this in a function file.
  • One suggestion involves using a for loop to iterate through the elements of the array z to perform the integration for each element, storing the results in a new array.
  • There is uncertainty about whether the log function can operate on vectors, with a suggestion that it likely can but requires confirmation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the suitability of fminsearch for the task, and there are differing views on how to handle the integration of an array. The discussion remains unresolved regarding the best approach to implement the integration and parameter optimization.

Contextual Notes

Participants express uncertainty about the capabilities of certain MATLAB functions, such as whether log can handle vector inputs, and there are unresolved issues regarding the integration of non-scalar values.

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

Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K