How do I get rid of this "DOUBLE Cannot Convert to Double Array Error"?
Show older comments
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)
Walter Roberson
on 18 Sep 2015
0 votes
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
Anonymous1a
on 18 Sep 2015
Edited: Anonymous1a
on 18 Sep 2015
Steven Lord
on 18 Sep 2015
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.
Anonymous1a
on 18 Sep 2015
Edited: Anonymous1a
on 18 Sep 2015
Walter Roberson
on 18 Sep 2015
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)
Anonymous1a
on 25 Sep 2015
Walter Roberson
on 25 Sep 2015
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.
Walter Roberson
on 25 Sep 2015
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
Walter Roberson
on 25 Sep 2015
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.
Walter Roberson
on 25 Sep 2015
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.
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!