The opf_model object (om) is created by opf_setup() which is called automatically when you execute runopf(). In order to modify it directly using om.add_quad_cost(), you will need to define a callback function as described in Chapter 7 of the manual. For a simple addition like this, it may be easier to just use the legacy cost mechanism described in Section 6.3.3. In that case, you can do it by simply adding some parameters to mpc. For example, define the mpc.N matrix to select the voltage magnitudes from the optimization vector x (i.e. the columns from nb+1 to 2*nb), and mpc.Cw equal to your Cw.
Hope this helps, Ray On Mar 12, 2020, at 2:46 PM, Ali Mohammadi <[email protected]<mailto:[email protected]>> wrote: Hi, I want to add a cost function in my objective function. I wrote the code as follows but I got error please correct me, If my code is wrong (this is just the code example). case_name='9'; mpc=loadcase(['case',case_name]); om=opt_model(mpc); % test = loadcase('t_case30_userfcns') % res=runopf(mpc); mpopt = mpoption('pf.alg', 'FDXB', 'verbose', 2, 'out.all', 0); om = opf_setup(mpc,mpopt); % mpc = toggle_reserves(mpc, 'on') " OutmailID: 124463678, List: 'matpower-l', MemberID: 82861091 SCRIPT: "% Cw=zeros(9,1); Q=zeros(9,1); Cw([5])=1; om.add_quad_cost('myQcost', Q, Cw, 0, {'Vm'});" TCL MERGE ERROR ( 03/16/2020 12:45:10 ): "extra characters after close-brace% mpc = add_userfcn(mpc, 'formulation', om); runopf(mpc) Thank you so much Ali
