Skip to content

Instantly share code, notes, and snippets.

@SidhBhat
Last active May 20, 2023 21:31
Show Gist options
  • Save SidhBhat/592caa713b8252e4ab8b0849e9cc350e to your computer and use it in GitHub Desktop.
Save SidhBhat/592caa713b8252e4ab8b0849e9cc350e to your computer and use it in GitHub Desktop.
Ocatve script to Plot the normalised electrostatic field vectors of a system of charges in 3 dimentions
clear all;
clf;
global Q;
global K;
% --------- User Configuration --------- %
%% Charges are defined as structures 'Q' containing two fields 'q' and 'x'
%% specifying the charge and position position is given as a three dimensional
%% row vector Multiple charges can be defined by creation an array of these
%% structures.
%% Define Charges
%% The default setup is an electric dipole.
%% The separation between charges
sep = 0.1;
Q(1).q = 1;
Q(1).x = [sep,0,0];
Q(2).q = -1;
Q(2).x = [-sep,0,0];
% % Uncomment the below lines for additional test charges
% Q(3).q = 3;
% Q(3).x = [-sep,-sep,0];
%
% Q(4).q = -1;
% Q(4).x = [-sep,sep,0];
%
% Q(5).q = 5;
% Q(5).x = [0.5,-0.6,0];
% Coulombs constant, It really does not matter what you set here, as long
% as it is non zero
K = 1;
% This defines the plot limits. Specifically the box if a square box with
% x,y limits between -plot_len to plot_len.
plot_len = 1;
% The density of the grid of points where the field vectors should be plotted
density = 0.25;
% The size of the smallest charge
marker_size = 20;
% --------- End Configuration --------- %
%% Everything below this line is not meant to be normally edited by
%% the user unless you know what you are doing.
global number = uint8(length(Q)); % Number of charges
% calculate field at point r
function E = e_field(r)
global Q;
global K;
global number;
E = [0,0,0];
for i = 1:number;
E += K*Q(i).q*(r - Q(i).x)/norm(r - Q(i).x)^3;
endfor
endfunction
% normalise a vector.
function ret = normalise(v)
ret = v/norm(v);
endfunction
% number of elements in a given dimension of the plot window
box_length = uint16(length(tmp = -plot_len:density:plot_len));
% Define the meshgrid
[xx, yy, zz] = meshgrid(tmp);
% Allot space for field vector data
E = zeros([size(xx),3]);
% calculate E
for n = 1:box_length
for m = 1:box_length
for p = 1:box_length
tmp = e_field([xx(1,m,1),yy(n,1,1),zz(1,1,p)]);
tmp = normalise(tmp);
E(n,m,p,1) = tmp(1);
E(n,m,p,2) = tmp(2);
E(n,m,p,3) = tmp(3);
endfor
endfor
endfor
% Start plot
hold("on");
h = quiver3(xx,yy,zz,E(:,:,:,1),E(:,:,:,2),E(:,:,:,3),0.5,"k","filled","linewidth", 1);
set (h, "maxheadsize", 0.25);
% smallest charge.
smallest_Q = abs(Q(1).q);
for i = 1:number;
if(smallest_Q > abs(Q(i).q))
smallest_Q = abs(Q(i).q);
endif
endfor
% Plot markers for the charges
for i = 1:number;
if(Q(i).q > 0)
plot(Q(i).x(1),Q(i).x(2),".r","markersize",abs(Q(i).q)/smallest_Q*marker_size);
else
plot(Q(i).x(1),Q(i).x(2),".b","markersize", abs(Q(i).q)/smallest_Q*marker_size);
endif
endfor
% define limits and axis.
plot_len += 0.1;
axis("off","equal",[-plot_len,plot_len,-plot_len,plot_len]);
grid("off");
xlabel("x");
ylabel("y");
zlabel("z");
@SidhBhat
Copy link
Author

Introduction

This GNU octave1 script plots the electrostatic field vectors2
of a system of charges that you define.

Usage

Usage is exactly as with my other script Just edit the section titled
User Configuration:

% --------- User Configuration --------- %
%% Charges are defined as structures 'Q' containing two fields 'q' and 'x'
%% specifying the charge and position position is given as a two dimensional
%% row vector. Multiple charges can be defined by creation an array of these
%% structures.
%% Define Charges
%%   The default setup is an electric dipole.

%% The separation between charges
sep = 0.1;

Q(1).q = 1;
Q(1).x = [sep,0];

% .....

The comments are pretty verbose. But you need to know some basics of
octave. At least enough to define variables and vectors.

Footnotes

  1. This script is not compatible with Matlab as it is.

  2. This one is for three dimensions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment