Mathematica处理若干个点电荷的电位电场的一个程序包
来自 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处理若干个点电荷的电位电场的一个程序包的更多相关文章
- 编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词
#include<iostream> #include<string> #include<vector> using namespace std; int main ...
- Nuget 命令 NuGet 管理项目库
因为可视化库程序包管理器的局限性,有很多需要的功能在界面中无法完成. 以下技巧均需要在"程序包管理器控制台"中使用命令来完成. 一.改变项目目标框架后,更新程序包 当改变项目的目标 ...
- NUGet的诞生与使用
本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...
- Oracle【IT实验室】数据库备份与恢复之四:RMAN(备份与恢复管理器)
RMAN是ORACLE提供的一个备份与恢复的工具,可以用来备份和还原数据库文件. 归档日志和控制文件.它也可以用来执行完全或不完全的数据库恢复. RMAN可以由命令行接口或者 OEM的 Backup ...
- 安装 RPM 包或者安装源码包
安装 RPM 包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下. ...
- Linux 基础 —— RPM
http://liaoph.com/linux-rpm/ 这篇文章主要讲 RPM 软件包管理器的使用. 软件包的演变史 最早期时,软件包是一些可以运行的程序组成的集合,可能还要加上若干配置文件和动态库 ...
- 【转】使用 NuGet 管理项目库-Phil Haack
原文地址:https://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 ...
- 使用 NuGet 管理项目库
使用 NuGet 管理项目库 Phil Haack 本文转载自:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Micros ...
- linux的基本操作(RPM包或者安装源码包)
RPM包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下.所以你要 ...
- [转载]使用 NuGet 管理项目库
原文:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 Mi ...
随机推荐
- jenkins +docker+python接口自动化之jenkins拉取gitee上的代码(四)
1.背景 1.经过我们前面几轮的安装测试,我们已经安装了jenkins容器,python3,以及运行我们python代码所需要的第三方库在requirements.txt文件下统一安装. 2.需求 我 ...
- 实验:STM32-ARDUINO-ESP01采用AT指令,通过MQTT连接上ONENET
1.硬件准备 要求:STM32支持Arduino. 2.程序逻辑结构 3.主流程状态机 4.测试数据抓图 5. 关键程序代码 unsigned char g_ArrTemp[1024]; int AT ...
- Apache Geronimo默认管理密码
网络空间资产搜索: FoFa 弱口令:system/manager 登陆成功! End!!!
- SQL-运算
dual表可以进行运算select * from dual; 算数运算 + - * /select 5 + 6 as aaa ,6 - 3 as bbb ,5 * 7 as ccc ,9/2 as d ...
- ZSTUOJ刷题⑩:Problem B.--零起点学算法103——查找最大元素
Problem B: 零起点学算法103--查找最大元素 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 9951 Solved: 4793 Descri ...
- 解决:pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your PATH. See README file for more information.
问题:使用pytesseract库识别图片中文字时出现报错 代码: import pytesseract from PIL import Image,ImageEnhance img=Image.op ...
- web3招聘信息渠道
[web3招聘信息渠道]随手转发,供参考,供补充. 个人微博:https://weibo.com/u/2434823180 Blog: https://apenote.github.io vx: ap ...
- 【JSON】Python读取JSON文件报错json.decoder.JSONDecodeError的问题
报错 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line * column * ...
- EBI数据库下载数据
EBI网址链接: https://www.ebi.ac.uk 方法:直接从ncbi上面找到想要下载数据的SRR号然后去EBI里面直接搜索即可得到. 底部就是想要下载的fastq文件了: 然后右键复制链 ...
- Spring Boot 中的异步调用
通常我们开发的程序都是同步调用的,即程序按照代码的顺序一行一行的逐步往下执行,每一行代码都必须等待上一行代码执行完毕才能开始执行.而异步编程则没有这个限制,代码的调用不再是阻塞的.所以在一些情景下,通 ...