I want to animate 3D vectors

7 visualizzazioni (ultimi 30 giorni)
Bilge Kaan Atay
Bilge Kaan Atay il 26 Set 2022
Risposto: Altaïr il 28 Mag 2025
Hello,
I want to demosntrate how changing every single angle would affect the areas. I did some search about Sliders. But it seems hard to build this on MATLAB. Any ideas?
I'm also open to use other online solutions or softwares.

Risposte (1)

Altaïr
Altaïr il 28 Mag 2025
To create an animated visualization of 3D vectors and explore how varying angles influence operating zones, you can leverage MATLAB’s programmatic app-building capabilities. You’ll find a comprehensive list of relevant functions by running:
web(fullfile(docroot, 'matlab/referencelist.html?type=function&s_tid=CRUX_topnav&category=develop-apps-programmatically'))
Below is a complete implementation of the app. It includes sliders for adjusting the characteristic angle, forward and reverse angle limits, and dynamically updates the 3D plot accordingly:
function animate_operating_zones
% Initial angle values
charAngle = 30;
fwdMin = 10;
fwdMax = 40;
revMin = 15;
revMax = 45;
% Create GUI figure
f = figure('Name', 'Operating Zones on Sphere', 'Position', [100 100 1000 600]);
% Axes for 3D plot
ax = axes('Parent', f, 'Position', [0.35 0.1 0.6 0.8]);
axis equal
hold on
grid on
view(3)
xlabel('X'); ylabel('Y'); zlabel('Z');
% Create slider structure
angles = struct();
labels = struct(); % for value text labels
% Slider: Characteristic Angle
uicontrol(f, 'Style', 'text', 'String', 'Characteristic Angle', 'Position', [20 520 200 20]);
uicontrol(f, 'Style', 'text', 'String', '0', 'Position', [20 500 30 20]);
angles.char = uicontrol(f, 'Style', 'slider', 'Min', 0, 'Max', 360, 'Value', charAngle, ...
'Position', [50 500 200 20], 'Callback', @(src,~) update());
uicontrol(f, 'Style', 'text', 'String', '360', 'Position', [250 500 30 20]);
labels.char = uicontrol(f, 'Style', 'text', 'String', num2str(charAngle), 'Position', [280 500 40 20]);
% Slider: Min Forward
uicontrol(f, 'Style', 'text', 'String', 'Min Forward Angle', 'Position', [20 460 200 20]);
uicontrol(f, 'Style', 'text', 'String', '0', 'Position', [20 440 30 20]);
angles.fwdMin = uicontrol(f, 'Style', 'slider', 'Min', 0, 'Max', 90, 'Value', fwdMin, ...
'Position', [50 440 200 20], 'Callback', @(src,~) update());
uicontrol(f, 'Style', 'text', 'String', '90', 'Position', [250 440 30 20]);
labels.fwdMin = uicontrol(f, 'Style', 'text', 'String', num2str(fwdMin), 'Position', [280 440 40 20]);
% Slider: Max Forward
uicontrol(f, 'Style', 'text', 'String', 'Max Forward Angle', 'Position', [20 400 200 20]);
uicontrol(f, 'Style', 'text', 'String', '0', 'Position', [20 380 30 20]);
angles.fwdMax = uicontrol(f, 'Style', 'slider', 'Min', 0, 'Max', 90, 'Value', fwdMax, ...
'Position', [50 380 200 20], 'Callback', @(src,~) update());
uicontrol(f, 'Style', 'text', 'String', '90', 'Position', [250 380 30 20]);
labels.fwdMax = uicontrol(f, 'Style', 'text', 'String', num2str(fwdMax), 'Position', [280 380 40 20]);
% Slider: Min Reverse
uicontrol(f, 'Style', 'text', 'String', 'Min Reverse Angle', 'Position', [20 340 200 20]);
uicontrol(f, 'Style', 'text', 'String', '0', 'Position', [20 320 30 20]);
angles.revMin = uicontrol(f, 'Style', 'slider', 'Min', 0, 'Max', 90, 'Value', revMin, ...
'Position', [50 320 200 20], 'Callback', @(src,~) update());
uicontrol(f, 'Style', 'text', 'String', '90', 'Position', [250 320 30 20]);
labels.revMin = uicontrol(f, 'Style', 'text', 'String', num2str(revMin), 'Position', [280 320 40 20]);
% Slider: Max Reverse
uicontrol(f, 'Style', 'text', 'String', 'Max Reverse Angle', 'Position', [20 280 200 20]);
uicontrol(f, 'Style', 'text', 'String', '0', 'Position', [20 260 30 20]);
angles.revMax = uicontrol(f, 'Style', 'slider', 'Min', 0, 'Max', 90, 'Value', revMax, ...
'Position', [50 260 200 20], 'Callback', @(src,~) update());
uicontrol(f, 'Style', 'text', 'String', '90', 'Position', [250 260 30 20]);
labels.revMax = uicontrol(f, 'Style', 'text', 'String', num2str(revMax), 'Position', [280 260 40 20]);
% Update function
function update()
cla(ax);
[X, Y, Z] = sphere(100);
surf(ax, 0.1*X, 0.1*Y, 0.1*Z, 'FaceAlpha', 0.8, 'EdgeColor', 'none');
axis(ax, [-1 1 -1 1 -1 1]);
xlabel('X'); ylabel('Y'); zlabel('Z');
hold on
% Get current angles
ca = angles.char.Value;
fmin = angles.fwdMin.Value;
fmax = angles.fwdMax.Value;
rmin = angles.revMin.Value;
rmax = angles.revMax.Value;
% Update value displays
labels.char.String = sprintf('%.1f', ca);
labels.fwdMin.String = sprintf('%.1f', fmin);
labels.fwdMax.String = sprintf('%.1f', fmax);
labels.revMin.String = sprintf('%.1f', rmin);
labels.revMax.String = sprintf('%.1f', rmax);
% Directions
zt_axis = [0 1 0]; % Zero torque axis = y-axis
mt_dir = rotate_vector([0 0 1], zt_axis, deg2rad(ca)); % Max torque
mt_dir = mt_dir / norm(mt_dir);
% Plot vectors
quiver3(0, 0, 0, mt_dir(1), mt_dir(2), mt_dir(3), 'r', 'LineWidth', 2, 'MaxHeadSize', 1);
text(mt_dir(1), mt_dir(2), mt_dir(3), 'Max Torque', 'Color', 'r');
quiver3(0, 0, 0, zt_axis(1), zt_axis(2), zt_axis(3), 'b', 'LineWidth', 2, 'MaxHeadSize', 1);
text(zt_axis(1), zt_axis(2), zt_axis(3), 'Zero Torque', 'Color', 'b');
negZ = [0 0 1];
quiver3(0, 0, 0, negZ(1), negZ(2), negZ(3), 'k', 'LineWidth', 2, 'MaxHeadSize', 1);
text(negZ(1), negZ(2), negZ(3), '-U_0', 'Color', 'k', 'VerticalAlignment', 'top');
% Plot sectors
plot_sector(mt_dir, zt_axis, -fmin, fmax, [0 1 0]); % Forward (green)
plot_sector(-mt_dir, zt_axis, rmin, -rmax, [1 0.5 0]); % Reverse (orange)
end
% Initial draw
update();
end
function v_rot = rotate_vector(v, axis, angle)
axis = axis / norm(axis);
v_rot = v*cos(angle) + cross(axis, v)*sin(angle) + axis*dot(axis, v)*(1 - cos(angle));
end
function plot_sector(mt_dir, zt_dir, minAngle, maxAngle, color)
N = 50;
u = mt_dir / norm(mt_dir);
v = zt_dir - dot(zt_dir, u)*u;
if norm(v) < 1e-6
v = null(u(:).');
v = v(:,1);
end
v = v / norm(v);
theta = deg2rad(linspace(minAngle, maxAngle, N));
arc = zeros(N, 3);
for i = 1:N
arc(i, :) = cos(theta(i))*u + sin(theta(i))*v;
arc(i, :) = arc(i, :) / norm(arc(i, :));
end
fill3([0; arc(:,1)], [0; arc(:,2)], [0; arc(:,3)], color, ...
'FaceAlpha', 0.4, 'EdgeColor', 'none');
end
This example creates an interactive MATLAB GUI using uicontrol elements to build sliders that adjust five angle parameters, enabling real-time updates of a 3D visualization. The app uses quiver3 and fill3 to plot vectors and angular zones on a sphere, with the axes object providing full 3D customization including labels, grid, and camera view. As users manipulate the sliders, the update() function dynamically redraws the scene: a unit sphere is rendered, and vectors representing maximum and zero torque directions are plotted. Colored sectors for forward and reverse operating zones are drawn using the plot_sector function, while vector orientations are computed using Rodrigues’ rotation formula implemented in the rotate_vector function. This setup is particularly effective for visualizing torque directions and understanding angular effects interactively.
Here's what the resulting app looks like:
To learn more about the functions used, you can access their documentation directly:
  • uicontrol
web(fullfile(docroot, 'matlab/ref/uicontrol.html'))
  • fill3
web(fullfile(docroot, 'matlab/ref/fill3.html'))
  • quiver3
web(fullfile(docroot, 'matlab/ref/quiver3.html'))

Categorie

Scopri di più su Animation in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by