I have plotted this equation by this :
clear all
close all
a=1000;
b=1000;
v0=100;
V=zeros(1000);
V(:,1000)=0;
V(:,1)=0;
V(1,:)=0;
V(1000,:)=100;
[X,Y]=meshgrid(0.01:0.01:10, 0.005:0.005:5);
for x=1:1000
for y=1:1000
sum=0;
for k=1:111
n=(2*k)-1;
s=((sin(n.*pi.*x./a)).*(sinh(n.*pi.*y./a)))./(n.*sinh(n.*pi.*(b./a)));
sum=sum+s;
end
V(x,y)=(4.*v0./pi).*sum; %here
end
end
figure
[C,h]=contourf(V);
clabel(C,h)
grid on;
axis('square')
The problem is after running I get this sort of plot:
I am supposed to get something like this :
If I swap V(x,y) with V(y,x) [commented in the code], then I get the right axis. What is happening? Are my x,y values getting interchanged or something? How to resolve this issue? Kindly help!
Thanks!

 Réponse acceptée

Star Strider
Star Strider le 31 Juil 2019

2 votes

If you use the meshgrid outputs (with one additional argument, so one additional matrix) you get the result you want. It also avoids the loops and is likely much faster:
a=1000;
b=1000;
v0=100;
[X,Y,K]=meshgrid(1:1000, 1:1000, 1:111);
N = 2*K-1;
s = @(x,y,n) ((sin(n.*pi.*x./a)).*(sinh(n.*pi.*y./a)))./(n.*sinh(n.*pi.*(b./a)));
V = 4*v0/pi*sum(s(X,Y,N),3);
figure
[C,h]=contourf(V);
clabel(C,h)
grid on;
axis('square')
I won’t post the plot image here, because it is the one you are supposed to get, and posting it would be redundant.

6 commentaires

John Doe
John Doe le 1 Août 2019
Modifié(e) : John Doe le 1 Août 2019
Thanks for the reply.
The sum function is useful, but I am curious to know why it is happening like that in "for loop" and is there any solution for it keeping the for loop.
Star Strider
Star Strider le 1 Août 2019
As always, my pleasure.
In your loop, you are incrementing ‘k’ first, then ‘y’, then ‘x’, since the inner loops complete before the outer loops, then saving them in the essentially opposite order. The meshgrid function creates matrices that are transposes of the one your loop creates (that is just how meshgrid works), however the ndgrid function (if you want to experiment with it) would re-create the result of your loop (although much faster), and would produce a result that is the transpose of the meshgrid result.
So the matrix created essentially relies on the indexing order (or the meshgrid or ndgrid function) you choose. [Note that some MATLAB functions (I don’t rmemember just now which ones) actually require meshgrid and will throw an error if you use ndgrid to calculate the matrices.]
Since the ‘problem’ (if it can be called such) is the indexing order, the solution would either be to reverse the indices, or transpose the 2D matrices that result.
John Doe
John Doe le 1 Août 2019
This makes so much sense now! I wish I could upvote you 1000x. Truly a MVP of this community. Thank you!
Star Strider
Star Strider le 1 Août 2019
I very much appreciate your compliment!
As always, my pleasure!
Adam Danz
Adam Danz le 1 Août 2019
Modifié(e) : Adam Danz le 1 Août 2019
"Truly a MVP of this community"
@John Doe, I +1'd for ya ;)
Star Strider
Star Strider le 1 Août 2019
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by