Plotting dipole field given in polar coordinates

22 views (last 30 days)
Hello, I'm trying to plot a dipole field around a uniformly polarized sphere.
Here is the setup where u is supposed to represent the field component in r-hat direction and v represents the vector component in theta-hat direction.
[x,y] = meshgrid(-8:0.5:8,-8:0.5:8);
%% for base change
r = sqrt(x.^2 + y.^2); % r in function of (x, y)
theta = atan2(y, x); % theta in function of (x, y)
%% Constants
rad = 2; %radius of sphere
pre = 0.1; %pre factor
scale1=50; %scaling factors for better visibility
scale2=200;
%% Definition of vectors in polar coordinates:
u = ((2*pre)./(r.*r.*r)).*cos(theta); %r-hat
v = (pre./(r.*r.*r)).*sin(theta); %theta-hat
afterwards i go over to plotting the vectors using the quiver function.
I'm not really sure if u*cos(theta) and v*sin(theta) are the right steps here. Basically, I want to plot the vector field given in polar coordinates in cartesian coordinates and I don't know if my transformation steps are correct.
%% plotter
figure(1)
hold on;
for i=1:33
for n=1:33
if((sqrt(x(i,n)*x(i,n)+y(i,n)*y(i,n)))>rad*2)
quiver(x(i,n), y(i,n), u(i,n)*cos(theta(i,n)), v(i,n)*sin(theta(i,n)),scale2,'r')
hold on;
elseif ((sqrt(x(i,n)*x(i,n)+y(i,n)*y(i,n)))>rad)
quiver(x(i,n), y(i,n), u(i,n)*cos(theta(i,n)), v(i,n)*sin(theta(i,n)),scale1,'r')
hold on;
end
end
end
I'm fairly new to matlab so your help is greatly appreciated.
Thank You :)
  2 Comments
Nolan Canegallo
Nolan Canegallo on 24 Jan 2021
Edited: Nolan Canegallo on 24 Jan 2021
I have a few questions in order to verify my solution.
  1. Where are the poles located on your sphere/circle?
  2. What are the base/original equations that you are using?
Philipp Traxler
Philipp Traxler on 24 Jan 2021
Thank You Nolan, that looks really good.
You helped me out a lot!
that's the given equation for the potential and the lower one is the one we care about.
The Electric Field is supposed to resemble a Dipole field as we are looking at a uniformly polarised Sphere.

Sign in to comment.

Accepted Answer

Nolan Canegallo
Nolan Canegallo on 24 Jan 2021
Edited: Nolan Canegallo on 24 Jan 2021
clear; clc; close all;
This is good practice when you are generating figures and troubleshooting to ensure the previous code, variables, and figures are cleared. I added this to the start of your code.
[x,y] = meshgrid(-8:0.5:8,-8:0.5:8);
%% for base change
r = sqrt(x.^2 + y.^2); % r in function of (x, y)
theta = atan2(y, x); % theta in function of (x, y)
%% Constants
rad = 2; %radius of sphere
pre = 0.1; %pre factor
scale1=1; %scaling factors for better visibility (No longer needed)
scale2=1;
%% Definition of vectors in polar coordinates:
u = ((2*pre)./(r.*r.*r)).*cos(theta); %r-hat
v = (pre./(r.*r.*r)).*sin(theta); %theta-hat
I have only changed the scale factors back to one here. I am assuming that the formulas that you provided are correct.
%% Conversion of u,v into x2 and y2
% 2d rotation equations
% Note that the u and v, need to be converted to x and y for quiver to
% display properly
x2 = u.*cos(theta) - v.*sin(theta);
y2 = u.*sin(theta) + v.*cos(theta);
This block converts the u and v coordinates into x and y values using the coordinate rotation equations.
%% plotter
figure(1)
% Plots circle
th = 0:pi/32:2*pi;
plot(rad*cos(th), rad*sin(th), '--b')
hold on;
%plots points farther than 2 radii
inds2 = x.^2 + y.^2 > 2*rad^2;
quiver(x(inds2),y(inds2),x2(inds2),y2(inds2),scale2,'r')
%plots points greater than 1 radii
inds1 = x.^2 + y.^2 > rad^2;
quiver(x(inds1),y(inds1),x2(inds1),y2(inds1),scale1,'r')
%Sets plot aspect ratio and area to properly display data
daspect([1 1 1])
axis tight
I have updated the plot section to display the circle and increase efficiency. The output is below:

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!