来自 Mathematica for Theoretical Physics Electrodynamics, Quantum Mechanics, General Relativity and Fractals by Gerd Baumann这本书

程序包

PointCharge.wl(修改版,适用Mathematica12.0)

做的修改有:

删除了Needs["Graphics`PlotField`"];,12.0版本已经内置这些

弃用GradientFieldPlot这一过时的函数,改为使用StreamPlot函数

BeginPackage["PointCharge`"];
(* --- load additional standard packages --- *)
Clear[Potential,Field,EnergyDensity,FieldPlot];
(* --- export functions --- *)
Potential::usage = "Potential[coordinates_List]
creates the potential of
an assembly of point charges. The cartesian
coordinates of the locations of
the charges are given in the form of
{{x,y,z,charge},{x,y,z,charge},...}.";
Field::usage = "Field[coordinates_List] calculates
the electric field for
an ensemble of point charges. The cartesian
coordinates are
lists in the form of {{x,y,z,charge},{...},...}.";
EnergyDensity::usage =
"EnergyDensity[coordinates_List] calculates the
density of the energy for an ensemble of point
charges. The cartesian
coordinates are lists in the form of
{{x,y,z,charge},{...},...}.";
FieldPlot::usage =
"FieldPlot[coordinates_List,typ_,options___] creates
a
ContourPlot for an ensemble of point charges. The
plot type (Potential,
Field, or Density) is specified as string in the
second input variable. The
third argument allows a change of the Options of
ContourPlot and
PlotGradientField.";
(* --- define the global variables x,y,z --- *)
x::usage;
y::usage;
z::usage;
Begin["`Private`"];
(* --- determine the potential --- *)
Potential[coordinates_List]:=
Block[{x,y,z},
Fold[Plus,0,Map[(#[[4]]/Sqrt[(x-#[[1]])^2 +
(y-#[[2]])^2 +
(z-#[[3]])^2])&, coordinates]]];
(* --- calculate the field ---*)
Field[coordinates_List]:=
Block[{field,x,y,z},
field = -
Fold[Plus,0,Map[(#[[4]]*({x,y,z}-Take[#,3])/
(Sqrt[(x-#[[1]])^2 +
(y-#[[2]])^2 +
(z-#[[3]])^2
])^3)&,coordinates]];
Simplify[field]
];
(* --- calculate the energy --- *)
EnergyDensity[coordinates_List]:=
Block[{density,x,y,z,field},
field = Field[coordinates];
density = field.field/(8*Pi)
];
(* --- create plots --- *)
FieldPlot[coordinates_List,typ_,options___]:=
Block[
{pot, ncharges, xmin, xmax, zmin, zmax, xcoord
= {}, zcoord = {},
pl1, pl2},
ncharges = Length[coordinates];
(* --- determine limits for the plot --- *)
Do[
AppendTo[xcoord,coordinates[[i,1]]];
AppendTo[zcoord,coordinates[[i,3]]],
{i,1,ncharges}];
xmax = Max[xcoord]*1.5;
zmax = Max[zcoord]*1.5;
xmax = Max[{xmax,zmax}];
zmax = xmax;
xmin = -xmax;
zmin = xmin;
Clear[xcoord,zcoord];
(* --- fix the type of the plot ---*)
If[typ == "Potential",pot =
Potential[coordinates] /. y -> 0,
If[typ == "Field",pot =
-Potential[coordinates] /. y -> 0,
If[typ == "EnergyDensity",pot =
EnergyDensity[coordinates] /. y -> 0,
Print[" "];
Print[" wrong key word! Choose "];
Print[" Potential, Field or EnergyDensity "];
Print[" to create a plot "];
Return[]
]]];
(* --- plot the pictures --- *)
If[typ == "Field",
pl1 =
StreamPlot[
Evaluate@{-D[pot, {x}], -D[pot, {z}]}, {x, xmin, xmax}, {z, zmin,
zmax}],
pl1=
ContourPlot[pot,{x,xmin,xmax},{z,zmin,zmax},
options,
PlotPoints->50,
ColorFunction->Hue,
Contours->15]
]
];
End[];
EndPackage[];

测试

所用mma版本号12.0

Mathematica处理若干个点电荷的电位电场的一个程序包的更多相关文章

  1. 编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词

    #include<iostream> #include<string> #include<vector> using namespace std; int main ...

  2. Nuget 命令 NuGet 管理项目库

    因为可视化库程序包管理器的局限性,有很多需要的功能在界面中无法完成. 以下技巧均需要在"程序包管理器控制台"中使用命令来完成. 一.改变项目目标框架后,更新程序包 当改变项目的目标 ...

  3. NUGet的诞生与使用

    本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...

  4. Oracle【IT实验室】数据库备份与恢复之四:RMAN(备份与恢复管理器)

    RMAN是ORACLE提供的一个备份与恢复的工具,可以用来备份和还原数据库文件. 归档日志和控制文件.它也可以用来执行完全或不完全的数据库恢复. RMAN可以由命令行接口或者 OEM的 Backup ...

  5. 安装 RPM 包或者安装源码包

    安装 RPM 包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下. ...

  6. Linux 基础 —— RPM

    http://liaoph.com/linux-rpm/ 这篇文章主要讲 RPM 软件包管理器的使用. 软件包的演变史 最早期时,软件包是一些可以运行的程序组成的集合,可能还要加上若干配置文件和动态库 ...

  7. 【转】使用 NuGet 管理项目库-Phil Haack

    原文地址:https://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 ...

  8. 使用 NuGet 管理项目库

    使用 NuGet 管理项目库 Phil Haack 本文转载自:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Micros ...

  9. linux的基本操作(RPM包或者安装源码包)

    RPM包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下.所以你要 ...

  10. [转载]使用 NuGet 管理项目库

    原文:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 Mi ...

随机推荐

  1. Java-String常用API

    返回值类型 方法 用途 备注 char charAt(int index) 返回 char指定索引处的值.   int compareTo(String anotherString) 按字典顺序比较两 ...

  2. python爬虫实战——自动下载百度图片(文末附源码)

    用Python制作一个下载图片神器 前言 这个想法是怎么来的? 很简单,就是不想一张一张的下载图片,嫌太慢. 在很久很久以前,我比较喜欢收集各种动漫的壁纸,作为一个漫迷,自然是能收集多少就收集多少.小 ...

  3. mysql自动生成时间

    字段类型: DATETIME 默认值: CURRENT_TIMESTAMP

  4. Java基础|02.方法的传递机制

    Java基础|02.方法传参机制 0x00 前言 Parameter:参数 java中只有传值,因为地址值也是值 0x01 基础数据类型的传参机制 基本数据类型的值传递,不改变原值.因为调用后会弹栈, ...

  5. 常用功能系列---【JWT生成Token实现接口登录认证方案思路】

    JWT生成Token实现接口登录认证方案思路 方案一(双token实现无感刷新) 在token中,refreshToken的作用主要是避免token过期时,前端用户突然退出登录,跳转至登录页面. 但是 ...

  6. kubeadm搭建k8s

    一.kubeadm 部署 K8S 集群架构 主机名 IP地址 安装组件 master(2C/4G,cpu核心数要求大于2) 192.168.160.20 docker.kubeadm.kubelet. ...

  7. Linux(2)

    虚拟机关键配置名词解释 远程链接工具 linux准则 远程链接工具快捷键 系统相关命令 文件相关命令 linux目录结构 虚拟机关键配置名词解释 # 虚拟网络编辑器说明 桥接模式  # 可以访问互联网 ...

  8. VMware linux 网络设置

    控制面板\所有控制面板项\网络连接 1.选择 VMware Virtual Ethernet Adapter for VMnet8 网卡  ->属性-->网络 2.勾选 -> VMw ...

  9. vue--v-model 的三种修饰符lazy、number、trim

    Vue--v-model的三种修饰符lazy.number.trim v-model.lazy:  值修改操作完成之后才会发生变化. v-model.number: 只修改时,保持其值为Number类 ...

  10. beamforming源码标记

    p:各阵元的声压信号矩阵 R:接收数据的自协方差矩阵 Pcbf:交叉谱矩阵