来自 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. nacos之服务注册、发现及维持心跳

    注册服务(增加健康检查)  服务发现(默认15s没有心跳请求,则自动注销服务)  心跳(维持服务运行状态) 参数说明 serviceName:服务名称 beat:服务的详细信息 ip 端口等,json ...

  2. 记一次mybatis中like 不执行sql的问题,单引号 与双引号的坑

    使用环境: springboot 2.0.4 mybatis-spring:1.3.2 错误也是比较蛋疼:  不报错,但是查询出来的结果不对. 原错误sql 示意:  select  * from  ...

  3. Java基础学习:3、数组

    1.三种声明方式: // 创建一个double数据类型的数组,长度为5 double array[] = new double[5]; // 创建int数据类型数组,长度为6 int age[] = ...

  4. Spring入门之IoC 的概念和作用(02)

    2.1 程序的耦合和解耦 2.1.1 程序的耦合耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的 ...

  5. Vue3注册全局组件

    1. Vue3全局组件注册 components文件夹下新建index.js文件,统一引入需要注册的组件 import CustomCheck from "./CustomCheck.vue ...

  6. 操作系统|03.Linux基础(2)

    Linux基础(二) 3.vim编辑器 命令模式:控制光标移动,对文本进行删除.复制.粘贴等工作.编辑模式:正常的文本录入.末行模式:保存.退出和设置编辑环境. 3.1命令模式常用命令 -r:恢复系统 ...

  7. 【Hive】数据倾斜原因及解决方法汇总

    1)数据倾斜根本原因:由于数据分布不均匀,导致map端读取的数据分布不均匀(数据长尾分布),从而使得map处理的数据量差异过大. (2)解决思路:Hive是分阶段执行的,map处理数据量的差异取决于上 ...

  8. vs2019 debug 出现: printf is ambiguous

    在vs中写c++代码时,莫名其妙出现:printf is ambiguous 的错误. 第一步,有设置std namespace 删除后再输入 using namespace std; 第二步.删除u ...

  9. redis面试题汇总

    1redis持久化机制 redis是一个支持持久化的内存数据库,通过持久化机制把内存中的数据同步到硬盘文件来保证数据持久化,当redis重启后通过把硬盘文件重新加载到内存,就能达到恢复数据的目的 2缓 ...

  10. div 自动滚轮2

    HTML: <div class="cal_bottom" style="height:78%;margin-top:2%;overflow:auto;" ...