how to do in code a type of graph and vectors

2 次查看(过去 30 天)
Hello, i would need some help in order to code the following

回答(1 个)

Satyam
Satyam 2025-4-24
Hi Álvaro,
According to my understanding you are trying to plot a vector diagram of electric dipole in the presence of a uniform electric field. For that purpose, we can first start by initializing necessary variables like Electric field magnitude and angle as well as magnitude of charge in dipole and the distance of separation between them. Then, utilizing the appropriate formulas we can determine the positions of vectors in the plot. We can then utilize 'quiver' plot to display these vectors. You can refer to the following documentation to know about the syntax of using 'quiver' plot: https://www.mathworks.com/help/matlab/ref/quiver.html
Here is code to explain the above approach.
% Parameters
d = 2; % Separation between charges
q = 0.5; % Charge magnitude
origin = [0, 0]; % Center of dipole
theta = pi/3; % Angle of dipole with x-axis
% Dipole vector
p_mag = q * d;
p_vec = p_mag * [cos(theta), sin(theta)];
% Positions of charges
r1 = origin + 0.5 * d * [cos(theta), sin(theta)]; % +q
r2 = origin - 0.5 * d * [cos(theta), sin(theta)]; % -q
% Uniform electric field (along x-axis)
E_mag = 1.2;
E_angle = pi/6; % x-direction
E_vec = E_mag * [cos(E_angle), sin(E_angle)];
% Force on each charge: F = qE
F_vec = q * E_vec;
figure; hold on; axis equal;
% Plot electric field lines (parallel arrows)
y_range = linspace(-1.5,2.5,7);
x_field = -2:0.5:2.5;
for j = 1:length(y_range)
for i = 1:length(x_field)
quiver(x_field(i), y_range(j), E_vec(1)*0.5, E_vec(2)*0.5, 0, ...
'g', 'LineWidth', 1.2, 'MaxHeadSize', 0.5);
end
end
% Plot dipole axis
plot([r2(1) r1(1)], [r2(2) r1(2)], 'k--');
% Plot charges
plot(r1(1), r1(2), 'ro', 'MarkerSize', 10, 'LineWidth', 2); % +q
plot(r2(1), r2(2), 'bo', 'MarkerSize', 10, 'LineWidth', 2); % -q
% Plot dipole moment vector (p)
quiver(r2(1), r2(2), p_vec(1), p_vec(2), 0, 'blue', 'LineWidth', 2, 'MaxHeadSize', 0.6);
text(r2(1)+0.5*p_vec(1), r2(2)+0.5*p_vec(2)+0.2, '\bf\itp', 'FontSize', 14);
% Plot force at +q (r1) due to E
quiver(r1(1), r1(2), F_vec(1)*0.7, F_vec(2)*0.7, 0, 'm', 'LineWidth', 2, 'MaxHeadSize', 0.6);
text(r1(1)+F_vec(1)*0.8, r1(2)+F_vec(2)*0.2, '\bf\itF', 'FontSize', 14, 'Color', 'm');
% Plot force at -q (r2), opposite direction
quiver(r2(1), r2(2), -F_vec(1)*0.7, -F_vec(2)*0.7, 0, 'm', 'LineWidth', 2, 'MaxHeadSize', 0.6);
text(r2(1)-F_vec(1)*0.9, r2(2)-F_vec(2)*0.2, '\bf-\itF', 'FontSize', 14, 'Color', 'm');
% Labels for charges
text(r1(1)+0.1, r1(2)+0.1, '+q', 'FontSize', 12, 'Color', 'r');
text(r2(1)+0.1, r2(2)+0.1, '-q', 'FontSize', 12, 'Color', 'b');
% Label one E field vector
text(-1.1, 2.5, '\bf\itE', 'FontSize', 14, 'Color', 'g');
axis([-2 3 -2 3]);
grid on;
hold off;
Hope it solves your query!

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by