Saturday, August 13, 2016

Matlab code to plot field for 2 point charges

Contents

% This matlab code is used to plot field for 2 point charges,
%minhanhnguyen@q.com
% Clear the screen
clc;
clear all;
% close all windows
close all;

input info.

Dimensions
dimension=1;
d=dimension;
% Charge Value
charge =1;
q=charge;

% Creates the array of points for plotting v
[x,y]=meshgrid([-d:d/33:d],[-d:d/33:d]);

% Evaluate v using Coulombs Law
v=-q./(4.*pi.*sqrt((x+d./2).^2+(y+d./2).^2))+q./(4.*pi.*sqrt((x-d./2).^2+(y-d./2).^2));

Plot result of equipotential contours at high resolution and countours

figure (1);
contour(v,[-1:.005:1]);
title('equipotential contours at high resolution');

%plot contours at an elevation proportional to voltage
figure;
contour3(v,[-1:.005:1]);
title('contours at elevation proportional to voltage for high resolution');


plot equipotential contours at lower resolution to see individual contours easier

figure (2);
contour(v,[-1:.05:1]);
title('equipotential contours at low resolution');

%%plot contours at an elevation proportional to voltage
figure;
contour3(v,[-1:.05:1]);
title('contours at elevation proportional to voltage');

plots all four graph on the same page for comparison

figure (3);
subplot(2,2,1);contour(v,[-1:.005:1]);
title('equipotential contours at high resolution');
subplot(2,2,2);contour3(v,[-1:.005:1]);
title('contours at elevation proportional to voltage for high resolution');
subplot(2,2,3);contour(v,[-1:.05:1]);
title('equipotential contours at low resolution');
subplot(2,2,4);contour3(v,[-1:.05:1]);
title('contours at elevation proportional to voltage');


Published with MATLAB® R2015a


No comments:

Post a Comment