Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
large numbers - syms

Subject: large numbers - syms

From: Marcio

Date: 23 Sep, 2008 19:56:01

Message: 1 of 9

Please, dear friends, I am really new to Matlab, I'm trying to solve a symbolic equation that shows coeffients like this:
1585014010279703/1125899906842624

Please, how do I convert that to a short number like "ans"

>> 1585014010279703/1125899906842624

ans = 1.4078

pretty much simpler, isn't it?

I put "format short" on my m-file and I started getting this error:

========================================================
??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> fzero at 309
    elseif ~isfinite(fx) || ~isreal(fx)
========================================================

Thank you very much

Subject: large numbers - syms

From: Nasser Abbasi

Date: 23 Sep, 2008 21:20:55

Message: 2 of 9


"Marcio " <marciobarbalho@eq.ufrn.br> wrote in message
news:gbbhkh$au2$1@fred.mathworks.com...
> Please, dear friends, I am really new to Matlab, I'm trying to solve a
> symbolic equation that shows coeffients like this:
> 1585014010279703/1125899906842624
>
> Please, how do I convert that to a short number like "ans"
>
>>> 1585014010279703/1125899906842624
>
> ans = 1.4078
>
> pretty much simpler, isn't it?
>
> I put "format short" on my m-file and I started getting this error:
>
> ========================================================
> ??? Operands to the || and && operators must be convertible to logical
> scalar values.
>
> Error in ==> fzero at 309
> elseif ~isfinite(fx) || ~isreal(fx)
> ========================================================
>
> Thank you very much

You need to first convert the sym object to a double to use format on it.

EDU>> a=sym(1585014010279703/1125899906842624)
EDU>> b=double(a)

b =

    1.4078

EDU>> format long
EDU>> b

b =

   1.407775238852785

EDU>> format short
EDU>> b

b =
    1.4078

Nasser

Subject: large numbers - syms

From: Marcio

Date: 24 Sep, 2008 02:11:04

Message: 3 of 9

>
> You need to first convert the sym object to a double to use format on it.
>
> EDU>> a=sym(1585014010279703/1125899906842624)
> EDU>> b=double(a)
>
> b =
>
> 1.4078
>
> EDU>> format long
> EDU>> b
>
> b =
>
> 1.407775238852785
>
> EDU>> format short
> EDU>> b

> b =
> 1.4078
>
> Nasser
>
please, what if the symbolic equation is calculated like this?

sum1=0;
for i=1:n
      sum1 = sum1 + 1/(1-V);
end

so, the coefficients of sum1 are going to appear in that large format, how can I use the "sym" command to convert it to a short number instead of a fraction?

Subject: large numbers - syms

From: Walter Roberson

Date: 24 Sep, 2008 02:50:26

Message: 4 of 9

Marcio wrote:

> please, what if the symbolic equation is calculated like this?
 
> sum1=0;
> for i=1:n
> sum1 = sum1 + 1/(1-V);
> end
 
> so, the coefficients of sum1 are going to appear in that large format,
> how can I use the "sym" command to convert it to a short number instead of a fraction?

It is not clear from your posting what V is. If V is a matlab number, or is
sym(a numeric constant), then double(sum1) will evaluate to the single
double precision floating point equivalent of sum1.

If, though, V is a symbolic name so your expression is some large expression
with a mix of fractions and operations and symbolic names and exponents,
and you want to reduce it that is a mix of simple fixed-point numbers and
operations and symbolic names and exponents, then the pure Maple call to do
that would be evalf(sum1) which would use the default Digits setting for the
precision, or evalf(sum1, N) which would use N digits for the precision.
I do not know how you would invoke that Maple call from Matlab; possibly
vpa(sum1) or vpa(sum1,N) would be the equivalents.

Usually, though, it is better to keep the fraction form around as long
as possible, using simplification and factorization and collection to
make it more humanly readable when that is needed. Then when you want to
evaluate the expression with a specific numeric value in place of the
symbol, you would use the Maple call subs() or eval(). If the numeric value
being substituted is in decimal format, then subs() or eval() will
automatically collapse as much as possible down to a decimal number.
For example the Maple call
subs(V=1/3,1/2+V) would result in the symbolic fraction 5/6 but
subs(V=0.333,1/2+V) would result in the symbolic number 0.8330000000 --
the decimal number triggers the collapse of all of the fractions.
But either way, you still need to double() these symbolic numbers to
get a double precision number that Matlab can operate on directly.

Sorry, I do not have the symbolic toolbox myself so I do not know what
the Matlab equivalent of all of the Maple forms are. I have Maple itself and
know it relatively well (the language itself, anyhow -- the libraries are too
big for any one person to reasonably understand them all.)

Subject: large numbers - syms

From: Marcio

Date: 24 Sep, 2008 03:28:02

Message: 5 of 9

Sorry, let me rephrase the question:

V is symbolic:

sum1 = 0;
for i=1:n
    sum1=sum1+1/(1-V); % for example
end

user defines 'n', so the symbolic expression must be solved, however, user needs to see the final equation if n=4, for example, then sum1 shows large coefficients, question is, how to make them readable?

Thanks

Subject: large numbers - syms

From: Marcio

Date: 24 Sep, 2008 03:33:01

Message: 6 of 9

Walter Roberson <wrote in message >
I do not know how you would invoke that Maple call from Matlab; possibly vpa(sum1) or vpa(sum1,N) would be the equivalents.

yeah, it worked out. Thanks

Subject: large numbers - syms

From: Bryn

Date: 14 Nov, 2008 01:54:02

Message: 7 of 9

Hello all. I am trying to write a loop to find the stiffness matrix in a FEM analysis. The code is:

K = sparse(4,4);
for i = 1:1:4
    for j = 1:1:4
        K(i,j) = vpa(int((d2phi(i)*d2phi(j)),-1,1),6);
    end
end

Basically d2phi(i) refers to symbolic expressions in the d2phi vector. I am trying to integrate 2 symbolic expressions multiplied together to get an actual number, and then append that to the matrix "K". The symbolic expressions vary with the indexes of the components of K. I keep getting the "conversion to double from sym is not possible" error message. Any ideas?

Thanks

Subject: large numbers - syms

From: Walter Roberson

Date: 14 Nov, 2008 05:27:20

Message: 8 of 9

Bryn wrote:
> Hello all. I am trying to write a loop to find the stiffness matrix in a FEM analysis. The code is:
>
> K = sparse(4,4);
> for i = 1:1:4
> for j = 1:1:4
> K(i,j) = vpa(int((d2phi(i)*d2phi(j)),-1,1),6);
> end
> end

> Basically d2phi(i) refers to symbolic expressions in the d2phi vector. I am trying to
> integrate 2 symbolic expressions multiplied together to get an actual number,
> and then append that to the matrix "K". The symbolic expressions vary with the indexes
> of the components of K. I keep getting the "conversion to double from sym is not possible"
> error message. Any ideas?

If there is even one case in which it is unable to find the integral of
d2phi(i)*d2phi(j), then the int() operation would return a symbolic result
and vpa() would be unable to convert it to a double.


A point to check:
I do not have the symbolic toolbox available to me to check with:
if x and y store symbolic expressions, then is x*y a symbolic expression? And if
so, which expression is it? Is it different than x.*y ?

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
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 complex(rand,rand)?

Subject: large numbers - syms

From: Baha Kuzu

Date: 15 Oct, 2010 14:47:05

Message: 9 of 9

Walter Roberson <roberson@hushmail.com> wrote in message <8VhCk.31593$rV4.5183@newsfe03.iad>...
> Marcio wrote:
>
> > please, what if the symbolic equation is calculated like this?
>
> > sum1=0;
> > for i=1:n
> > sum1 = sum1 + 1/(1-V);
> > end
>
> > so, the coefficients of sum1 are going to appear in that large format,
> > how can I use the "sym" command to convert it to a short number instead of a fraction?
>
> It is not clear from your posting what V is. If V is a matlab number, or is
> sym(a numeric constant), then double(sum1) will evaluate to the single
> double precision floating point equivalent of sum1.
>
> If, though, V is a symbolic name so your expression is some large expression
> with a mix of fractions and operations and symbolic names and exponents,
> and you want to reduce it that is a mix of simple fixed-point numbers and
> operations and symbolic names and exponents, then the pure Maple call to do
> that would be evalf(sum1) which would use the default Digits setting for the
> precision, or evalf(sum1, N) which would use N digits for the precision.
> I do not know how you would invoke that Maple call from Matlab; possibly
> vpa(sum1) or vpa(sum1,N) would be the equivalents.
>
_______________

I am having same problem. Here;
>> u=[0.6124 -0.6124 -0.5; -0.2041-0.5772*i -0.2041+0.5772*i 0.5; -0.2887+0.4082*i -0.2887-0.4082*i 0.7071];
>> syms t
>> a=u*[exp(2.*i.*t) 0 0; 0 exp(-2.*i.*t) 0; 0 0 1]*transpose(u)*[1; 0; 0]
>> ans =
 
                                (2343961*cos(t)^2)/1562500 - 1562711/3125000
        - (3124771*sin(2*t)*i)/12500000 - 1/4 - (2209233*cos(2*t)*i)/3125000
 - (4419997*sin(2*t)*i)/12500000 - 7071/20000 + (3124771*cos(2*t)*i)/6250000
_________________
Anybody knows to figure out the problem? I want to get something like 1.2*sin(..)

Thank you very much in advance.
Baha

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us