Last active
May 20, 2023 21:11
-
-
Save SidhBhat/1dc8a2622406811f7580fff7f6147473 to your computer and use it in GitHub Desktop.
Ocatve script to Plot the normalised electrostatic field vectors of a system of charges in 2 dimentions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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]; | |
Q(2).q = -1; | |
Q(2).x = [-sep,0]; | |
% % Uncomment the below lines for additional test charges | |
% Q(3).q = 3; | |
% Q(3).x = [-sep,-sep]; | |
% | |
% Q(4).q = -1; | |
% Q(4).x = [-sep,sep]; | |
% | |
% Q(5).q = 5; | |
% Q(5).x = [0.5,-0.6]; | |
% 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.05; | |
% 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]; | |
for i = 1:number; | |
E += K*Q(i).q*(r - Q(i).x)/norm(r - Q(i).x)^3; | |
endfor | |
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] = meshgrid(tmp); | |
% Allot space for field vector data | |
E = zeros([size(xx),2]); | |
% calculate E | |
for n = 1:box_length | |
for m = 1:box_length | |
tmp = e_field([xx(1,m),yy(n,1)]); | |
tmp = tmp/norm(tmp); | |
E(n,m,1) = tmp(1); | |
E(n,m,2) = tmp(2); | |
endfor | |
endfor | |
% Start plot | |
hold("on"); | |
quiver(xx,yy,E(:,:,1),E(:,:,2),0.5,"-k","filled"); | |
% 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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
:The comments are pretty verbose. But you need to know some basics of
octave. At least enough to define variables and vectors.
Footnotes
This script is not compatible with Matlab as it is. ↩
This one is for two dimensions ↩