Plotting 3 variable functions (Laplace equation)

CONTOUR PLOTTING OF LAPLACE EQUATION
Capture.JPG
I wish to plot this equation. The gist that I get is :
  1. I have to vary x,y and n
  2. n will have odd numbers only for this arrangement
Accordingly I have typed this out in MATLAB:
clc
clear all
close all
v0=100;
a=5;
b=10;
x=1:1:200;
y=1:1:200;
[x,y]=meshgrid(x,y);
for n=1:2:100
z(n)=(4.*v0./pi).*(sin(n.*pi.*x./a).*(sinh(n.*pi.*y./a)./(n.*sinh(n.*pi.*b./a))));
end
contour3(x,y,z);
From my basic understanding of plotting, I have to index z so that I can keep the values for plotting later. But an error is generated at the z(n) line.The error says,
Subscripted assignment dimension mismatch.
I know this is a matrix size problem, but I am unable to find a feasible solution. How do I solve this size problem? Is there any better way to formulate this equation since it is a 3-variable formula? Or is there any constraint of choosing the range of x,y? If you have a clear explanation and a solution for this, kindly help.
Thanks.

 Akzeptierte Antwort

Star Strider
Star Strider am 19 Jul. 2019
You need to create matrices from ‘x’ and ‘y’ using either meshgrid or ndgrid. Then, you can plot the contours.
Please re-examine your values for ‘x’ and ‘y’. I divided them by 100 here to get what I consider to be a reasonably acceptable plot:
v0=100;
a=5;
b=10;
x=1:1:200;
y=1:1:200;
n=1:2:100;
[X,Y,N]=meshgrid(x/100,y/100,n);
Z = @(x,y,n) (4.*v0./pi).*(sin(n.*pi.*x./a).*(sinh(n.*pi.*y./a)./(n.*sinh(n.*pi.*b./a))));
Zs = sum(Z(X,Y,N),3); % Sum Over ‘n’
figure
contourf(x/100, y/100, Zs)
axis('equal')
Experiment to get the results you want.

7 Kommentare

John Doe
John Doe am 20 Jul. 2019
Thank you so so much for the prompt and efficient reply.
I just have few more silly questions to clear my concept. Why did you write "Zs=sum(Z(X,Y,N),3)" instead of "Zs=sum(Z(x,y,n),3)" ? and "contourf(x/100, y/100, Zs)" rather than "contourf(X/100, Y/100, Zs)" ?
As always, my pleasure.
First, I used:
Zs = sum(Z(X,Y,N),3); % Sum Over ‘n’
because the three matrices are 3D matrices, each with the same row, column, and ‘page’ sizes, with ‘n’ (creating ‘N’ in the third dimension), so I summed over the third dimension, as the posted equation requires. Note that in MATLAB (and matrix calculations generally), all the matrices must be the same sizes. The meshgrid (and ndgrid) functions create appropriate-sized matrices from their argument vectors, allowing straightforward element-wise matrix calculations, as necessary here. The ‘x’, ‘y’ and ‘n’ vectors do not share the dimension equality required for matrix calculations, and even if they did, would not produce the necessary combination of values necessary for the calculation, instead creating a vector that would likely be the main diagonal of the resulting matrix, rather than the entire matrix, that is necessary for the countourf function. (You can also use mesh or surf if you want to see the ‘Zs’ matrix as a 3D surface, with the same arguments as with contourf.)
Second, I plotted:
contourf(x/100, y/100, Zs)
because, as I mentioned, the ‘X’ and ‘Y’ matrices are 3D, and the matrix plotting functions (e.g. contour, surf, mesh) only accept 2D matrix arguments, although they will take vector arguments for the first two arguments, in order to calculate the axes correctly (so that they use the ‘x’ and ‘y’ vector values for the axis tick values, rather than the matrix index values that would otherwise be the default). I could have done another meshgrid call to create 2D versions of the ‘X’ and ‘Y’ matrices, and then used them in the contourf call, however that is computationally inefficient when contourf can accept vectors for the first two arguments.
John Doe
John Doe am 20 Jul. 2019
Thanks a lot! You are a life-saver!
Star Strider
Star Strider am 20 Jul. 2019
As always, my pleasure!
Thank you!
John Doe
John Doe am 23 Jul. 2019
Bearbeitet: John Doe am 23 Jul. 2019
Hello, Star Strider. Hope you are doing well.
Back again with the continuation of the same problem of contour plotting, but with a different task.
This is another equation which I am trying to plot. Here is the same as my previously posted V(x,y) in the thread.
What I am trying to do is to calculate + in one "sum" function as you showed before and similarly calcalute the j portion with another "sum" function. As this equation deals with central difference approach of Numerical techniques, I have taken "i(x in the code)" to vary from 2 so that is nonzero in the calculation and kept j ( as y in the code) starting from 1. Vice versa while calculating the j portion, so is nonzero. Then add the two "sum" functions together.
Here's my code :
clc
clear all
close all
v0=100;
a=10;
b=5;
x1=2:1000;
y1=1:500;
n=1:2:400;
x2=1:500;
y2=2:1000;
[X1,Y1,N1]=meshgrid(x1/100,y1/100,n);
[X2,Y2,N2]=meshgrid(x2/100,y2/100,n);
Z1 = @(x1,y1,n)((4.*v0./pi).*(sin(n.*pi.*(x1+1)./a).*(sinh(n.*pi.*y1./a)./(n.*sinh(n.*pi.*b./a))))+(4.*v0./pi).*(sin(n.*pi.*(x1-1)./a).*(sinh(n.*pi.*y1./a)./(n.*sinh(n.*pi.*b./a)))));
Z2 = @(x2,y2,n)((4.*v0./pi).*(sin(n.*pi.*x2./a).*(sinh(n.*pi.*(y2+1)./a)./(n.*sinh(n.*pi.*b./a))))+(4.*v0./pi).*(sin(n.*pi.*x2./a).*(sinh(n.*pi.*(y2-1)./a)./(n.*sinh(n.*pi.*b./a)))));
Zs1 = sum(Z1(X1,Y1,N1),3);
Zs2 = sum(Z2(X2,Y2,N2),3);
Z= (1/4)*(Zs1+Zs2);
[C,h]=contourf(x1/100,y1/100,Z);
clabel(C,h);
axis('square')
Again I am getting an matrix sizing error at :
Z= (1/4)*(Zs1+Zs2);
The error says,
Matrix dimensions must agree.
How do I add my Zs1,Zs2 now? I know they have to be the same dimension for the addition. But the workspace says they have different size. Is it due to coding or am I doing any silly mistake? Also, I am kind of confused which "x" should I use in the "contour" function.
Kindly help out if you can.
Thankyou.
In order to make them agree, one way is to transpose ‘Zs2’:
Z = (1/4)*(Zs1+Zs2');
That works, and it’s compatible with the contourf call (although the resulting plot looks strange).
please help me to solve this exercise, thanks a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Contour Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by