<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Solving an equation , non linear in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663863#M3149</link>
    <description>Thank you !!!</description>
    <pubDate>Sun, 21 Jun 2020 18:24:17 GMT</pubDate>
    <dc:creator>Thomas_mp</dc:creator>
    <dc:date>2020-06-21T18:24:17Z</dc:date>
    <item>
      <title>Solving an equation , non linear</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663753#M3147</link>
      <description>&lt;P&gt;Good morning,&lt;/P&gt;&lt;P&gt;I am trying to solve an equation of the type&amp;nbsp;&amp;nbsp; a1 = c1+&amp;nbsp; (b1-X)*c1/(1+X)&amp;nbsp; + (b2-X)*c2/(1+X)^2 + (b3-X)*c3/(1+X)^3 +… (bt-X)*ct/X*(1/(1+X)^t-1)&lt;/P&gt;&lt;P&gt;Note that the first (t-1) terms are of the same form (bj-Xj)*cj/(1+X)^j-1 &amp;nbsp;, but the last term is different.&lt;/P&gt;&lt;P&gt;I need to find the value of X ( 0&amp;lt;X&amp;lt;1).&amp;nbsp; In the case of multiple solutions, chose the smaller non-negative solution.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my case I have 25 variables.&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;ai&lt;/LI&gt;&lt;LI&gt;12 bi&lt;/LI&gt;&lt;LI&gt;12 ci&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data looks like:&lt;/P&gt;&lt;P&gt;Input &amp;nbsp;&amp;nbsp;a&amp;nbsp;&amp;nbsp;&amp;nbsp; b1&amp;nbsp;&amp;nbsp;&amp;nbsp; b2&amp;nbsp;&amp;nbsp;&amp;nbsp; b3&amp;nbsp;&amp;nbsp;&amp;nbsp; b4&amp;nbsp;&amp;nbsp;&amp;nbsp; b5&amp;nbsp;&amp;nbsp;&amp;nbsp; b6&amp;nbsp;&amp;nbsp;&amp;nbsp; b7&amp;nbsp;&amp;nbsp;&amp;nbsp; b8&amp;nbsp;&amp;nbsp;&amp;nbsp; b9&amp;nbsp;&amp;nbsp;&amp;nbsp; b10&amp;nbsp; b11&amp;nbsp;&amp;nbsp;&amp;nbsp; b12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c1&amp;nbsp;&amp;nbsp;&amp;nbsp; c2&amp;nbsp;&amp;nbsp;&amp;nbsp; c3&amp;nbsp;&amp;nbsp;&amp;nbsp; c4&amp;nbsp;&amp;nbsp;&amp;nbsp; c5&amp;nbsp;&amp;nbsp;&amp;nbsp; c6&amp;nbsp;&amp;nbsp;&amp;nbsp; c7&amp;nbsp;&amp;nbsp;&amp;nbsp; c8&amp;nbsp;&amp;nbsp;&amp;nbsp; c9&amp;nbsp;&amp;nbsp;&amp;nbsp; c10&amp;nbsp; c11&amp;nbsp; &amp;nbsp; c12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;cards&lt;/P&gt;&lt;P&gt;84.3&amp;nbsp; 0.33&amp;nbsp; 0.31&amp;nbsp; 0.29&amp;nbsp; 0.28&amp;nbsp; 0.26&amp;nbsp; 0.25&amp;nbsp; 0.24&amp;nbsp; 0.22&amp;nbsp; 0.21&amp;nbsp; 0.19&amp;nbsp; 0.18&amp;nbsp; 0.17&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;11.1&amp;nbsp; 13.4&amp;nbsp; 16.1&amp;nbsp; 19.1&amp;nbsp; 22.4&amp;nbsp; 26.2&amp;nbsp; 30.4&amp;nbsp; 34.9&amp;nbsp; 39.8&amp;nbsp; 45.1&amp;nbsp; 50.7&amp;nbsp; 56.5&lt;/P&gt;&lt;P&gt;91.5&amp;nbsp; 0.332&amp;nbsp; 0.310&amp;nbsp; 0.291&amp;nbsp; 0.277&amp;nbsp; 0.263&amp;nbsp; 0.249&amp;nbsp; 0.235&amp;nbsp; 0.221&amp;nbsp; 0.207&amp;nbsp; 0.194&amp;nbsp; 0.180&amp;nbsp; 0.166&amp;nbsp; 11.076&amp;nbsp; 13.422&amp;nbsp; 16.074&amp;nbsp; 19.061&amp;nbsp; 22.433&amp;nbsp; 26.201&amp;nbsp; 30.368&amp;nbsp; 34.927&amp;nbsp; 39.859&amp;nbsp; 45.132&amp;nbsp; 50.700&amp;nbsp; 56.502&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can, by trial and error, find the value of X, this is: solve the equation&amp;nbsp; in Excel&lt;/P&gt;&lt;P&gt;84.3=&amp;nbsp; 11.1 +(0.33-X)*11.1/(1+X) +&amp;nbsp;&amp;nbsp;(0.31-X)*13.4/(1+X)^2 +&amp;nbsp;(0.29-X)*16.1/(1+X)^3+&amp;nbsp;(0.28-X)*19.1/(1+X)^4 + .............&amp;nbsp; +&amp;nbsp;(0.18-X)*50.7/(1+X)^11 +&amp;nbsp;&amp;nbsp;(0.17-X)*56.5/X*[1/(1+X)^11]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this case the solution is X=0.068, this X produces a value for&amp;nbsp; a=84.3.&lt;/P&gt;&lt;P&gt;Doing the same, the solution for the second row of data (which starts with a value of a=91.5) is X = 0.0638&lt;/P&gt;&lt;P&gt;But I have several thousands of observations …&amp;nbsp; so I do not how to do this in Excel by trial and error (function IRR does not work in excel because X is both in the numerator and in the denominator).&lt;/P&gt;&lt;P&gt;I am attaching the excel file with the data and the solution.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you !!!&lt;/P&gt;&lt;P&gt;Tomas&lt;/P&gt;</description>
      <pubDate>Sat, 20 Jun 2020 20:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663753#M3147</guid>
      <dc:creator>Thomas_mp</dc:creator>
      <dc:date>2020-06-20T20:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: Solving an equation , non linear</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663793#M3148</link>
      <description>&lt;P&gt;Here's one way to do it with PROC OPTMODEL by using a DO loop to call the solver once per observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n = 12;
data indata;
   input a b1-b&amp;amp;n c1-c&amp;amp;n @@;
   datalines;
84.3 0.33  0.31  0.29  0.28  0.26  0.25  0.24  0.22  0.21  0.19  0.18  0.17  
     11.1  13.4  16.1  19.1  22.4  26.2  30.4  34.9  39.8  45.1  50.7  56.5
91.5 0.332 0.310 0.291 0.277 0.263 0.249 0.235 0.221 0.207 0.194 0.180 0.166 
     11.076 13.422 16.074 19.061 22.433 26.201 30.368 34.927 39.859 45.132 50.700 56.502
;

proc optmodel printlevel=0;
   num n = &amp;amp;n;
   set OBS;
   num a {OBS};
   num b {OBS, 1..n};
   num c {OBS, 1..n};
   read data indata into OBS=[_N_]
      a {j in 1..n} &amp;lt;b[_N_,j]=col('b'||j)&amp;gt; {j in 1..n} &amp;lt;c[_N_,j]=col('c'||j)&amp;gt;;

   var X &amp;gt;= 0 &amp;lt;= 1;
   min Z = X;
   num obsThis, aThis, bThis {1..n}, cThis {1..n};
   con IRR:
      aThis = cThis[1] + sum {j in 1..n-1} (bThis[j]-X)*cThis[j]/(1+X)^j + (bThis[n]-X)*cThis[n]/X/(1+X)^(n-1);

   num Xsol {OBS};
   do obsThis = OBS;
      put obsThis=;
      aThis = a[obsThis];
      for {j in 1..n} do;
         bThis[j] = b[obsThis,j];
         cThis[j] = c[obsThis,j];
      end;
      solve;
      Xsol[obsThis] = X.sol;
   end;
   create data outdata from [i] X=Xsol;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use a COFOR loop to solve these independent problems concurrently.&amp;nbsp; Just replace the DO statement with these two statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   cofor {i in OBS} do;
      obsThis = i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SAS Viya 3.5, it is even simpler (no explicit looping) by using the groupBy option in the runOptmodel action:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sascas1.indata;
   set indata;
   i = _N_;
run;

proc cas noqueue;
   source pgm;
      put _BY_LINE_;

      num n = &amp;amp;n;
      num a;
      num b {1..n};
      num c {1..n};
      read data indata into
         a {j in 1..n} &amp;lt;b[j]=col('b'||j)&amp;gt; {j in 1..n} &amp;lt;c[j]=col('c'||j)&amp;gt;;

      var X &amp;gt;= 0 &amp;lt;= 1;
      min Z = X;
      con IRR:
         a = c[1] + sum {j in 1..n-1} (b[j]-X)*c[j]/(1+X)^j + (b[n]-X)*c[n]/X/(1+X)^(n-1);

      solve;
      create data outdata from X;
   endsource;
   
   action optimization.runOptmodel / code=pgm printlevel=0 groupBy={"i"};
   run;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 14:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663793#M3148</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2020-06-23T14:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: Solving an equation , non linear</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663863#M3149</link>
      <description>Thank you !!!</description>
      <pubDate>Sun, 21 Jun 2020 18:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/663863#M3149</guid>
      <dc:creator>Thomas_mp</dc:creator>
      <dc:date>2020-06-21T18:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Solving an equation , non linear</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/664134#M3156</link>
      <description>Robert, Your code using proc optmodel works. Thank you. I am trying to merge the solutions for X (in outdata) with my original sample (data - indata). I can create a counting variable n_ +1 in indata.&lt;BR /&gt;How can I keep this variable n_ in outdata so I can merge outdata and indata by n_ ?&lt;BR /&gt;&lt;BR /&gt;Thank you again&lt;BR /&gt;Tomas&lt;BR /&gt;</description>
      <pubDate>Tue, 23 Jun 2020 04:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/664134#M3156</guid>
      <dc:creator>Thomas_mp</dc:creator>
      <dc:date>2020-06-23T04:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: Solving an equation , non linear</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/664296#M3157</link>
      <description>&lt;P&gt;No need to merge.&amp;nbsp; Just replace the READ DATA statement with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;read data indata into OBS=[n_]
   a {j in 1..n} &amp;lt;b[n_,j]=col('b'||j)&amp;gt; {j in 1..n} &amp;lt;c[n_,j]=col('c'||j)&amp;gt;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then replace the CREATE DATA statement with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create data outdata from [n_] X=Xsol
   a {j in 1..n} &amp;lt;col('b'||j)=b[n_,j]&amp;gt; {j in 1..n} &amp;lt;col('c'||j)=c[n_,j]&amp;gt;;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 14:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/664296#M3157</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2020-06-23T14:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: Solving an equation , non linear</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/665224#M3170</link>
      <description>Thank you again !!!</description>
      <pubDate>Fri, 26 Jun 2020 02:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Solving-an-equation-non-linear/m-p/665224#M3170</guid>
      <dc:creator>Thomas_mp</dc:creator>
      <dc:date>2020-06-26T02:40:03Z</dc:date>
    </item>
  </channel>
</rss>

