Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Symbolic math (double, vpa, sym error)

739 views
Skip to first unread message

Matin Bagherpour

unread,
Sep 17, 2009, 6:10:03 AM9/17/09
to
I am trying to use Symbolic math tool to calculate this equation

function [q] = Eq27(Lg,x,r)
q = 0;
for k = 0:floor(x)
for l = 0:Lg-1
q =((-1^l)/(factorial(l)*factorial(k)))*nchoosek(Lg+k,k+l+1)*((r*(k-x))^(k+1))*exp(-1*r*(k-x));
end
end
q = ((1-r)^Lg)* q;

I define Lg, x, and r as symbolic variables in command prompt. However, I get this error message whenever I run Eq27(Lg,x,r):
??? Error using ==> sym.double at 25
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.

Error in ==> sym.colon at 20
n = double((b-a)/d);

Error in ==> Eq27 at 3
for k = 0:floor(x)

Can you please help me to solve it?

eematic ee

unread,
Sep 17, 2009, 8:00:21 AM9/17/09
to
try putting the Syms statement at the begining of the program:

syms Lg x r


"Matin Bagherpour" <mbagh...@yahoo.com> wrote in message <h8t1tr$1uu$1...@fred.mathworks.com>...


> I am trying to use Symbolic math tool to calculate this equation
>
> function [q] = Eq27(Lg,x,r)

syms Lg x r %try it here

Matin Bagherpour

unread,
Sep 18, 2009, 11:32:02 AM9/18/09
to
I put it there. But again the same error messages:

??? Error using ==> sym.double at 25
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.

Error in ==> sym.colon at 20
n = double((b-a)/d);

Error in ==> Eq27 at 4


for k = 0:floor(x)

It seems that I have to change definition of symbolic variables with subs or vpa function. But I don't know how.

"eematic ee" <eem...@att.net> wrote in message <h8t8cl$bp6$1...@fred.mathworks.com>...

Steven Lord

unread,
Sep 18, 2009, 1:04:47 PM9/18/09
to

"Matin Bagherpour" <mbagh...@yahoo.com> wrote in message
news:h9095i$mm1$1...@fred.mathworks.com...

>I put it there. But again the same error messages:
> ??? Error using ==> sym.double at 25
> DOUBLE cannot convert the input expression into a double array.
> If the input expression contains a symbolic variable, use the VPA function
> instead.
>
> Error in ==> sym.colon at 20
> n = double((b-a)/d);
>
> Error in ==> Eq27 at 4
> for k = 0:floor(x)
>
> It seems that I have to change definition of symbolic variables with subs
> or vpa function. But I don't know how.

You can't do a FOR loop over a symbolic expression if that expression
contains a symbolic variable. So for instance, this would work, since while
there is a symbolic expression involved that symbolic expression does not
contain a symbolic variable:


y = 0;
for k = 1:sym(12345)
y = y+1;
end
y


but this would not, since we can't tell how many times to iterate on the FOR
loop:


clear z
y = 0;
for k = 1:sym('z')
y = y+1;
end
y


If the expression you're using in the loop does contain some symbolic
variables, you must substitute in a value for those variables (using SUBS)
before trying to use it as a limit or increment in the FOR loop.

I've just checked and in release R2009b, the message that appears in the
latter situation has been made clearer:


>> clear z
y = 0;
for k = 1:sym('z')
y = y+1;
end
y
??? Error using ==> sym.colon at 27
Cannot compute the number of steps from 1 to z by 1.


--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Matin Bagherpour

unread,
Sep 19, 2009, 6:23:01 AM9/19/09
to
Thanks. But the problem is that I don't want to substitute a number for that symbolic variable 'x'. Because I want Matlab to calculate closed form of integration of a function which contains summation of some values from 1 to floor('x'), and 'x' is the iintegration variable. How can I substitute 'x' with a value and still obtain integration as a function of 'x'?

"Steven Lord" <sl...@mathworks.com> wrote in message <h90ej0$ddo$1...@fred.mathworks.com>...

Nasser Abbasi

unread,
Sep 19, 2009, 9:47:14 PM9/19/09
to
"Matin Bagherpour" <mbagh...@yahoo.com> wrote in message
news:h92be5$opf$1...@fred.mathworks.com...

> Thanks. But the problem is that I don't want to substitute a number for
> that symbolic variable 'x'. Because I want Matlab to calculate closed form
> of integration of a function which contains summation of some values from
> 1 to floor('x'), and 'x' is the iintegration variable. How can I
> substitute 'x' with a value and still obtain integration as a function of
> 'x'?
>

To make it easier to help you, can you show all the commands you made.
Starting from the console, what you typed, and how you called the function
Eq27() ?

This will make it easier for someone to repeat the same steps you made.

--Nasser


Steven Lord

unread,
Sep 21, 2009, 9:47:54 AM9/21/09
to

"Matin Bagherpour" <mbagh...@yahoo.com> wrote in message
news:h92be5$opf$1...@fred.mathworks.com...

> Thanks. But the problem is that I don't want to substitute a number for
> that symbolic variable 'x'. Because I want Matlab to calculate closed form
> of integration of a function which contains summation of some values from
> 1 to floor('x'), and 'x' is the iintegration variable. How can I
> substitute 'x' with a value and still obtain integration as a function of
> 'x'?

You could try using SYMSUM, but I would probably write my integrand function
so it accepts a numeric value for x and evaluates the integrand for that
value of x, then use QUADGK.

Matin Bagherpour

unread,
Sep 22, 2009, 4:55:07 AM9/22/09
to
I have written an M-file named Eq38 as follows:
%---------------------------------------------------------------------------------------------------
function [q] = Eq38(x,r1,r2,b1,b2,n1,n2)
%Convolution of waiting times in M/D/1 queues having different service times
%Two groups of M/D/1 queues of size i n , each with load ?i and service time bi ,i = 1,2
% DF of the waiting time
q = 0;
q1 = 0;
q2 = 0;
xx = r1/b1;
y = r2/b2;
z = y/(y-xx);
n = xx/(xx-y);
for k = 0:floor(x/b1)
for j = 0:floor((x-k.*b1)/b2)
for s = 0:n2-1
q11 = 0;
for l = max(0,j-n1+1):min(n2-s-1+j,k+j)
q11 = q11 + ((-1)^(j-l)).*nchoosek(n2-s-1+j,l).*nchoosek(n1-1+k,k+j-l).*(z^l).*(n^(k+j-l));
end
q1 =q1 + ((1-r2)^(n2-s-1)).*z.*(nchoosek(n1+n2-s-2,n2-s-1).*nchoosek(k+j,j).*(n^(n2-s-1)).*(z^(n1-1))).*q11.*(Eq28(s+1,((x-k.*b1-j.*b2)/b2),r2)-Eq28(s+1,((x-k.*b1-j.*b2)/b2)-1,r2));
end
end
end
for k = 0:floor(x/b2)
for j = 0:floor((x-k.*b2)/b1)
for s = 0:n1-1
q22 = 0;
for l = max(0,j-n1+s+1):min(n2-1+j,k+j)
q22 = q22 + ((-1)^(j-l)).*nchoosek(n2-1+j,l).*nchoosek(n1-s-1+k,k+j-l).*(z^l).*(n^(k+j-l));
end
q2 =q2 + ((1-r1)^(n1-s-1)).*n.*(nchoosek(n1+n2-s-2,n2-1).*nchoosek(k+j,j).*(n^(n2-1)).*(z^(n1-s-1))).*q22.*(Eq28(s+1,((x-k.*b1-j.*b2)/b1),r1)-Eq28(s+1,((x-k.*b1-j.*b2)/b1)-1,r1));
end
end
end
q = ((1-r1)^n1).* q1 + ((1-r2)^n2).* q2;
%--------------------------------------------------------------------------------------
Then another function to calculate integrations of Eq38 named Delay38:
%---------------------------------------------------------------------------------------------------
function [ete] = Delay38(rv1,rv2,bv1,bv2,nv1,nv2,rl1,rl2,bl1,bl2,nl1,nl2,ru1,ru2,bu1,bu2,nu1,nu2)
v = sym('v');
l = sym('l');
u = sym('u');
UR_1 = int(Eq38(v,rv1,rv2,bv1,bv2,nv1,nv2),'v',t-l,inf);
First = int(Eq38(l,rl1,rl2,bl1,bl2,nl1,nl2).*Eq38(t-l,ru1,ru2,bu1,bu2,nu1,nu2).*UR_1,'l',0,inf);
UR_2 = int(Eq38(u,ru1,ru2,bu1,bu2,nu1,nu2),'u',t-l,inf);
Second = int(Eq38(l,rl1,rl2,bl1,bl2,nl1,nl2).*Eq38(t-l,rv1,rv2,bv1,bv2,nv1,nv2).*UR_2,'l',0,inf);
ete = int(t.*(First + Second),'t',0,inf);
%------------------------------------------------------------------------------------------------------

In the console I typed:
>> Delay38(0.6,0.7,1,0.5,1,2,0.7,0.5,1,0.2,1,0,0.7,0.5,1,0.5,1,2)
(some typical values for the inputs)
and received this message:

??? Error using ==> sym.double at 25
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.

Error in ==> sym.colon at 20
n = double((b-a)/d);

Error in ==> Eq38 at 13
for k = 0:floor(x/b1)

Error in ==> Delay38 at 5
UR_1 = int(Eq38(v,rv1,rv2,bv1,bv2,nv1,nv2),'v',t-l,inf);

which seems that there is a problem with having a symbolic variable as FOR loop control variable.
I don't know how to use SUBS while as you can see from Delay38 that I need the first integration:
UR_1 = int(Eq38(v,rv1,rv2,bv1,bv2,nv1,nv2),'v',t-l,inf);
to be symbolic (a function of 't' and 'l') and be used in the second integration (a function of 't'):
First = int(Eq38(l,rl1,rl2,bl1,bl2,nl1,nl2).*Eq38(tl,ru1,ru2,bu1,bu2,nu1,nu2).*UR_1,'l',0,inf);

and finally in the third one:
ete = int(t.*(First + Second),'t',0,inf);

Could you please help me with it?

"Nasser Abbasi" <n...@12000.org> wrote in message <wQftm.16710$j34....@newsfe01.iad>...

0 new messages