f



How can I get the equation with numerical parameters ?

I have a equation like this

a*x1 +b*x2  (a,b are all symbolic values)

and I assigned the numerical value to a,b(e.g. a=0.5,b=0.6),and wish
to get an equation like this:

0.5*x1 + 0.6*x2

Because I wish to write the equation above into a file, so I want to
know how can I get the string forms like this:


'0.5*x1 + 0.6*x2'

Is there any simple way to do it ? Thanks in advance !
0
liuauto (51)
1/24/2009 11:59:50 AM
comp.soft-sys.matlab 211265 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies
302 Views

Similar Articles

[PageSpeed] 16

workaholic wrote:
> I have a equation like this
> 
> a*x1 +b*x2  (a,b are all symbolic values)
> 
> and I assigned the numerical value to a,b(e.g. a=0.5,b=0.6),and wish
> to get an equation like this:
> 
> 0.5*x1 + 0.6*x2
> 
> Because I wish to write the equation above into a file, so I want to
> know how can I get the string forms like this:
> 
> 
> '0.5*x1 + 0.6*x2'
> 
> Is there any simple way to do it ? Thanks in advance !

syms x1 x2 a b
w = a*x1 + b*x2

w =

a*x1 + b*x2

 >> w = subs(w,a,.5)

w =

x1/2 + b*x2

 >> w = subs(w,b,.6)

w =

x1/2 + (3*x2)/5

 >> y = char(w)

y =

x1/2 + (3*x2)/5

Note that y is a string, not a symbolic expression.

Alan Weiss
MATLAB mathematical toolbox documentation
0
aweiss (880)
1/24/2009 12:42:26 PM
On 1=D4=C224=C8=D5, =CF=C2=CE=E78=CA=B142=B7=D6, Alan Weiss <awe...@mathwor=
ks.com> wrote:
> workaholic wrote:
> > I have a equation like this
>
> > a*x1 +b*x2  (a,b are all symbolic values)
>
> > and I assigned the numerical value to a,b(e.g. a=3D0.5,b=3D0.6),and wis=
h
> > to get an equation like this:
>
> > 0.5*x1 + 0.6*x2
>
> > Because I wish to write the equation above into a file, so I want to
> > know how can I get the string forms like this:
>
> > '0.5*x1 + 0.6*x2'
>
> > Is there any simple way to do it ? Thanks in advance !
>
> syms x1 x2 a b
> w =3D a*x1 + b*x2
>
> w =3D
>
> a*x1 + b*x2
>
>  >> w =3D subs(w,a,.5)
>
> w =3D
>
> x1/2 + b*x2
>
>  >> w =3D subs(w,b,.6)
>
> w =3D
>
> x1/2 + (3*x2)/5
>
>  >> y =3D char(w)
>
> y =3D
>
> x1/2 + (3*x2)/5
>
> Note that y is a string, not a symbolic expression.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Thanks,but in my program, the numerical values are calculated in a
program, and there are too many of them which means I can't input them
one by one manually, how can I do it automatically in Matlab?

Thanks again for your help!
0
liuauto (51)
1/25/2009 3:11:12 AM
> I want to know how can I get the string forms like this:
> '0.5*x1 + 0.6*x2'
----------------------------------------------------------------------------
Use the sprintf function, like
outputString = sprintf('%.1f*x1 + %.1f*x2', a, b);

Regards,
ImageAnalyst
0
imageanalyst (7621)
1/25/2009 4:47:02 AM
workaholic wrote:
> On 1=D4=C224=C8=D5, =CF=C2=CE=E78=CA=B142=B7=D6, Alan Weiss <awe...@mat=
hworks.com> wrote:
>> workaholic wrote:
>>> I have a equation like this
>>> a*x1 +b*x2  (a,b are all symbolic values)
>>> and I assigned the numerical value to a,b(e.g. a=3D0.5,b=3D0.6),and w=
ish
>>> to get an equation like this:
>>> 0.5*x1 + 0.6*x2

You do not appear to have read the documentation on the subs() command
that you were pointed to. :-(

NewEquation =3D subs(OldEquation);



--=20
=2Esignature note: I am now avoiding replying to unclear or ambiguous pos=
tings.
Please review questions before posting them. Be specific. Use examples of=
 what you mean,
of what you don't mean. Specify boundary conditions, and data classes and=
 value
relationships -- what if we scrambled your data or used -Inf, NaN, or com=
plex(rand,rand)?
0
roberson (2980)
1/25/2009 4:49:26 AM
On 1=D4=C225=C8=D5, =CF=C2=CE=E712=CA=B149=B7=D6, Walter Roberson <rober...=
@hushmail.com> wrote:
> workaholic wrote:
> > On 1=D4=C224=C8=D5, =CF=C2=CE=E78=CA=B142=B7=D6, Alan Weiss <awe...@mat=
hworks.com> wrote:
> >> workaholic wrote:
> >>> I have a equation like this
> >>> a*x1 +b*x2  (a,b are all symbolic values)
> >>> and I assigned the numerical value to a,b(e.g. a=3D0.5,b=3D0.6),and w=
ish
> >>> to get an equation like this:
> >>> 0.5*x1 + 0.6*x2
>
> You do not appear to have read the documentation on the subs() command
> that you were pointed to. :-(
>
> NewEquation =3D subs(OldEquation);
>
> --
> .signature note: I am now avoiding replying to unclear or ambiguous posti=
ngs.
> Please review questions before posting them. Be specific. Use examples of=
 what you mean,
> of what you don't mean. Specify boundary conditions, and data classes and=
 value
> relationships -- what if we scrambled your data or used -Inf, NaN, or com=
plex(rand,rand)?

Well, thank you very much, and many thanks to all!
I never expected that it is so easy. It is my fault...
0
liuauto (51)
1/26/2009 11:10:30 AM
Reply: