From: Adrian gavrila on 29 Mar 2010 10:29 Hello, I have a problem, I am using an fminsearch function to determine a minimum value of my .m file function. I will post the code but, this will only work if the path is set with Femm. I am trying to plot the values of the FVAL variable in fminsearch function but this is not posible with OPTIMSET option.I am trying to implement a vector a=[] where I can store the value of the function that I want to store (T-Torque), this is beeing implemented in the .m file. What happens is, after each iteration the vector is rewritten and so the previous value is lost. So my question is : how can I store the FVAL result in a vector after each iteration without deleting the previous value OR even better plotting the FVAL with all the iterations values ( an example is psoptimset / pattern search function that has PlotFcns included in options). More details in the code. Thank you for then help. ___________________THIS IS THE M FILE_____________ %--permanet magnets synchronous motor-- function T=PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag,p); openfemm() opendocument('materials.fem') %-- A file with materials chosen %--dofile('topologie_PMSM.lua') mi_setgrid(1e-3,'cart') mi_probdef(0,'meters','planar',1e-8,1,20) pi = 3.14159265 mi_addnode(0,0) %----------------------block inputs----------------------------------------------------------------- I1=3 %--current on A circuit I2=3 %--current on B circuit I3=3 %--current on C circuit theta=0 %--angle of rotor spining x = 4 %--mesh size coefficient mstat=((1e-3)*x) %--stator iron mesh size mrot=((1e-3)*(3*x/2)) %--rotor iron mesh size mgap=((1e-3)*(1e-1)*(2*x)) %--airgap mesh size q=((1e-3)*x) %--coils mesh size mmag=((1e-3)*(1e-1)*(2*x+2)) %--permanent magnets mesh size %----------------------stator----------------------------------------------------------------- tooth_s = 12e-3 %-- number of stator teeth ext_s= 30.5e-3 %-- outer stator radius int_s= 17e-3 %-- inner stator radius mid_s= 27e-3 %-- middle stator radius mid1_s= 18.3e-3 %-- under middle stator radius int1_s= 17.3e-3 %-- up inner stator radius h_base= 0.3e-3 %-- height base tooth stator h_mid= 1e-3 %-- height middle slot stator w_tooth = 6e-3 %-- stator tooth weight h_yoke = 13.5e-3 %-- stator yoke height slot_dist=1/2 ang_r=180/12 sin_r= sin(pi/12) cos_r= cos(pi/12) sin_5= sin(pi/36) cos_5= cos(pi/36) mi_addnode(0,int_s) %--s1 mi_addnode(0,mid_s) %--s2 mi_addnode(0,ext_s) %--s3 mi_addnode(sin_r*mid_s,cos_r*mid_s) %--s4 mi_addnode(sin_r*ext_s,cos_r*ext_s) %--s5 mi_addnode(sin_5*ext_s,cos_5*ext_s) %--s55 mi_addnode(w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))) %--s6 mi_addnode(sin_r*int_s,cos_r*int_s) %--s10 mi_addnode(sin_r*int1_s,cos_r*int1_s) %--s12 ang_n=360/(2*34*pi) mi_addnode(sin(pi/12-(ang_n/180)*pi)*int_s,cos(pi/12-(ang_n/180)*pi)*int_s) %--s9 mi_addnode(sin(pi/12-(ang_n/180)*pi)*(int1_s),cos(pi/12-(ang_n/180)*pi)*(int1_s)) %--s8 mi_addnode(w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))) %--s6 mi_addnode(w_tooth/2,sqrt(mid1_s*mid1_s-(w_tooth/2)*(w_tooth/2))) %--s7 %-- mi_addnode(mid1_s*sin_r,mid1_s*cos_r) %--s11 %-- mi_addsegment(sin(pi/12-(ang_n/180)*pi)*int_s,cos(pi/12-(ang_n/180)*pi)*int_s,sin(pi/12-(ang_n/180)*pi)*(int1_s),cos(pi/12-(ang_n/180)*pi)*(int1_s)) %--s8 -> s9 mi_addsegment(w_tooth/2,sqrt(mid1_s*mid1_s-(w_tooth/2)*(w_tooth/2)),sin(pi/12-(ang_n/180)*pi)*(int1_s),cos(pi/12-(ang_n/180)*pi)*(int1_s)) %--s7 -> s8 mi_addsegment(w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2)),w_tooth/2,sqrt(mid1_s*mid1_s-(w_tooth/2)*(w_tooth/2))) %--s6 -> s7 mi_addsegment(sin_r*mid_s,cos_r*mid_s,w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))) %--s6 -> s4 mi_addarc(sin_r*int_s,cos_r*int_s,0,int_s,ang_r,2) %--s1 -> s10 mi_selectarcsegment((sin_r*int_s+sin(pi/12-(ang_n/180)*pi)*int_s)/2,(cos_r*int_s+cos(pi/12-(ang_n/180)*pi)*int_s)/2) %--s10 -> s9 mi_deleteselectedarcsegments() mi_selectnode(0,mid_s) %--s2 mi_selectnode(sin_r*int_s,cos_r*int_s) %--s10 mi_deleteselectednodes() mi_createradius(w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2)),0.5e-3) %--s6 %-- mi_addsegment(w_tooth/2+0.1e-3,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2)),w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))-0.1e-3) %-- corner s6 mi_addsegment(sin(pi/12-(ang_n/180)*pi)*(int1_s),cos(pi/12-(ang_n/180)*pi)*(int1_s),sin_r*int1_s,cos_r*int1_s) %-- s8-->s12 %-- mi_clearselected() mi_selectarcsegment(w_tooth/2,sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))) %--s6 mi_deleteselectedarcsegments() %---------------------------------------block stator---------------------------------------- mi_selectsegment((sin(pi/12-(ang_n/180)*pi)*(int1_s)+sin(pi/12-(ang_n/180)*pi)*int_s)/2,(cos(pi/12-(ang_n/180)*pi)*(int1_s)+cos(pi/12-(ang_n/180)*pi)*int_s)/2) %--s8s9 mi_selectsegment((w_tooth/2+sin(pi/12-(ang_n/180)*pi)*(int1_s))/2,(sqrt(mid1_s*mid1_s-(w_tooth/2)*(w_tooth/2))+cos(pi/12-(ang_n/180)*pi)*(int1_s))/2) %--s7s8 mi_selectsegment(w_tooth,(sqrt(mid1_s*mid1_s-(w_tooth/2)*(w_tooth/2))+sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2)))/2) %--s6s7 mi_selectsegment((sin_r*mid_s+w_tooth/2)/2,(cos_r*mid_s+sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2)))/2) %--s6s4 mi_selectsegment((w_tooth/2+0.1e-3+w_tooth/2)/2,(sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))+sqrt(mid_s*mid_s-(w_tooth/2)*(w_tooth/2))-0.1e-3)/2) %--corner s6 mi_selectsegment(((sin(pi/12-(ang_n/180)*pi)*(int1_s))+(sin_r*int1_s))/2,((cos(pi/12-(ang_n/180)*pi)*(int1_s))+(cos_r*int1_s))/2) %--s8s12 mi_setsegmentprop('none', 1, 0, 0, 1110) mi_selectarcsegment((sin(pi/12-(ang_n/180)*pi)*int_s)/2,(cos(pi/12-(ang_n/180)*pi)*int_s+int_s)/2) %--s1s9 mi_setarcsegmentprop(1,'none', 0, 1110) %------------------------------------------------------------------------------------------- mi_clearselected() mi_selectgroup(1110) mi_mirror(0,0,0,ext_s) mi_selectgroup(1110) mi_copyrotate2(0,0,30,11,4) mi_clearselected() %-------------------------------------rotor--------------------------------------------------- %rad_mag=13.5e-3 %-- radiu rotor up to the magnets %diam_mag=27e-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 %-- number of rotor pole pairs ang_s=180/5 sin_s= sin(pi/5) cos_s= cos(pi/5) mi_addnode(0,rad_mag) %--s1 mi_addnode(sin_s*rad_mag,cos_s*rad_mag) %--s3 ang_clear1=clear_s/(rad_mag*pi) ang_clear2=clear_s/(rad_mag*pi) sin_b=sin(ang_clear1) cos_b=cos(ang_clear2) sin_c=sin(pi/5-(clear_s/(rad_rot*pi))) cos_c=cos(pi/5-(clear_s/(rad_rot*pi))) mi_addnode(sin_b*rad_mag,cos_b*rad_mag) %--s5 mi_addnode(sin_c*rad_mag,cos_c*rad_mag) %--s7 mi_addnode(sin_b*rad_rot,cos_b*rad_rot) %--s6 mi_addnode(sin_c*rad_rot,cos_c*rad_rot) %--s8 mi_addarc(sin_s*rad_mag,cos_s*rad_mag,0,rad_mag,36,1) %--s3 -> s1 mi_addarc(sin_c*rad_rot,cos_c*rad_rot,sin_b*rad_rot,cos_b*rad_rot,34.13,1) %--s8 -> s6 mi_addsegment(sin_b*rad_mag,cos_b*rad_mag,sin_b*rad_rot,cos_b*rad_rot) %--s5 -> s6 mi_addsegment(sin_c*rad_mag,cos_c*rad_mag,sin_c*rad_rot,cos_c*rad_rot) %--s7 -> s8 mi_createradius(sin_b*rad_rot,cos_b*rad_rot,0.4e-3) %--s6 mi_createradius(sin_c*rad_rot,cos_c*rad_rot,0.4e-3) %--s8 mi_addsegment(sin_b*rad_rot-0.1e-3,cos_b*rad_rot-0.1e-3,sin_b*rad_rot+0.1e-3,cos_b*rad_rot+0.1e-3) %--s6 corner mi_addsegment(sin_c*rad_rot-0.1e-3,cos_c*rad_rot-0.1e-3,sin_c*rad_rot+0.1e-3,cos_c*rad_rot+0.1e-3) %--s8 corner mi_addsegment(0,rad_mag,sin_b*rad_mag,cos_b*rad_mag) %--s1 -> s5 mi_addsegment(sin_s*rad_mag,cos_s*rad_mag,sin_c*rad_mag,cos_c*rad_mag) %--s3 -> s7 mi_selectarcsegment(sin_b*rad_rot,cos_b*rad_rot) %--s6 mi_selectarcsegment(sin_c*rad_rot,cos_c*rad_rot) %--s8 mi_selectarcsegment(sin_b*rad_mag/2,(rad_mag+cos_b*rad_mag)/2) %--s1 -> s5 mi_selectarcsegment((sin_c*rad_mag+sin_s*rad_mag)/2,(cos_c*rad_mag+cos_s*rad_mag)/2) %--s3 -> s7 mi_deleteselectedarcsegments() mi_clearselected() %-----------------------------------------block 234 -- rotor------------------------------------ mi_selectsegment((sin_b*rad_mag+sin_b*rad_rot)/2,(cos_b*rad_rot+cos_b*rad_mag)/2) %--s5 -> s6 mi_selectsegment((sin_c*rad_mag+sin_c*rad_rot)/2,(cos_c*rad_rot+cos_c*rad_mag)/2) %--s7 -> s8 mi_selectsegment(((sin_b*rad_rot-0.1e-3)+(sin_b*rad_rot+0.1e-3))/2,((cos_b*rad_rot-0.1e-3)+(cos_b*rad_rot+0.1e-3))/2) %-- s6 corner mi_selectsegment(((sin_c*rad_rot-0.1e-3)+(sin_c*rad_rot+0.1e-3))/2,((cos_c*rad_rot-0.1e-3)+(cos_c*rad_rot+0.1e-3))/2) %-- s6 corner mi_selectsegment((sin_b*rad_mag)/2,(rad_mag+cos_b*rad_mag)/2) mi_selectsegment((sin_s*rad_mag+sin_c*rad_mag)/2,(cos_s*rad_mag+cos_c*rad_mag)/2) mi_setsegmentprop('none', 1, 0, 0,234) mi_selectarcsegment(sin_s*rad_mag/2,(cos_s*rad_mag+rad_mag)/2) %--s5 -> s7 mi_selectarcsegment(sin_s*rad_rot/2,(cos_s*rad_rot+rad_rot)/2) %--s6 -> s8 mi_setarcsegmentprop(1,'none', 0,234) %----------------------------------------------------------------------------------------------- mi_selectgroup(234) mi_copyrotate2(0,0,36,9,4) %-------------------------------materials definition-------------------------------------------- %mi_addmaterial('Air',1,1,0,0,0,0,0,1,0,0,0,0,0) %mi_addmaterial('Pure_Iron',1,1,0,0,2.03,0,0,1,0,0,0,0,0) %mi_addmaterial('coil',1,1,0,0,58,0,0,1,3,0,0,1,0.644) %mi_addmaterial('p_mg',1.048,1.048,950000,0,0.667,0,0,1,0,0,0,0,0) %mi_addmaterial('p_mg1',1.048,1.048,950000,0,0.667,0,0,1,0,0,0,0,0) Air = 'Air' %-- materials defined by the "materials.fem" coil = '22 AWG' Pure_Iron = 'M-19' p_mg = 'NdFeB 37 MGOe' p_mg1 = 'NdFeB 37 MGOe' %--Air = 'Air' %--coil = 'coil' %--Pure_Iron = 'Pure_Iron' %--p_mg = 'p_mg' %--p_mg1 = 'p_mg1' %----------------------------------iron rotor--------------------------------------------------- mi_addblocklabel(1e-3,1e-3) mi_selectlabel(1e-3,1e-3) mi_setblockprop(Pure_Iron,0,mrot,0,0,20555,0) mi_clearselected() %---------------------------------iron stator ------------------------------------------------- mi_addblocklabel(0,mid_s) mi_selectlabel(0,mid_s) mi_setblockprop(Pure_Iron,0,mstat,0,0,121,0) mi_clearselected() %----------------------------------air gap------------------------------------------------------ mi_addblocklabel(0,(int_s-0.35e-3)) mi_selectlabel(0,(int_s-0.35e-3)) mi_setblockprop(Air,0,mgap,0,0,216,0) mi_clearselected() %----------------------------------circuits----------------------------------------------------- mi_addcircprop('A',I1,1) mi_addcircprop('B',I2,1) mi_addcircprop('C',I3,1) %----------------------------------COILS from stator------------------------------------------ sin_15=sin(pi/12) cos_15=cos(pi/12) sin_45=sin(pi/4) cos_45=cos(pi/4) sin_75=sin(75*pi/180) cos_75=cos(75*pi/180) sin_105=sin(105*pi/180) cos_105=cos(105*pi/180) sin_135=sin(135*pi/180) cos_135=cos(135*pi/180) sin_165=sin(165*pi/180) cos_165=cos(165*pi/180) sin_195=sin(195*pi/180) cos_195=cos(195*pi/180) sin_225=sin(225*pi/180) cos_225=cos(225*pi/180) sin_255=sin(255*pi/180) cos_255=cos(255*pi/180) sin_285=sin(285*pi/180) cos_285=cos(285*pi/180) sin_315=sin(315*pi/180) cos_315=cos(315*pi/180) sin_345=sin(345*pi/180) cos_345=cos(345*pi/180) rad_l=(mid_s-mid1_s)/2+mid1_s %------------ coils placement degree mi_clearselected() %-----------------------------------------15------------------------------------------------ mi_addblocklabel(sin_15*rad_l,cos_15*rad_l) mi_selectlabel(sin_15*rad_l,cos_15*rad_l) mi_setblockprop(coil,0,q,'A',0,333,56) mi_clearselected() %-----------------------------------------45------------------------------------------------ mi_addblocklabel(sin_45*rad_l,cos_45*rad_l) mi_selectlabel(sin_45*rad_l,cos_45*rad_l) mi_setblockprop(coil,0,q,'B',0,334,-56) mi_clearselected() %-----------------------------------------75------------------------------------------------ mi_addblocklabel(sin_75*rad_l,cos_75*rad_l) mi_selectlabel(sin_75*rad_l,cos_75*rad_l) mi_setblockprop(coil,0,q,'B',0,334,56) mi_clearselected() %-----------------------------------------105------------------------------------------------ mi_addblocklabel(sin_105*rad_l,cos_105*rad_l) mi_selectlabel(sin_105*rad_l,cos_105*rad_l) mi_setblockprop(coil,0,q,'C',0,335,-56) mi_clearselected() %-----------------------------------------135------------------------------------------------ mi_addblocklabel(sin_135*rad_l,cos_135*rad_l) mi_selectlabel(sin_135*rad_l,cos_135*rad_l) mi_setblockprop(coil,0,q,'C',0,335,56) mi_clearselected() %-----------------------------------------165------------------------------------------------ mi_addblocklabel(sin_165*rad_l,cos_165*rad_l) mi_selectlabel(sin_165*rad_l,cos_165*rad_l) mi_setblockprop(coil,0,q,'A',0,333,56) mi_clearselected() %-----------------------------------------195------------------------------------------------ mi_addblocklabel(sin_195*rad_l,cos_195*rad_l) mi_selectlabel(sin_195*rad_l,cos_195*rad_l) mi_setblockprop(coil,0,q,'A',0,333,-56) mi_clearselected() %-----------------------------------------225------------------------------------------------ mi_addblocklabel(sin_225*rad_l,cos_225*rad_l) mi_selectlabel(sin_225*rad_l,cos_225*rad_l) mi_setblockprop(coil,0,q,'B',0,334,56) mi_clearselected() %-----------------------------------------255------------------------------------------------ mi_addblocklabel(sin_255*rad_l,cos_255*rad_l) mi_selectlabel(sin_255*rad_l,cos_255*rad_l) mi_setblockprop(coil,0,q,'B',0,334,-56) mi_clearselected() %-----------------------------------------285------------------------------------------------ mi_addblocklabel(sin_285*rad_l,cos_285*rad_l) mi_selectlabel(sin_285*rad_l,cos_285*rad_l) mi_setblockprop(coil,0,q,'C',0,335,56) mi_clearselected() %-----------------------------------------315------------------------------------------------ mi_addblocklabel(sin_315*rad_l,cos_315*rad_l) mi_selectlabel(sin_315*rad_l,cos_315*rad_l) mi_setblockprop(coil,0,q,'C',0,335,-56) mi_clearselected() %-----------------------------------------345------------------------------------------------ mi_addblocklabel(sin_345*rad_l,cos_345*rad_l) mi_selectlabel(sin_345*rad_l,cos_345*rad_l) mi_setblockprop(coil,0,q,'A',0,333,-56) mi_clearselected() %------------------------------- PM from rotor------------------------------------- --------- ang_h=180/10 sin_h= sin(pi/10) cos_h= cos(pi/10) mi_addblocklabel(sin_s*rad_rot/2,rad_mag*cos_h+h_mag/2) mi_selectlabel(sin_s*rad_rot/2,rad_mag*cos_h+h_mag/2) mi_setblockprop(p_mg,0,mmag,0,78,20555,0) mi_copyrotate2(0,0,72,4,2) mi_addblocklabel(-sin_s*rad_rot/2,rad_mag*cos_h+h_mag/2) mi_selectlabel(-sin_s*rad_rot/2,rad_mag*cos_h+h_mag/2) mi_setblockprop(p_mg1,0,mmag,0,288,20555,0) mi_copyrotate2(0,0,72,4,2) mi_clearselected() %----------------------------------------bounds---------------------------------------------- mi_addsegment(0,ext_s,sin_5*ext_s,cos_5*ext_s) %--s3 --> s55 mi_selectsegment((sin_5*ext_s)/2,((cos_5*ext_s)+ext_s)/2) %--s3 --> s55 mi_setsegmentprop('abc',1,1,0,'1223') mi_addboundprop('abc',0,0,0,0,0,0,0,0,0) mi_selectgroup(1223) mi_copyrotate(0,0,5,71) mi_clearselected() %----------------------------------------for rotate----------------------------------------- %--for k=0, 360, 2 %--do %--mi_selectgroup(234) %--mi_selectgroup(20555) %--mi_moverotate(0,0,2,4) %--mi_saveas('motor.fem') %--mi_analyze() %--mi_loadsolution() %--mi_zoomnatural() %--mo_showdensityplot(1,0,2.6e-5,1.9,'bimag') %--mo_hidepoints() %--alfa=k %--mo_savebitmap(format('acgen_%1$d.bmp',alfa)) %--end %-----------------------------------------end---------------------------------------------- mi_saveas('motor.fem') mi_zoomnatural() mi_createmesh() mi_showmesh() mi_zoomnatural() mi_analyze() mi_loadsolution() %mi_zoomnatural() %mo_showdensityplot(1,0,2.6e-5,1.9,'bimag') %mo_hidepoints() %--**************************************************************************************__ mi_clearselected() mi_selectgroup(234) mi_selectgroup(20555) mi_moverotate(0,0,theta) mi_clearselected() %mi_analyze(1) %mi_loadsolution() %mo_showdensityplot(1,0,4e-5,1.9,'bimag') %mo_hidepoints() %mi_zoomnatural() %--------------------------------flux linkage------------------------------- circpA= mo_getcircuitproperties('A') fluxA=circpA(1,3) circpB= mo_getcircuitproperties('B') fluxB=circpB(1,3) circpC= mo_getcircuitproperties('C') fluxC=circpC(1,3) save('flux_linkage','fluxA','fluxB','fluxC') %--------------------------------torque------------------------------- %mo_groupselectblock(20555) %torque = mo_blockintegral(22) %-- Steady-state weighted stress tensor torque %save('torque','torque') %mo_clearblock() %--------------------------------spinning rotor------------------------------- mi_selectgroup(234) mi_selectgroup(20555) mi_moverotate(0,0,-theta) mi_clearselected() %--------------------------------image save----------------------------------- %--mi_zoomnatural() %--mo_showdensityplot(1,0,4e-5,1.9,'bimag') %--mo_savebitmap(format('acgen_%1$d.bmp',theta)) mo_groupselectblock(20555) T=mo_blockintegral(22); %fplot(@(PMSM_m_mod)[T],[-25.70 25.70]) %save('dates') %save('myfile','T',) %%%%%%%%%%%%%%this is the vector that stores the value but it is rewritten after the next iteration%%%%%%%%%%% ________problem is that I do not Know the value of MaxFunEvals__________ %a=[] %for i=1:1:MaxFunEvals % for j=1:1:MaxFunEvals % a(i,j)=fval % end %end closefemm() %--------------------------------block outputs-------------------------------------- %--FluxA, FluxB, FluxC, torque end _____________________THIS IS THE FUNCTION_____________________________ clear rad_mag=13.5e-3 %-- radiu rotor up to the magnets diam_mag=2*rad_mag %--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',10,'MaxFunEvals',3) [M,fval,exitflag,output]=fminsearch(@(rad_mag) PMSM_m_mod(rad_mag,clear_s,diam_mag,diam_rot,rad_ax,rad_rot,h_mag,p),rad_mag,options); clear
|
Pages: 1 Prev: Error while evaluating uicontrol Callback Next: Mixed variable optimization |