How to apply sum function for simulating charges around a circle?

I'm trying to model electric potential energy of protons arranged in a circle, graphically is(the next plot was made manually in geogebra 3d),
For :
Asumming k=1, q1=1, q2=2 , mathematically can been described as:
The thing is that i want to generalize this into a summation function in matlab for n charges separated in the circle, mathematically it will be:
I try the next code, but it doesn't work. Any suggestions? I can't understand how the sum function works exactly:
[x,y,t]=meshgrid(-15:0.5:15,-15:0.5:15, 0:pi/8:pi/2);
r=5;
z=sum(1./sqrt((x-r*cos(t)).^2+(y-r*sin(t).^2)), 3);
surf(x(:,:,1),y(:,:,1),z);

回答 (2 件)

Matt J
Matt J 2021 年 3 月 9 日
編集済み: Matt J 2021 年 3 月 9 日
xs=linspace(0,10,500);
[x,y,t]=meshgrid(xs,xs, 0:pi/8:pi/2);
r=5;
z=sum(1./ ( (x-r*cos(t)).^2 + (y-r*sin(t)).^2 ), 3);
surf(x(:,:,1),y(:,:,1),z,'EdgeColor','none','FaceAlpha',0.3,'FaceColor','g');

6 件のコメント

Rodrigo Ayala
Rodrigo Ayala 2021 年 3 月 9 日
Anyways that doesn't gives me the output that i expect.
Matt J
Matt J 2021 年 3 月 9 日
It looks quite similar to the picture you posted.
Matt J
Matt J 2021 年 3 月 9 日
編集済み: Matt J 2021 年 3 月 9 日
To conserve memory, use ndgridVecs instead,
xs=linspace(0,8,400);
[y,x,t]=ndgridVecs(xs,xs, 0:pi/8:pi/2);
r=5;
z=sum(1./ ( (x-r*cos(t)).^2 + (y-r*sin(t)).^2 ), 3);
surf(x,y,z,'EdgeColor','none','FaceAlpha',0.3);
Rodrigo Ayala
Rodrigo Ayala 2021 年 3 月 9 日
Thats looks good, but why i doesn't seems like the geogebra 3d? There must be some type of error. It should look like this:
Matt J
Matt J 2021 年 3 月 9 日
編集済み: Matt J 2021 年 3 月 9 日
Is the same r being used in the geogebra drawing? Also, you've presented different versions of your equations in different places, some with sqrt()'s and some without. Including or removing sqrt would affect the sharpness of the peaks.
Here's what we get with r=1 and including the sqrt():
r=1;
xs=linspace(0,1.5*r,500);
[y,x,t]=ndgrid(xs,xs, 0:pi/8:pi/2);
z=sum(1./ sqrt( (x-r*cos(t)).^2 + (y-r*sin(t)).^2 ), 3);
surf(x(:,:,1),y(:,:,1),z,'EdgeColor','none','FaceAlpha',0.3);
zlim([0,30]); view([-45,30])
axis vis3d
grid off
Rodrigo Ayala
Rodrigo Ayala 2021 年 3 月 9 日
Oh thanks, that is the expect output!

サインインしてコメントする。

カテゴリ

質問済み:

2021 年 3 月 9 日

コメント済み:

2021 年 3 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by