0% found this document useful (0 votes)
10 views6 pages

Document

The document contains MATLAB code for animating a 3D parametric curve by varying the parameter k from 0 to 4π. It defines the parameter t and creates a 3D plot where the x, y, and z coordinates are calculated based on t and k values. The animation updates the plot in real-time, allowing for interactive 3D rotation after completion.

Uploaded by

SHARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Document

The document contains MATLAB code for animating a 3D parametric curve by varying the parameter k from 0 to 4π. It defines the parameter t and creates a 3D plot where the x, y, and z coordinates are calculated based on t and k values. The animation updates the plot in real-time, allowing for interactive 3D rotation after completion.

Uploaded by

SHARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

% Define the range of the parameter t

t = linspace(-5, 5, 200); % Reduced points for faster animation per k

% Define the range of the parameter k to vary from 0 to 4*pi

k_values = linspace(0, 4*pi, 50); % Adjust the number of k values as needed

% Create a figure and axes for the 3D plot

figure;

ax = axes();

view(3); % Set the 3D view

grid on;

title('Animating 3D Parametric Curve by Varying k (0 to 4\pi)');

xlabel('X');

ylabel('Y');

zlabel('Z');

% Initialize an empty line object

h = animatedline(ax, 'Color', 'blue', 'LineWidth', 1.5);

% Loop through the k values

for j = 1:length(k_values)

k_current = k_values(j);

% Clear the previous animated line

clearpoints(h);
% Loop through the t values and update the animated line for the current
k

for i = 1:length(t)

% Calculate the current x, y, and z coordinates

x_current = t(i) + sin(40 * t(i));

y_current = -t(i) + cos(40 * t(i));

z_current = sin(t(i) + k_current);

% Add the current point to the animated line

addpoints(h, x_current, y_current, z_current);

% Update the plot

drawnow limitrate; % Use limitrate for potentially faster updates

% Pause briefly to control the animation speed (optional)

pause(0.005);

end

% Pause slightly longer between each k value (optional)

pause(0.1);

end

% Enable interactive 3D rotation after the animation

rotate3d on;

% Define the range of the parameter t


t = linspace(-5, 5, 200); % Reduced points for faster animation per k

% Define the range of the parameter k to vary from 0 to 4*pi

k_values = linspace(0, 4*pi, 50); % Adjust the number of k values as needed

% Create a figure and axes for the 3D plot

figure;

ax = axes();

view(3); % Set the 3D view

grid on;

title('Animating 3D Parametric Curve by Varying k (0 to 4\pi)');

xlabel('X');

ylabel('Y');

zlabel('Z');

% Initialize an empty line object

h = animatedline(ax, 'Color', 'blue', 'LineWidth', 1.5);

% Loop through the k values

for j = 1:length(k_values)

k_current = k_values(j);

% Clear the previous animated line

clearpoints(h);

% Loop through the t values and update the animated line for the current
k
for i = 1:length(t)

% Calculate the current x, y, and z coordinates

x_current = t(i) + sin(20 * t(i));

y_current = -t(i) + cos(40 * t(i));

z_current = sin(t(i) + k_current);

% Add the current point to the animated line

addpoints(h, x_current, y_current, z_current);

% Update the plot

drawnow limitrate; % Use limitrate for potentially faster updates

% Pause briefly to control the animation speed (optional)

pause(0.005);

end

% Pause slightly longer between each k value (optional)

pause(0.1);

end

% Enable interactive 3D rotation after the animation

rotate3d on;

% Define the range of the parameter t

t = linspace(-5, 5, 500); % Adjust the range and number of points as needed

% Set a constant value for k (you can change this if you want to vary k)

k = 0;
% Create a figure and axes for the 3D plot

figure;

ax = axes();

view(3); % Set the 3D view

grid on;

title('Animating 3D Parametric Curve');

xlabel('X');

ylabel('Y');

zlabel('Z');

% Initialize an empty line object that will be updated in the animation

h = animatedline(ax, 'Color', 'blue', 'LineWidth', 1.5);

% Loop through the t values and update the animated line

for i = 1:length(t)

% Calculate the current x, y, and z coordinates

x_current = t(i) + sin(20 * t(i));

y_current = -t(i) + cos(20 * t(i));

z_current = sin(t(i) + k);

% Add the current point to the animated line

addpoints(h, x_current, y_current, z_current);

% Update the plot

drawnow;
% Pause briefly to control the animation speed (optional)

pause(0.01);

end

% Enable interactive 3D rotation after the animation

rotate3d on;

You might also like