|
|
conversion from symbolic to double
Hi
I've a problem when I want to convert a symbolic variable to double.
The example is from the MATLAB documentation.
>> syms x
>> f = sin(x^2);
>> y = feval(symengine, 'numeric::fsolve',f,'x=3')
y =
x = 3.069980123839465465438655
>> double(y)
??? Error using ==> sym.double at 29
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
How can I convert the variable x to double?
VPA did not work, too. (or maybe I did it the wrong way)
Thanks for any hints.
|
|
0
|
|
|
|
Reply
|
Martin
|
4/7/2010 12:26:23 PM |
|
"Martin " <martin3421@googlemail.com> wrote in message
news:hphtlf$qj8$1@fred.mathworks.com...
> Hi
> I've a problem when I want to convert a symbolic variable to double.
> The example is from the MATLAB documentation.
>
>>> syms x
>>> f = sin(x^2);
>>> y = feval(symengine, 'numeric::fsolve',f,'x=3')
>
> y =
>
> x = 3.069980123839465465438655
>
>>> double(y)
> ??? Error using ==> sym.double at 29
> DOUBLE cannot convert the input expression into a double array.
> If the input expression contains a symbolic variable, use the VPA function
> instead.
>
> How can I convert the variable x to double?
Strip off the "x = " portion of y first before trying to convert it to
DOUBLE.
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
|
|
0
|
|
|
|
Reply
|
Steven
|
4/7/2010 1:31:13 PM
|
|
"Steven Lord" <slord@mathworks.com> wrote in message <hpi1e6$o85$1@fred.mathworks.com>...
>
> "Martin " <martin3421@googlemail.com> wrote in message
> news:hphtlf$qj8$1@fred.mathworks.com...
> > Hi
> > I've a problem when I want to convert a symbolic variable to double.
> > The example is from the MATLAB documentation.
> >
> >>> syms x
> >>> f = sin(x^2);
> >>> y = feval(symengine, 'numeric::fsolve',f,'x=3')
> >
> > y =
> >
> > x = 3.069980123839465465438655
> >
> >>> double(y)
> > ??? Error using ==> sym.double at 29
> > DOUBLE cannot convert the input expression into a double array.
> > If the input expression contains a symbolic variable, use the VPA function
> > instead.
> >
> > How can I convert the variable x to double?
>
> Strip off the "x = " portion of y first before trying to convert it to
> DOUBLE.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
thanks for the quick response!
But that means that I have to convert the variable into a string first?
>> z = char(y)
z =
x = 3.069980123839465465438655
>> u = z(5:end)
u =
3.069980123839465465438655
>> o = str2num(u)
o =
3.069980123839466
This way it works, thanks, but it looks a bit complicated to me.
Maybe there is an better way?
|
|
0
|
|
|
|
Reply
|
Martin
|
4/7/2010 1:58:26 PM
|
|
"Martin " <martin3421@googlemail.com> wrote in message <hphtlf$qj8$1@fred.mathworks.com>...
> Hi
> I've a problem when I want to convert a symbolic variable to double.
> The example is from the MATLAB documentation.
>
> >> syms x
> >> f = sin(x^2);
> >> y = feval(symengine, 'numeric::fsolve',f,'x=3')
>
> y =
>
> x = 3.069980123839465465438655
>
> >> double(y)
> ??? Error using ==> sym.double at 29
> DOUBLE cannot convert the input expression into a double array.
> If the input expression contains a symbolic variable, use the VPA function instead.
>
> How can I convert the variable x to double?
> VPA did not work, too. (or maybe I did it the wrong way)
> Thanks for any hints.
one of the few solutions
double(solve(y))
% ans = 3.06998012383947 % <- format long g
us
|
|
0
|
|
|
|
Reply
|
us
|
4/7/2010 2:05:25 PM
|
|
"us " <us@neurol.unizh.ch> wrote in message <hpi3f5$qcq$1@fred.mathworks.com>...
> "Martin " <martin3421@googlemail.com> wrote in message <hphtlf$qj8$1@fred.mathworks.com>...
> > Hi
> > I've a problem when I want to convert a symbolic variable to double.
> > The example is from the MATLAB documentation.
> >
> > >> syms x
> > >> f = sin(x^2);
> > >> y = feval(symengine, 'numeric::fsolve',f,'x=3')
> >
> > y =
> >
> > x = 3.069980123839465465438655
> >
> > >> double(y)
> > ??? Error using ==> sym.double at 29
> > DOUBLE cannot convert the input expression into a double array.
> > If the input expression contains a symbolic variable, use the VPA function instead.
> >
> > How can I convert the variable x to double?
> > VPA did not work, too. (or maybe I did it the wrong way)
> > Thanks for any hints.
>
> one of the few solutions
>
> double(solve(y))
> % ans = 3.06998012383947 % <- format long g
>
> us
Thank you!
|
|
0
|
|
|
|
Reply
|
Martin
|
4/7/2010 2:22:27 PM
|
|
|
4 Replies
1139 Views
(page loaded in 0.885 seconds)
|
|
|
|
|
|
|
|
|