How do I get rid of this "DOUBLE Cannot Convert to Double Array Error"?

Hey! So, I don't really know too much about matlab but need to write a program to demonstrate the electric potential using a contour plot. Now, I've tried to input the code as best as possible but I'm getting this error:
Error using contour (line 55) DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use VPA.
Error in As1 (line 6) contour(x,y,v)
Can someone please help me and tell me how to write the code? The equation I'm trying to input is:
The code I've written is as follows:
a = 10;
b = 5;
[x,y] = meshgrid(0:0.5:10,0:0.5:5);
syms n x y
v = ((4*100)/pi)*symsum((1/n)*(sin(n.*pi*y/b))*(cosh(n.*pi*x/b)+sinh(n.*pi*x/b)).*((1-cosh(n*pi*a/b))/sinh(n*pi*a/b)), n , 1, Inf);
contour(x,y,v)
end
Also, some of you might have noticed that in my sum, I only want odd numbers. I have no idea how to do that so if someone could help me out with that as well, I've be very grateful.
Thanks!

Answers (1)

Your symsum involves x and y and n, and so would at best be reduced to being a formula in x and y. You then try to contour() the symbolic formula over the symbolic ranges x and y.
You need to pick a definite numeric range for x and y, create a meshgrid or ndgrid of those values, and substitute into the formula.
To make the adjustment for odd n, substitute n = 2*N+1, N over 0 to infinity.
My tests so far suggest that the sum is not convergent and that for any leading prefix of infinity, the plot oscillates infinitely over (x,y). Perhaps if the range were to be drastically restricted.

9 Comments

Thanks for the reply!
Uh, so since I've already defined a and b and 10 and 5, you want me to use the values instead of a and b? Otherwise, as far as I can tell, the formula is in terms of x and y.
Is this new code correct (I'm sorry but you used a lot of jargon and I'm very, very new to Matlab):
a = 10;
b = 5;
x = 0:0.5:10;
y = 0:0.5:5;
[x,y] = meshgrid(0:0.5:10,0:0.5:5);
syms k x y
v = ((4*100)/pi)*symsum((1/(2*k-1))*(sin((2*k-1)*pi*y/5))*(cosh((2*k-1)*pi*x/5)+sinh((2*k-1)*pi*x/5)).*((1-cosh((2*k-1)*pi*10/5))/sinh((2*k-1)*pi*10/5)), k , 1, Inf);
contour(x,y,v)
end
Note: I changed n to k for the series. Also, I used 2k-1, instead of 2k+1 (made the change before your reply and just copied it here) but since the range is 1-infinity, I don't think there's any difference...
Suppose I tell you to plot the points (x, k*x) for x = 0:100. I refuse to tell you what k is. You don't know the slope of the line to be plotted so you can't plot it accurately. Should the line look more like y = x, y = -x, or y = 0? [These would correspond to k being positive, negative, or 0.]
You called MESHGRID to generate a grid of x and y values, then promptly overwrote those variables with your call to SYMS on the next line. [I suspect you thought that would convert the grid values created by MESHGRID into a symbolic form; it does not. There's a way to do that, but let's explore a slightly different option first.] Therefore when you run your code, you're doing something like giving CONTOUR a set of (x, y, k*x*y) points to plot but refusing to tell it what k is.
Don't MESHGRID until after you've called SYMSUM. Then use SUBS to substitute those values into v.
syms x y
v = someFunctionOf(x, y);
[xDouble, yDouble] = meshgrid(...)
vDouble = subs(v, {x, y}, {xDouble, yDouble});
Now I'm not so sure that this will help you in this specific situation. The fact that your summation has a term that uses SIN instead of SINH and the fact that you're trying to sum from 1 to Inf makes me suspect the same thing as Walter: I don't know if this converges but I suspect it does not.
OK, I'm sure that you guys must be rather frustrated because I keep coming back but I'm honestly don't even know what a symbolic value is. I wrote syms because the Matlab help page for symsum always had that so I thought it must be important and just copied it.
Now, I've changed the code (and it's still not working) but I did have one other question. You wrote syms x y but why didn't you include k? In the Matlab help page on symsum, for the symsum of k^x, they define k and x both as variables. Since my functions also includes k, x and y, shouldn't it be syms k x y? Anyways, I've done it both ways but I still didn't get it. Here's the new code:
a = 10;
b = 5;
syms x y
v = ((4*100)/pi)*symsum((1/(2*k-1))*(sin((2*k-1)*pi*y/5))*(cosh((2*k-1)*pi*x/5)+sinh((2*k-1)*pi*x/5)).*((1-cosh((2*k-1)*pi*10/5))/sinh((2*k-1)*pi*10/5)), k , 1, Inf);
[xDouble, yDouble] = meshgrid(0:1:10,0:1:5);
vDouble = subs(v, {x, y}, {xDouble, yDouble});
contour(x,y,v)
end
a = 10;
b = 5;
syms x y k
v = ((4*100)/pi)*symsum((1/(2*k-1))*(sin((2*k-1)*pi*y/5))*(cosh((2*k-1)*pi*x/5)+sinh((2*k-1)*pi*x/5)).*((1-cosh((2*k-1)*pi*10/5))/sinh((2*k-1)*pi*10/5)), k , 1, Inf);
[xDouble, yDouble] = meshgrid(0:1:10,0:1:5);
vDouble = subs(v, {x, y}, {xDouble, yDouble});
contour(xDouble,yDouble,vDouble)
Sorry, I was unable to get to it before. And, that was pretty dumb of me, using x instead of xDouble. Anyways, I've tried to do that and there are still errors:
Error using symengine
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in sym/double (line 588)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in datachk (line 11)
y = full(double(x));
Error in contourobjHelper>localParseargs (line 83)
z = datachk(args{3});
Error in contourobjHelper (line 16)
[varargout{1}, varargout{2}, varargout{3}, varargout{4}, varargout{5}] = localParseargs(varargin{:});
Error in contour (line 47)
[pvpairs, ~, ~, errmsg, warnmsg] = contourobjHelper('parseargs', false, args{:});
Error in As1 (line 7)
contour(xDouble,yDouble,vDouble)
Is it the equation? Have I not written it properly? I've tried to make sure the dot operator is used whenever needed but not I'm not so sure...
You would get that answer if your symbolic expression cannot be resolved to a numeric value even when you substitute in particular x and y.
Note for myself:
a := 10; b := 5; v := (4*100)*(Sum(sin((1/5)*(2*k-1)*Pi*y)*(cosh((1/5)*(2*k-1)*Pi*x)+sinh((1/5)*(2*k-1)*Pi*x))*(1-cosh(10*(2*k-1)*Pi*(1/5)))/((2*k-1)*sinh(10*(2*k-1)*Pi*(1/5))), k = 1 .. infinity))/Pi
I am almost certain this does not converge. If you try x=2 y=3 then look near at the individual contributions to the sums near k=10000 you can see that the contributions have a positive/negative cycle of length lcm(2,3) = 6, and that the values are getting larger every cycle, going way above 10^1000. Even in the k up to 20 or so you can see that the values get about 10^5 larger for each cycle of 6 elements.
The following discussion pertains to x = 2, y = 3:
I miscounted, the cycle is length 5. And yes, I can prove that it is divergent.
If you take the individual terms for k and convert the cosh to exp() format, you get
P(k) = (exp(2*(2*k-1)*Pi)-1) * exp((2/5)*(2*k-1)*Pi) * sin((3/5)*(2*k-1)*Pi) / ((exp(2*(2*k-1)*Pi)+1)*(2*k-1))
For larger k, the (exp(2*(2*k-1)*Pi)-1) on the numerator will be the same to working precision as the exp(2*(2*k-1)*Pi)+1) on the denominator. You can effectively drop those terms out, getting
P(k) = exp((2/5)*(2*k-1)*Pi) * sin((3/5)*(2*k-1)*Pi) / (2*k-1)
We are working with integer k, so we can ask the question of whether (3/5)*(2*k-1) can be an integer, as then you would have sin(integer*Pi) which would be 0. The answer is Yes, depending upon mod(k,5). If we write k = 5*L+M, M being 0, 1, 2, 3, or 4, for some positive integer L, then we can see in particular that for M=3, (3/5)*(2*(5*L+M)-1) will be an integer and therefore P(k) will be 0 with a period of 5, for the integers that end in 3 or 8 in decimal form.
Test then the other values of M and reduce the period in the sin() and you will find that two of the varieties involve sin(Pi/5) and two involve sin(2*Pi/5), with two each of them being positive or negative. These are effectively constant terms with period 5.
The denominator, (2*k-1), is a linear term. It is increasing with k, which might give a faint tinge of hope that the terms reduce in magnitude for increasing k. But the hope does not carry through.
We are left then with consideration of the term
exp((2/5)*(2*k-1)*Pi)
and that clearly grows exponentially large with increasing k. Exponential terms dominate over linear changes.
We therefore must conclude that although there are a mix of positive and negative terms, that once we have reached a k large enough for the numerator and denominator terms with the exp(something +/- 1) to numerically cancel out, then the following terms have magnitudes that are increasing exponentially for with period 5 (except that every 5th term is a 0.) This cannot converge. The infinite sum is undefined.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 18 Sep 2015

Commented:

on 25 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!