i am trying to implement something along thoese lines but i have discs instead of plates , to reach this objective i started from easier problems like superposition and tried to get used to drawing field lines not there yet , and here it seemed like a bit of a jump to try to do this right after but i would like to learn
the use of field graphing tools
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
K = 8.99e9;
% Input the number of charges
n = input('Enter number of charges: ');
% Initialize arrays to store charge values and coordinates
q = zeros(1, n);
x = zeros(1, n);
y = zeros(1, n);
minx = Inf;
maxx = -Inf;
miny = Inf;
maxy = -Inf;
% Input charge values
for i = 1:n
q(i) = input(['Enter the charge in coulombs for charge ', num2str(i), ': ']);
end
% Input coordinates of the charges
for i = 1:n
x(i) = input(['Enter x coordinate for charge ', num2str(i), ': ']);
y(i) = input(['Enter y coordinate for charge ', num2str(i), ': ']);
if x(i) > maxx
maxx = x(i);
end
if x(i) < minx
minx = x(i);
end
if y(i) > maxy
maxy = y(i);
end
if y(i) < miny
miny = y(i);
end
end
% Define the grid for visualization
[X, Y] = meshgrid((minx-1):0.1:(maxx+1), (miny-1):0.1:(maxy+1));
Ex = zeros(size(X));
Ey = zeros(size(X));
% i tried to improve the out put [X, Y] = meshgrid((minx-1):0.01:(maxx+1), (miny-1):0.01:(maxy+1));
%Ex = zeros(size(X));
%Ey = zeros(size(X));
% yet it also lead to the unablitiy to see clearly if there is for example
% a way to change the arrows size or ask the computer for optimal view
% since i am viewing all possible options and most end up badly
% Calculate the electric field components
for i = 1:n
R = ((X - x(i)).^2 + (Y - y(i)).^2 ).^1.5; % Distance cubed
Ex = Ex + K * q(i) * (X - x(i)) ./ R;
Ey = Ey + K * q(i) * (Y - y(i)) ./ R;
end
% Plot the electric field vectors
figure;
quiver(X, Y, Ex, Ey);
xlabel('x (m)');
ylabel('y (m)');
title('Electric Field Vectors');
axis equal;
grid on;
% Plot the potential distribution (if needed)
% Potential at each point
V = zeros(size(X));
for i = 1:n
R = sqrt((X - x(i)).^2 + (Y - y(i)).^2 ); % Distance
V = V + K * q(i) ./ R;
end
figure;
contourf(X, Y, V, 50, 'LineColor', 'none');
colorbar;
xlabel('x (m)');
ylabel('y (m)');
title('Potential Distribution');
axis equal;
grid on;
i wrote the following code and i am trying to improve a couple of things
1 i want the graph to fit the boundaries so it wont draw a grid that has field lines in parts and the rest is grid
2 when i enter a negative point charge it gets problamatic this is physically correct but i want to try and write something to deal with it
3 the field lines are write but they do not really reflect the behavoir well especially when i write something like 3 + and -1 on a destince 1 from each other they look just like 1 and -1 a bit better in the voltage aspect
now this is for improvment that seem okayish yet from my trial and error i could not implement
now the problamtic request is for the next part of the code i am trying to write i am trying to draw a visullization of the field between 2 finite planes
i started this part by writing an approximation for the derivative and checking that it works
h = logspace(-1,-15,100);
real_d = -sin(1);
real_d2 = -sin(1+h);
di_d = (cos(1+h)-cos(1))./h ;
simt_d = (cos(1+h)-cos(1-h))./(2*h) ;
plot (log10(h) , log10(real_d - di_d),'*')
hold on
plot (log10(h) , log10(real_d - simt_d),'*')
hold o
xlabel ("log(h) from x=1")
ylabel ("log(di) from -sin(1)")
i derived the equations i need
yet i am having a hard time trying to implment them
% Constants
d = 0.5; % distance between plates in cm
R = 10.0; % radius of the plates in cm
V = 1.0; % potential difference in V
h = d / 20; % step size in cm
r_max = R + 5 * d; % maximum r value in cm
z_max = 10 * d; % maximum z value in cm
tolerance = 0.0001; % convergence criterion (0.01%)
% Define the grid
r_points = floor(r_max / h) + 1;
z_points = 2 * floor(z_max / h) + 1;
phi = zeros(r_points, z_points);
% Set boundary conditions for the finite plates
for i = 1:floor(R / h) + 1
% Positive plate at z = d/2
phi(i, floor(z_max / h) + 1 + floor(d / (2 * h))) = 0.5 * V;
% Negative plate at z = -d/2
phi(i, floor(z_max / h) + 1 - floor(d / (2 * h))) = -0.5 * V;
end
% Relaxation method
max_diff = tolerance + 1; % Initialize max difference
while max_diff > tolerance
max_diff = 0;
phi_new = phi;
for i = 2:r_points-1
for j = 2:z_points-1
% Skip the points on the plates
if (i * h <= R) && (j == floor(z_max / h) + 1 + floor(d / (2 * h)) || j == floor(z_max / h) + 1 - floor(d / (2 * h)))
continue;
end
phi_new(i, j) = 0.25 * ( ...
phi(i + 1, j) * (1 + h / (2 * (i * h))) + ...
phi(i - 1, j) * (1 - h / (2 * (i * h))) + ...
phi(i, j + 1) + ...
phi(i, j - 1));
max_diff = max(max_diff, abs(phi_new(i, j) - phi(i, j)));
end
end
phi = phi_new;
end
% Plotting the potential
r = 0:h:r_max;
z = -z_max:h:z_max;
[R, Z] = meshgrid(r, z);
figure;
contourf(R, Z, phi', 50, 'LineColor', 'none');
colorbar;
xlabel('r (cm)');
ylabel('z (cm)');
title('Electric Potential in the r-z Plane');
the drawing seems weird too if some one can give a bit of guidenss on the use of relaxation
5 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Interpolation of 2-D Selections in 3-D Grids in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!