COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### solve strangeness

• Email
• Follow

```I'm sure I must be missing the obvious, but ... below the same
calculation is done twice, only the symbol names changing. Yet
the results from the 2 runs differ: ds= -7.8125 whereas the
equivalent BEs= -16.2500, and cs= -16.2500 whereas CD= -7.8125
Apols in advance if it's all because of a typo.
--------

clear all
syms AD BD CD BE CE DE real
eqn(3)=-3*CD+3*BD;
eqn(4)=-8*BE-10*DE-8*CE;
eqn(5)=4*DE-50;
eqn(6)=3*BE-3*CE;

BDs=eval(BDs)
CDs=eval(CDs)
BEs=eval(BEs)
CEs=eval(CEs)
DEs=eval(DEs)

check=subs(eqn(3),{CD,BD},{CDs,BDs})

% the same, but with different variable names
clear all
syms a b c d e f real
eqn(1)=-2*a+2*c+2*b+10*f-20;
eqn(2)=-4*a-4*c-4*b-4*f;
eqn(3)=-3*c+3*b;
eqn(4)=-8*d-10*f-8*e;
eqn(5)=4*f-50;
eqn(6)=3*d-3*e;

[as,bs,cs,ds,es,fs]=solve(eqn(1),eqn(2),eqn(3),eqn(4),eqn(5),eqn(6),a,b,c,d,e,f);

as=eval(as)
bs=eval(bs)
cs=eval(cs)
ds=eval(ds)
es=eval(es)
fs=eval(fs)

check=subs(eqn(3),{c,b},{cs,bs})
```
 0
Reply tpl (606) 1/28/2004 1:08:29 PM

See related articles to this posting

```In article <bv8c8d\$fs2\$1@pegasus.csx.cam.ac.uk>,
Tim Love <tpl@eng.cam.ac.uk> wrote:
>I'm sure I must be missing the obvious, but ... below the same
>calculation is done twice, only the symbol names changing. Yet
>the results from the 2 runs differ: ds= -7.8125 whereas the
>equivalent BEs= -16.2500, and cs= -16.2500 whereas CD= -7.8125
>Apols in advance if it's all because of a typo.
>--------
>
>
>
>
>clear all
>syms AD BD CD BE CE DE real
>eqn(3)=-3*CD+3*BD;
>eqn(4)=-8*BE-10*DE-8*CE;
>eqn(5)=4*DE-50;
>eqn(6)=3*BE-3*CE;
>
>
>BDs=eval(BDs)
>CDs=eval(CDs)
>BEs=eval(BEs)
>CEs=eval(CEs)
>DEs=eval(DEs)
>
>check=subs(eqn(3),{CD,BD},{CDs,BDs})
>
>% the same, but with different variable names
>clear all
>syms a b c d e f real
>eqn(1)=-2*a+2*c+2*b+10*f-20;
>eqn(2)=-4*a-4*c-4*b-4*f;
>eqn(3)=-3*c+3*b;
>eqn(4)=-8*d-10*f-8*e;
>eqn(5)=4*f-50;
>eqn(6)=3*d-3*e;
>
>[as,bs,cs,ds,es,fs]=solve(eqn(1),eqn(2),eqn(3),eqn(4),eqn(5),eqn(6),a,b,c,d,e,f);
>
>as=eval(as)
>bs=eval(bs)
>cs=eval(cs)
>ds=eval(ds)
>es=eval(es)
>fs=eval(fs)
>
>check=subs(eqn(3),{c,b},{cs,bs})

Hi --

Did you notice that your second set of solutions is just a reordering
of the first set?  In your first use of solve, MATLAB and solve can
not tell that you want AD stored in ADs, BD stored in BDs, etc.  If
you read the help information for "solve" carefully, you will see that
it sorts the results in lexicographic (i.e. alphabetical) order.
Since the list [AD,BD,CD,BE,CE,DE] is not in alphabetical order, the
results get reordered.  Your second list [a,b,c,d,e,f] is in
alphabetical order.

Your equations are linear.  You could use matrix notation.

A = [  -2   2   2   0   0  10
-4  -4  -4   0   0  -4
0   3  -3   0   0   0
0   0   0  -8  -8 -10
0   0   0   0   0   4
0   0   0   3  -3   0 ]

b = [20 0 0 0 50 0]'

format rat

x = A\b

20
-65/4
-65/4
-125/16
-125/16
25/2

-- Cleve
moler@mathworks.com
```
 0
Reply moler (96) 1/28/2004 3:52:06 PM

```Hi,

If I remember correctly, you're best choice when calling SOLVE is to
just use a single output, even if you're solving for many variables.
SOLVE will output a structure with fieldnames that correspond to the
variables solved for. And because SOLVE populates this structure,
you won't run into issues related to incorrect ordering of your
outputs.

ex -

S = solve(...)

good luck,

-- Nabeel
```
 0
Reply nabeel (2) 1/28/2004 4:06:36 PM

```Thanks for the replies in this subject. Light has dawned.
```
 0
Reply tpl (606) 1/29/2004 9:16:46 AM

3 Replies
47 Views

Similar Articles

12/6/2013 1:40:04 PM
[PageSpeed]