<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161946</link>
    <title>MATLAB Central Newsreader - code to solve a linear equity - Need help</title>
    <description>Feed for thread: code to solve a linear equity - Need help</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 12 Jan 2008 21:49:02 -0500</pubDate>
      <title>code to solve a linear equity - Need help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161946#409151</link>
      <author>leon Galushko</author>
      <description>Hi,&lt;br&gt;
i have had previosly adressed this question titled with "Lingo..." Now there are more MatLab experts which have good experience in MaTLab, so i rephrase the problem once more. The task is simply to solve the equity where&lt;br&gt;
could be n amount of variables, like:&lt;br&gt;
a1 * x1 + a2 * x2 + a3 * x3 = Y;&lt;br&gt;
a1, a2 and a3 are given, the Y too; now is to find all&lt;br&gt;
possible values which x1, x2 and x3 could take on to solve the equity; one constraint is, that x1,2, 3 have to be only integer, that is whole numbers. I will let the programm run till it had worked out all possible solving ways that could be for the given equation. I need all solving possibilities be outputed.&lt;br&gt;
&lt;br&gt;
I tried in MatLab this code:&lt;br&gt;
target_result = .....&lt;br&gt;
% Iterations boundaries:&lt;br&gt;
x1_start = 1;&lt;br&gt;
x1_stop = 10000;&lt;br&gt;
x2_start = 1;&lt;br&gt;
x2_stop = 10000;&lt;br&gt;
result = []; % initialize empy Array  &lt;br&gt;
for i = x1_start:x1_stop&lt;br&gt;
&amp;gt;      for j = x2_start:x2_stop&lt;br&gt;
&amp;gt;          result = a * i + b * j;&lt;br&gt;
&amp;gt;          if result == target_result&lt;br&gt;
&amp;gt;              number_1 = i&lt;br&gt;
&amp;gt;              number_1 = j&lt;br&gt;
Resutls store in a matrix&lt;br&gt;
&amp;gt;              result = [result; number_1 number_1];&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;This do indeed, BUT the really problem is one cannot more than 2 or 3 variables, in case equation is longer, like that:&lt;br&gt;
x1 * a1 + x2 * a2 + ..........x15 * a15 = Y;&lt;br&gt;
&lt;br&gt;
There are then more than 15 loops, which MatLab not up to run in a time. &lt;br&gt;
Is there another way to make a better code for this model or this task?&lt;br&gt;
Thanks&lt;br&gt;
Leon&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sun, 13 Jan 2008 01:00:19 -0500</pubDate>
      <title>Re: code to solve a linear equity - Need help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161946#409155</link>
      <author>Roger Stafford</author>
      <description>leon Galushko &amp;lt;leonid.galushko@rwth-aachen.de&amp;gt; wrote in message &lt;br&gt;
&amp;lt;16745921.1200174572444.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; i have had previosly adressed this question titled with "Lingo..." Now there &lt;br&gt;
are more MatLab experts which have good experience in MaTLab, so i &lt;br&gt;
rephrase the problem once more. The task is simply to solve the equity where&lt;br&gt;
&amp;gt; could be n amount of variables, like:&lt;br&gt;
&amp;gt; a1 * x1 + a2 * x2 + a3 * x3 = Y;&lt;br&gt;
&amp;gt; a1, a2 and a3 are given, the Y too; now is to find all&lt;br&gt;
&amp;gt; possible values which x1, x2 and x3 could take on to solve the equity; one &lt;br&gt;
constraint is, that x1,2, 3 have to be only integer, that is whole numbers. I &lt;br&gt;
will let the programm run till it had worked out all possible solving ways that &lt;br&gt;
could be for the given equation. I need all solving possibilities be outputed.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I tried in MatLab this code:&lt;br&gt;
&amp;gt; target_result = .....&lt;br&gt;
&amp;gt; % Iterations boundaries:&lt;br&gt;
&amp;gt; x1_start = 1;&lt;br&gt;
&amp;gt; x1_stop = 10000;&lt;br&gt;
&amp;gt; x2_start = 1;&lt;br&gt;
&amp;gt; x2_stop = 10000;&lt;br&gt;
&amp;gt; result = []; % initialize empy Array  &lt;br&gt;
&amp;gt; for i = x1_start:x1_stop&lt;br&gt;
&amp;gt; &amp;gt;      for j = x2_start:x2_stop&lt;br&gt;
&amp;gt; &amp;gt;          result = a * i + b * j;&lt;br&gt;
&amp;gt; &amp;gt;          if result == target_result&lt;br&gt;
&amp;gt; &amp;gt;              number_1 = i&lt;br&gt;
&amp;gt; &amp;gt;              number_1 = j&lt;br&gt;
&amp;gt; Resutls store in a matrix&lt;br&gt;
&amp;gt; &amp;gt;              result = [result; number_1 number_1];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;    This do indeed, BUT the really problem is one cannot more than 2 or 3 &lt;br&gt;
variables, in case equation is longer, like that:&lt;br&gt;
&amp;gt; x1 * a1 + x2 * a2 + ..........x15 * a15 = Y;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There are then more than 15 loops, which MatLab not up to run in a time. &lt;br&gt;
&amp;gt; Is there another way to make a better code for this model or this task?&lt;br&gt;
&amp;gt; Thanks&lt;br&gt;
&amp;gt; Leon&lt;br&gt;
--------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Quite aside from excessive execution time, I foresee accuracy difficulties for &lt;br&gt;
you in your procedure.  You are requiring exact equality when you say:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;if result == target_result,&lt;br&gt;
&lt;br&gt;
but unless Y and the coefficients a1, a2, etc. are all fractions with &lt;br&gt;
denominators only powers of two, there will occur inevitable roundoff errors &lt;br&gt;
as the 'result' is computed and you may not achieve exact equality, even &lt;br&gt;
though you may have a valid solution.  In this case you need to determine the &lt;br&gt;
largest roundoff errors that could possibly develop and tolerate enough &lt;br&gt;
divergence from exact equality to allow such near solutions to be listed.  That &lt;br&gt;
is, you should say&lt;br&gt;
&lt;br&gt;
&amp;nbsp;if abs(result-target_result) &amp;lt; tolerance&lt;br&gt;
&lt;br&gt;
for some appropriately small value of 'tolerance'.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;As to the time of execution, you should be aware that with 15 variables, &lt;br&gt;
each ranging from 1 to 10000, you have a total of 10^60 combinations to &lt;br&gt;
check, which is far too many for your computer to ever finish computing.  &lt;br&gt;
Optimistically assume one nanosecond for each summation.  Then 10^60 of &lt;br&gt;
them would require some 3e+43 years to complete!  The universe will have &lt;br&gt;
long since suffered the heat death predicted for it by astronomers and &lt;br&gt;
physicists.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;If the a-coefficients and Y are restricted to fractions with only comparatively &lt;br&gt;
small denominators, there are some shortcuts I can conceive of, but I fear the &lt;br&gt;
execution time for 15 terms would still be far out of reach for practical &lt;br&gt;
computation, even if you had all the earth's computers devoted in parallel to &lt;br&gt;
the project.  And even if you succeeded in this, what would you do with the &lt;br&gt;
prodigious quantities of result listings?&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
