Using fminsearch : converting mex file to a function
Show older comments
iono_layer_parms = [a, b, -1, -1, -1, -1];
[iono, iono_extra] = iri2012(lat, lon, R12, UT, ht_start, ht_step,num_hts,iono_layer_parms);
ionofreq = 0.008978663597663*sqrt((10.^(-6))*(iono(1,18:end)));
ban = ionofreq(76:126) - D6xc;
[x,fval] = fminsearch(@(x)ban,[10.52, 490]);
This is the code I have written to use fminsearch for my purpose. I have all the variables defined for iri2012 except 'a' and 'b'. I want to change variables 'a' and 'b' only and want to mimize 'ban'. iri2012 is a Matlab wrapper to the IRI-2012 fortan based empirical model ionosphere. The actual programme is a mex wrapper (iri2012_matlab_wrapper.for) to the Fortran code (irisub.for). And when I run this code, I get the following errors :
Subscripted assignment dimension mismatch.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Error in Naldermead (line 25)
[x,fval] = fminsearch(@(x)ban,[10.52, 490]);
I want to take one of the outputs of iri2012 which is iono(1,18:end) .
The code is named Naldermead.m
I think there is more than these errors. The real pronblem may be converting the mex wrapper into a function. Please let me know if I can work on this problem with this approach.
6 Comments
John D'Errico
on 23 Feb 2015
So ionofreq returns a vector, ban?
Dev
on 23 Feb 2015
John D'Errico
on 23 Feb 2015
Please use the code formatting button when you post code. Select the code, then click on the "{} Code" button. Without that, your code is far less readable.
Next, there are way too many things wrong in just a few lines for me to want to wade in to fix. Maybe else has the energy to do that. But basically, it looks like you have no idea how tools like fminsearch work, nor do you understand functions. So really, you need to go back and do some of the basic work to learn MATLAB. Spend some time looking at the examples for fminsearch, and try them out. Try to optimize a simple function.
At the very least, you need to understand that fminsearch cannot optimize a vector. Maybe your goal is to minimize a sum of squares, or something like that. Fminsearch does not do that for you. Optimizers like fminsearch minimize a scalar objective. So if you are trying to fit a model of some sort to data, you need to form a sum of squares of residuals.
At the very least, spend some time learning how to write and use a function.
Dev
on 23 Feb 2015
John D'Errico
on 23 Feb 2015
So if ban is a vector, it is a CONSTANT. You may think of it as a function of a and b, but it is just a constant as you have it here.
You need to write a function that returns the sum of squares of the vector ban, where the function will take a and b as an argument vector of length 2. The function must compute ban internally, then return sum(ban.^2) to minimize.
Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!