From: Adrian gavrila on 31 Mar 2010 06:26 Hello, I have a big problem. I am doing an optimization problem on a syncronous machine, matlab coupled with FEMM 4.2. I am using --fminsearch-- as function for optimization, but the problem is that I can only input one value to be modified : @(rad_mag) [M,fval,exitflag,output]=@(rad_mag) (fminsearch(@(rad_mag) PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options)); The function PMSM_m_mod uses a coupling with Femm so my optimized expresion is a .m file in Femm library: mo_blockintegral(22), basicly I cannot see the function beeing optimized (ex:banana = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;) mine is T=mo_blockintegral(22); function T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag); ..... ..... mo_groupselectblock(20555) T=mo_blockintegral(22); and so do as the banana example is made.(---x(2)-x(1)---are 2 variables that are modified instead of one) How can I input 2 variables or more in my model, I have read about Anonymous Functions this is a method but I cannot get it to work. Also in fminsearch help it is written that : fminsearch attempts to find a minimum of a scalar function of ----several variables----(HOW????), starting at an initial estimate. Please help me. Thank you.
From: Steven Lord on 31 Mar 2010 09:39 "Adrian gavrila" <adig_coool(a)yahoo.com> wrote in message news:hov7vs$5im$1(a)fred.mathworks.com... > Hello, > > I have a big problem. > I am doing an optimization problem on a syncronous machine, matlab coupled > with FEMM 4.2. I am using --fminsearch-- as function for optimization, but > the problem is that I can only input one value to be modified : @(rad_mag) No. You can only input one _variable_ that FMINSEARCH will use to optimize your function, not one _value_. There's a big difference there. Take a look at the first example on the reference page for FMINSEARCH and I think you'll see how to do what you want. http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fminsearch.html > [M,fval,exitflag,output]=@(rad_mag) (fminsearch(@(rad_mag) > PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options)); The statement that _creates_ an anonymous function can have at most one output argument. You have one too many instances of "@(rad_mag)" in your code -- get rid of the first one. When you _call_ an anonymous function, if the function returns multiple output arguments, you can call it with multiple outputs. > The function PMSM_m_mod uses a coupling with Femm so my optimized > expresion is a .m file in Femm library: mo_blockintegral(22), basicly I > cannot see the function beeing optimized (ex:banana = > @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;) mine is T=mo_blockintegral(22); That's fine. FMINSEARCH doesn't really care what you do to evaluate the function at the points it passes into your function -- all it really cares about is that you can evaluate the function. You could have your function display "Search the web to locate the average temperature at latitude 42 degrees 17 minutes 00 seconds N, longitude 71 degrees 21 minutes 00 seconds W on July 4th and enter that value here: " and wait for the user to follow the instructions. That would be perfectly acceptable to FMINSEARCH. > function > T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag); > .... > .... > mo_groupselectblock(20555) T=mo_blockintegral(22); > > and so do as the banana example is made.(---x(2)-x(1)---are 2 variables > that are modified instead of one) The anonymous function here is serving as an "adapter" -- FMINSEARCH expects the function you pass into it to be a function with one input argument, and that's the face the anonymous function shows to it. Your PMSM_m_mod function expects seven input arguments, and that's how the anonymous function calls it. > How can I input 2 variables or more in my model, I have read about > Anonymous Functions this is a method but I cannot get it to work. In your call above, you're telling FMINSEARCH that your function accepts a 1-element vector as input (because that's the size of x0.) If you changed x0 to be a 1-by-2 or 2-by-1 vector, things might turn out differently. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Rakesh Kumar on 31 Mar 2010 09:57 "Adrian gavrila" <adig_coool(a)yahoo.com> wrote in message <hov7vs$5im$1(a)fred.mathworks.com>... > Hello, > > I have a big problem. > I am doing an optimization problem on a syncronous machine, matlab coupled with FEMM 4.2. I am using --fminsearch-- as function for optimization, but the problem is that I can only input one value to be modified : @(rad_mag) > > [M,fval,exitflag,output]=@(rad_mag) (fminsearch(@(rad_mag) PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options)); > > > The function PMSM_m_mod uses a coupling with Femm so my optimized expresion is a .m file in Femm library: mo_blockintegral(22), basicly I cannot see the function beeing optimized (ex:banana = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;) mine is T=mo_blockintegral(22); > > function T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag); > .... > .... > mo_groupselectblock(20555) > T=mo_blockintegral(22); > > and so do as the banana example is made.(---x(2)-x(1)---are 2 variables that are modified instead of one) > > > How can I input 2 variables or more in my model, I have read about Anonymous Functions this is a method but I cannot get it to work. > > Also in fminsearch help it is written that : fminsearch attempts to find a minimum of a scalar function of ----several variables----(HOW????), starting at an initial estimate. > > Please help me. > > Thank you. I am not sure I understand the problem correctly but I will try to help you. I think you have a function that takes 'multiple scalar input' something like: myfun(arg1,arg2,arg3....) You want to know how I can pass all the arguments (arg1, arg2, etc) to this function because fmincon takes only one input. Is that the question? If it is then there is a simple solution to this. The objective function that you supply to fminsearch must take one argument that can be a 'vector' so you will have to write a wrapper function (say myobjective.m) that takes in a vector input (say x) and calls 'myfun.m'. Here is how it will all look like: ========================================== function y = myfun(arg1,arg2,arg3) % You do not have any control over how this function is called. ========================================== function fval = myobjective(x) arg1 = x(1); arg2 = x(2); arg3 = x(3); fval = myfun(arg1,arg2,arg3); =========================================== A call to fminsearch will look like: [x,f] = fminsearch(@myobjective,xInitial); Your other question about "scalar function". The function (say myobjective.m) being optimized must return a scalar value argument. It can take a vector input argument. hth, Rakesh
From: Adrian gavrila on 31 Mar 2010 10:35 Hello, This helps. Thank you. Some more questions down below. "Steven Lord" <slord(a)mathworks.com> wrote in message <hovj9n$nki$1(a)fred.mathworks.com>... > > "Adrian gavrila" <adig_coool(a)yahoo.com> wrote in message > news:hov7vs$5im$1(a)fred.mathworks.com... > > Hello, > > > > I have a big problem. > > I am doing an optimization problem on a syncronous machine, matlab coupled > > with FEMM 4.2. I am using --fminsearch-- as function for optimization, but > > the problem is that I can only input one value to be modified : @(rad_mag) > > No. You can only input one _variable_ that FMINSEARCH will use to optimize > your function, not one _value_. There's a big difference there. Take a > look at the first example on the reference page for FMINSEARCH and I think > you'll see how to do what you want. > > http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fminsearch.html > ============================================ Sorry I ment variable.I got that.So Fminsearch ====> only one variable as input ===>variable that can be a scalar or a vector. =========================================== > > [M,fval,exitflag,output]=@(rad_mag) (fminsearch(@(rad_mag) > > PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options)); > > The statement that _creates_ an anonymous function can have at most one > output argument. You have one too many instances of "@(rad_mag)" in your > code -- get rid of the first one. When you _call_ an anonymous function, if > the function returns multiple output arguments, you can call it with > multiple outputs. > ================================================== IN the @(rad_mag) I have tried multiple options to see maybe it works, you are right in the original code the '' [M,fval,exitflag,output]=@(rad_mag) (fminsearc'' does not appear, and it works ok. =================================================== > > The function PMSM_m_mod uses a coupling with Femm so my optimized > > expresion is a .m file in Femm library: mo_blockintegral(22), basicly I > > cannot see the function beeing optimized (ex:banana = > > @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;) mine is T=mo_blockintegral(22); > > That's fine. FMINSEARCH doesn't really care what you do to evaluate the > function at the points it passes into your function -- all it really cares > about is that you can evaluate the function. You could have your function > display "Search the web to locate the average temperature at latitude 42 > degrees 17 minutes 00 seconds N, longitude 71 degrees 21 minutes 00 seconds > W on July 4th and enter that value here: " and wait for the user to follow > the instructions. That would be perfectly acceptable to FMINSEARCH. > =================================================== It works ok, but that I did not know how it works calling an .m file from another library, but as you say that is done ok with no problems and I do not need to know what it is in the .m file ''mo_blockintegral(22)''' as long as it runs ================================================= > > function > > T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag); > > .... > > .... > > mo_groupselectblock(20555) T=mo_blockintegral(22); > > > > and so do as the banana example is made.(---x(2)-x(1)---are 2 variables > > that are modified instead of one) > > The anonymous function here is serving as an "adapter" -- FMINSEARCH expects > the function you pass into it to be a function with one input argument, and > that's the face the anonymous function shows to it. Your PMSM_m_mod > function expects seven input arguments, and that's how the anonymous > function calls it. > ========================================= As for the 7 inputs that fminsearch aspects, I have it covered because , with that I define the 6 variables in the minim.m (fminsearch call==>as an minim.m file) as below: %rad_mag=13.5e-3 %-- radiu rotor up to the magnets diam_mag=2*13.5e-3 %--diameter rotor up the magnets diam_rot=33e-3 %--diameter rotor rad_ax=4.45e-3 %-- radius axe rotor rad_rot=16.5e-3 %-- radius outer rotor h_mag=3e-3 %-- height of magnet clear_s=0.3e-3 %-- half of the clearance between magnets %p =5 options =optimset('Display','iter','MaxIter',100,'MaxFunEvals',4,'OutputFcn',@outfun) hold on ============================= %%%%% also made an @outfun for printing values but as I can see I can only print the rad_mag value.I wish I can print the ===fval== value of the torque at each iteration as in report of number of Iterations============== HOW CAN I DO THAT? %%%%%% ================================ [M,fval,exitflag,output]=fminsearch(@(rad_mag) PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options); hold off %clear load('dates'); workspace so by defining it like this it is basically defining a global variable, it is easier to keep track of each variable that I input into the @()PMSM_m_mod, without going back to PMSM_m_mod.m file and redefine them. ===================================================== > > How can I input 2 variables or more in my model, I have read about > > Anonymous Functions this is a method but I cannot get it to work. > > In your call above, you're telling FMINSEARCH that your function accepts a > 1-element vector as input (because that's the size of x0.) If you changed > x0 to be a 1-by-2 or 2-by-1 vector, things might turn out differently. > > -- > Steve Lord > slord(a)mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > ================================ Yes that is a good ideea. I will try it to see what it does and if it does the trick ================================= This helps very much. Thank you.
From: Adrian gavrila on 31 Mar 2010 10:50 "Rakesh Kumar" <r.kumar_spamhole(a)mathworks.com> wrote in message <hovkbh$bbb$1(a)fred.mathworks.com>... > "Adrian gavrila" <adig_coool(a)yahoo.com> wrote in message <hov7vs$5im$1(a)fred.mathworks.com>... > > Hello, > > > > I have a big problem. > > I am doing an optimization problem on a syncronous machine, matlab coupled with FEMM 4.2. I am using --fminsearch-- as function for optimization, but the problem is that I can only input one value to be modified : @(rad_mag) > > > > [M,fval,exitflag,output]=@(rad_mag) (fminsearch(@(rad_mag) PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options)); > > > > > > The function PMSM_m_mod uses a coupling with Femm so my optimized expresion is a .m file in Femm library: mo_blockintegral(22), basicly I cannot see the function beeing optimized (ex:banana = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;) mine is T=mo_blockintegral(22); > > > > function T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag); > > .... > > .... > > mo_groupselectblock(20555) > > T=mo_blockintegral(22); > > > > and so do as the banana example is made.(---x(2)-x(1)---are 2 variables that are modified instead of one) > > > > > > How can I input 2 variables or more in my model, I have read about Anonymous Functions this is a method but I cannot get it to work. > > > > Also in fminsearch help it is written that : fminsearch attempts to find a minimum of a scalar function of ----several variables----(HOW????), starting at an initial estimate. > > > > Please help me. > > > > Thank you. > > I am not sure I understand the problem correctly but I will try to help you. I think you have a function that takes 'multiple scalar input' something like: > myfun(arg1,arg2,arg3....) > You want to know how I can pass all the arguments (arg1, arg2, etc) to this function because fmincon takes only one input. Is that the question? If it is then there is a simple solution to this. > > The objective function that you supply to fminsearch must take one argument that can be a 'vector' so you will have to write a wrapper function (say myobjective.m) that takes in a vector input (say x) and calls 'myfun.m'. Here is how it will all look like: > ========================================== > function y = myfun(arg1,arg2,arg3) > % You do not have any control over how this function is called. > ========================================== > function fval = myobjective(x) > arg1 = x(1); > arg2 = x(2); > arg3 = x(3); > fval = myfun(arg1,arg2,arg3); > =========================================== > A call to fminsearch will look like: > [x,f] = fminsearch(@myobjective,xInitial); > > Your other question about "scalar function". The function (say myobjective.m) being optimized must return a scalar value argument. It can take a vector input argument. > > hth, > Rakesh ================================================= Hello, Thank you for the help, This is how my call fminsearch function is: named: minim.m %rad_mag=13.5e-3 %-- radiu rotor up to the magnets diam_mag=2*13.5e-3 %--diameter rotor up the magnets diam_rot=33e-3 %--diameter rotor rad_ax=4.45e-3 %-- radius axe rotor rad_rot=16.5e-3 %-- radius outer rotor h_mag=3e-3 %-- height of magnet clear_s=0.3e-3 %-- half of the clearance between magnets %p =5 options =optimset('Display','iter','MaxIter',100,'MaxFunEvals',4,'OutputFcn',@outfun) hold on [M,fval,exitflag,output]=fminsearch(@(rad_mag) PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag),13.3e-3,options); hold off %clear %load('dates'); workspace %clear ======= and this is how my PMSM_m_mod.m file is , file called by fminsearch:==> called by minim.m in command window function T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag) .... .... .... mo_groupselectblock(20555) T=mo_blockintegral(22) ========== I will try to do a trick like that but I hope that solves the ideea of modifying the diam_mag=2*13.5e-3 %--diameter rotor up the ma diam_rot=33e-3 %--diameter rotor rad_ax=4.45e-3 %-- radius axe rotor rad_rot=16.5e-3 %-- radius outer rotor h_mag=3e-3 %-- height of magnet clear_s=0.3e-3 %-- half of the clearance between dimension, all at one optimization call of fmin. DO you think that is going to be made simultaniously? ============================================ Thnak you.
|
Pages: 1 Prev: Image convolution, low & high pass filters Next: plotyy and timeseries |