简体   繁体   中英

3D Plot in Matlab - not correct

I am trying to calculate the potential function in a given coplanar rectangular structure. Here is the equation i deduced by using mathematics, here is the sumary: Assumption

Now I try to plot it in MATLAB, here is my code:

function [x,y,v] = potentialFM(a1,a2,a3,b)
syms n;

%range in normalized values
x=0:1/20:1;
y=0:(b/a3)/20:(b/a3);
[X,Y] = meshgrid(x,y);

%normalized values
an1=a1/a3;
an2=a2/a3;
bn=b/a3;

%symbol k 
k=(n.*2+1)*(pi/2);

C1=(1/(k.^2)).*(2/(an1-an2));
C2=cos(k.*an2)-cos(k.*an1);
C3=1/(sinh(k.*bn));
Vx=cos(X*k);
Vy=sinh(Y*k);
v=symsum(C1.*C2.*C3.*Vx.*Vy,n,1,20);
end

So, Could someone have a try to my code and check where is an error. The result should look like the assumed graph in the link but i cannot achieve it.

Best Regards,

fRz

You don't have a problem with plotting, but with the evaluation of your function. Why do you use symbolics if you want a numeric result ready for plotting?

Here's a simple fix – n is now simply a loop variable, k another normal variable, and the sum is computed cumulatively using the variable v :

function [x,y,v] = potentialFM(a1,a2,a3,b)

%range in normalized values
x=0:1/20:1;
y=0:(b/a3)/20:(b/a3);
[X,Y] = meshgrid(x,y);

%normalized values
an1=a1/a3;
an2=a2/a3;
bn=b/a3;

v = 0;
for n = 1 : 20
    k=(n.*2+1)*(pi/2);

    C1=(1/(k.^2)).*(2/(an1-an2));
    C2=cos(k.*an2)-cos(k.*an1);
    C3=1/(sinh(k.*bn));
    Vx=cos(X*k);
    Vy=sinh(Y*k);
    v = v + C1.*C2.*C3.*Vx.*Vy;
end

This is not necessarily the most efficient implementation, maybe some of the computations could be vectorized – but it works. Choosing arbitrarily the parameters,

[x,y,v] = potentialFM(1, 2, 3, 4);
surf(x, y, v)

gives:

表面图

Another attempt: If you need the value of the infinite sum and hope that symsum can find a closed form expression, there is no need to mix it with numerics. Instead, declare all the variables as symbolic:

syms n an1 an2 bn X Y
k=(n.*2+1)*(pi/2);
C1=(1/(k.^2)).*(2/(an1-an2));
C2=cos(k.*an2)-cos(k.*an1);
C3=1/(sinh(k.*bn));
Vx=cos(X*k);
Vy=sinh(Y*k);
v=symsum(C1.*C2.*C3.*Vx.*Vy,n,1,inf);

symsum returns a result, but it looks like this:

-(8*sum(((exp(-(pi*X*(2*n + 1)*i)/2)/2 + exp((pi*X*(2*n + 1)*i)/2)/2)*(exp(-(pi*Y*(2*n + 1))/2)/2 - exp((pi*Y*(2*n + 1))/2)/2)*(exp(-(pi*an1*(2*n + 1)*i)/2)/2 + exp((pi*an1*(2*n + 1)*i)/2)/2 - exp(-(pi*an2*(2*n + 1)*i)/2)/2 - exp((pi*an2*(2*n + 1)*i)/2)/2))/((2*n + 1)^2*(exp(-(pi*bn*(2*n + 1))/2)/2 - exp((pi*bn*(2*n + 1))/2)/2)), n == 1..Inf))/(pi^2*(an1 - an2))

As you can see, the Symbolic Math Toolbox was able to perform some simplifications (?) but could not solve the core of the problem: There is still an infinite sum in there. As far as I understand, this indicates that there is no closed form expression (or at least that finding it is beyond the capabilities of the toolbox).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM