use solution of solve (symbolic)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

use solution of solve (symbolic)

Stef Pillaert
Hello,
I must me missing something obvious, but can't find a simple solution:
I have two symbolic variables (a,b) and two other variables depending on
them (dbl_a, dbl_b).
I solve two equations, which I can solve for a and b.
How do I "substitute" those solutions into the depending variables, so I can
see what their values are?

pkg load symbolic
syms a b
dbl_a=2*a
dbl_b=2*b
eq1=dbl_a==9
eq2=dbl_b==10
solution=solve([eq1 eq2],[a b])
a=solution.a
b=solution.b
vpa(dbl_a)
vpa(dbl_b)

When I run this, I get:

a = (sym) 9/2
b = (sym) 5
ans = (sym) 2.0*a
ans = (sym) 2.0*b

But I need the last two lines to return "9" and "10", to inspect the values.
I allready tried to use "subs", without succes....
Of course, I can change the last two lines to "vpa(dbl_a=2*a)" and
"vpa(dbl_b=2*b)", but actually my calculations and equations are a lot more
complex, this is a MWE
Many thanks for any help,
Stef



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: use solution of solve (symbolic)

Doug Stewart-4


On Wed, Mar 27, 2019 at 4:58 PM Stef Pillaert <[hidden email]> wrote:
Hello,
I must me missing something obvious, but can't find a simple solution:
I have two symbolic variables (a,b) and two other variables depending on
them (dbl_a, dbl_b).
I solve two equations, which I can solve for a and b.
How do I "substitute" those solutions into the depending variables, so I can
see what their values are?

pkg load symbolic
syms a b
dbl_a=2*a
dbl_b=2*b
eq1=dbl_a==9
eq2=dbl_b==10
solution=solve([eq1 eq2],[a b])
a=solution.a
b=solution.b
vpa(dbl_a)
vpa(dbl_b)

here is one way

solution=solve([eq1 eq2],[a b])
a1=solution.a
b1=solution.b
double(subs(dbl_a,a,a1))
double(subs(dbl_b,b,b1))



 
When I run this, I get:

a = (sym) 9/2
b = (sym) 5
ans = (sym) 2.0*a
ans = (sym) 2.0*b

But I need the last two lines to return "9" and "10", to inspect the values.
I allready tried to use "subs", without succes....
Of course, I can change the last two lines to "vpa(dbl_a=2*a)" and
"vpa(dbl_b=2*b)", but actually my calculations and equations are a lot more
complex, this is a MWE
Many thanks for any help,
Stef



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: use solution of solve (symbolic)

Colin Macdonald-2
On 2019-03-27 6:21 p.m., Doug Stewart wrote:
> here is one way
>
> solution=solve([eq1 eq2],[a b])
> a1=solution.a
> b1=solution.b
> double(subs(dbl_a,a,a1))
> double(subs(dbl_b,b,b1))

I agree with Doug.

In the *next* version of Symbolic (2.8.0, not out yet) you will also be
able to use subs and/or eval to shortcut this a little bit:

syms a b
dbl_a=2*a
dbl_b=2*b
eq1=dbl_a==9
eq2=dbl_b==10
solution=solve([eq1 eq2],[a b])
a = solution.a
b = solution.b
eval(dbl_a)
eval(dbl_b)

The eval command will search your workspace for variables whose names
match the symbols a and b.

Note: IMHO, this sort of thing might be nice for interactive
command-line usage, but Doug's approach is better for code.

Colin


Reply | Threaded
Open this post in threaded view
|

RE: use solution of solve (symbolic)

Stef Pillaert
OK, many thanks. What I need is the second solution (indeed for interactive usage), to do similar things as easily achievable in Matlab.
The first solution is too tedious for my goals, my original solution (using copy-paste) is quicker .... But I agree, for programming purposes the first approach is more logical.
Any idea when the first solution wiil be available? I would like to apply this for my students next academic year (september?)

Stef

-----Oorspronkelijk bericht-----
Van: Help-octave <help-octave-bounces+stef.pillaert=[hidden email]> Namens Colin Macdonald
Verzonden: woensdag 27 maart 2019 23:52
Aan: [hidden email]
Onderwerp: Re: use solution of solve (symbolic)

On 2019-03-27 6:21 p.m., Doug Stewart wrote:
> here is one way
>
> solution=solve([eq1 eq2],[a b])
> a1=solution.a
> b1=solution.b
> double(subs(dbl_a,a,a1))
> double(subs(dbl_b,b,b1))

I agree with Doug.

In the *next* version of Symbolic (2.8.0, not out yet) you will also be able to use subs and/or eval to shortcut this a little bit:

syms a b
dbl_a=2*a
dbl_b=2*b
eq1=dbl_a==9
eq2=dbl_b==10
solution=solve([eq1 eq2],[a b])
a = solution.a
b = solution.b
eval(dbl_a)
eval(dbl_b)

The eval command will search your workspace for variables whose names match the symbols a and b.

Note: IMHO, this sort of thing might be nice for interactive command-line usage, but Doug's approach is better for code.

Colin




Reply | Threaded
Open this post in threaded view
|

Re: use solution of solve (symbolic)

Colin Macdonald-2
On 2019-03-28 11:03 a.m., Stef Pillaert wrote:
> OK, many thanks. What I need is the second solution (indeed for interactive usage), to do similar things as easily achievable in Matlab.
> The first solution is too tedious for my goals, my original solution (using copy-paste) is quicker .... But I agree, for programming purposes the first approach is more logical.
> Any idea when the first solution wiil be available? I would like to apply this for my students next academic year (september?)

The solution that Doug showed works now...  I guess you mean the
"eval(foo)" and "subs(foo)" approach that will be in Symbolic 2.8.0.

Symbolic 2.8.0 will be released soon after SymPy 1.4, likely within a
month or so.

Some testing of subs/eval stuff would be great: feel free to download
and try it before the release.

Colin