the use of field graphing tools

조회 수: 2 (최근 30일)
dareen
dareen 2024년 7월 12일
편집: dareen 2024년 7월 13일
K = 8.99e9;
% Input the number of charges
n = input('Enter number of charges: ');
Error using input
Support for user input is required, which is not available on this platform.
% 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
dareen
dareen 2024년 7월 12일
i have an idea for impletaion i set the boundries down as zero and zero in the first column i sart calculating from a guess then updating the first column with the new calculated potintial and so on yet did not reach satisfactory results
dareen
dareen 2024년 7월 12일
편집: dareen 2024년 7월 12일
@Walter Roberson yeah i am mostly not closed on if i am implementing this part correctly
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;
i derivied the expression then checked it online tried to write a code that updates the potential each run until it starts to stablize yet still trying to get the hang of the implementaion , i even tried to draw field line to feel better about how this looks yet the field lines were compact and hard to see anything thats happening same thing with that additional part one i did to get used to this is a new field in matlan, the results felt like i only had 2 plates and line between them looking now it seems density out side is diffrent which is a good sign , but with harder to visulize fields it is harder to feel confident with the lines i get when in part one they did not really give the results i amied for excactly for things like e3 and -e ,

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation of 2-D Selections in 3-D Grids에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by