Store all iterations of a symbolic variable as double, numeric, etc. while using fmincon optimization to visualize post-run

5 views (last 30 days)
I am using fmincon to optimize a two-reservoir system. Following the example syntax, I am using symbolic variables for reservoir storage, inflow, and releases. However, following optimization, I would like to plot and visualize the reservoir storage. I have tried cell2mat, double() and other ways of converting the symbolic variables to a format that can be used to see all iterations, but get an error each time.
What is the best way to have all iterations of each variable stored during optimization using fmincon, and what syntax should I use to convert them out of symbolic and see the values?
Thanks.

Answers (1)

Walter Roberson
Walter Roberson on 14 Apr 2018
If double() fails on them, then they very likely still have symbolic variables in them, and cannot be directly plotted. To plot something that contains a symbolic variable you would need to substitute definite values for the symbolic variables. fplot() can do that for you if there is only one variable, and fsurf() can do that for 2 variables (producing a surface.)
  6 Comments
Sarah
Sarah on 16 Apr 2018
Edited: Walter Roberson on 17 Apr 2018

Thank you for your patience and help.

class(S{1}) returns 'sym' and cell2mat(cellfun(@double, storage, 'uniform', 0)) returns the error:

Error using symengine
DOUBLE cannot convert the input expression into a double array.
Error in sym/double (line 643)
        Xstr = mupadmex('symobj::double', S.s, 0);

Using celldisp(S), an example entry is as follows (and I've tried to attach it):

S{1} =
S0
S{2} =
S0 + c3*(QF1 - X241)
S{3} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242)
S{4} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243)
S{5} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244)
S{6} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244) + c3*(QF5 - X245)
S{7} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244) + c3*(QF5 - X245) + c3*(QF6 - X246)
S{8} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244) + c3*(QF5 - X245) + c3*(QF6 - X246) + c3*(QF7 - X247)
S{9} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244) + c3*(QF5 - X245) + c3*(QF6 - X246) + c3*(QF7 - X247) + c3*(QF8 - X248)
S{10} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244) + c3*(QF5 - X245) + c3*(QF6 - X246) + c3*(QF7 - X247) + c3*(QF8 - X248) + c3*(QF9 - X249)
... 
S{120} =
S0 + c3*(QF1 - X241) + c3*(QF2 - X242) + c3*(QF3 - X243) + c3*(QF4 - X244) + c3*(QF5 - X245) + c3*(QF6 - X246) + c3*(QF7 - X247) + c3*(QF8 - X248) + c3*(QF9 - X249) + c3*(QF10 - X250) + c3*(QF11 - X251) + c3*(QF12 - X252) + c3*(QF13 - X253) + c3*(QF14 - X254) + c3*(QF15 - X255) + c3*(QF16 - X256) + c3*(QF17 - X257) + c3*(QF18 - X258) + c3*(QF19 - X259) + c3*(QF20 - X260) + c3*(QF21 - X261) + c3*(QF22 - X262) + c3*(QF23 - X263) + c3*(QF24 - X264) + c3*(QF25 - X265) + c3*(QF26 - X266) + c3*(QF27 - X267) + c3*(QF28 - X268) + c3*(QF29 - X269) + c3*(QF30 - X270) + c3*(QF31 - X271) + c3*(QF32 - X272) + c3*(QF33 - X273) + c3*(QF34 - X274) + c3*(QF35 - X275) + c3*(QF36 - X276) + c3*(QF37 - X277) + c3*(QF38 - X278) + c3*(QF39 - X279) + c3*(QF40 - X280) + c3*(QF41 - X281) + c3*(QF42 - X282) + c3*(QF43 - X283) + c3*(QF44 - X284) + c3*(QF45 - X285) + c3*(QF46 - X286) + c3*(QF47 - X287) + c3*(QF48 - X288) + c3*(QF49 - X289) + c3*(QF50 - X290) + c3*(QF51 - X291) + c3*(QF52 - X292) + c3*(QF53 - X293\\\n) + c3*(QF54 - X294) + c3*(QF55 - X295) + c3*(QF56 - X296) + c3*(QF57 - X297) + c3*(QF58 - X298) + c3*(QF59 - X299) + c3*(QF60 - X300) + c3*(QF61 - X301) + c3*(QF62 - X302) + c3*(QF63 - X303) + c3*(QF64 - X304) + c3*(QF65 - X305) + c3*(QF66 - X306) + c3*(QF67 - X307) + c3*(QF68 - X308) + c3*(QF69 - X309) + c3*(QF70 - X310) + c3*(QF71 - X311) + c3*(QF72 - X312) + c3*(QF73 - X313) + c3*(QF74 - X314) + c3*(QF75 - X315) + c3*(QF76 - X316) + c3*(QF77 - X317) + c3*(QF78 - X318) + c3*(QF79 - X319) + c3*(QF80 - X320) + c3*(QF81 - X321) + c3*(QF82 - X322) + c3*(QF83 - X323) + c3*(QF84 - X324) + c3*(QF85 - X325) + c3*(QF86 - X326) + c3*(QF87 - X327) + c3*(QF88 - X328) + c3*(QF89 - X329) + c3*(QF90 - X330) + c3*(QF91 - X331) + c3*(QF92 - X332) + c3*(QF93 - X333) + c3*(QF94 - X334) + c3*(QF95 - X335) + c3*(QF96 - X336) + c3*(QF97 - X337) + c3*(QF98 - X338) + c3*(QF99 - X339) + c3*(QF100 - X340) + c3*(QF101 - X341) + c3*(QF102 - X342) + c3*(QF103 - X343) + c3*(QF104 - X344) + c3*(QF105 - X345) + c\\\n3*(QF106 - X346) + c3*(QF107 - X347) + c3*(QF108 - X348) + c3*(QF109 - X349) + c3*(QF110 - X350) + c3*(QF111 - X351) + c3*(QF112 - X352) + c3*(QF113 - X353) + c3*(QF114 - X354) + c3*(QF115 - X355) + c3*(QF116 - X356) + c3*(QF117 - X357) + c3*(QF118 - X358) + c3*(QF119 - X359)
Walter Roberson
Walter Roberson on 17 Apr 2018
You could probably get all of those into one single symbolic vector, but you are still faced with some difficulties:
  • What you are looking at there with all those equations is the value of those variables. There is no hidden numeric value
  • The only way to plot those is to substitute specific numeric values for the symbolic variables. That would likely be a problem for you as it appears there are at least a few dozen symbolic variables involved and it is seldom practical to make a plot with more than 3 (at most 4) independent variables simultaneously.
When I look at those expressions, it looks to me as if you are building up an equation in a loop. I wonder if you need to store all of the iterations, or if you only need the final one?
It seems unlikely that you should be doing that computation inside an objective function. Instead likely you should be computing the equation once outside of fmincon and then using matlabFunction() to optimize over it. You would likely use the 'vars' parameter of matlabFunction to bundle a number of variables into a vector.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!