How can I animate springs in MATLAB?

15 visualizzazioni (ultimi 30 giorni)
Srikar V.S
Srikar V.S il 29 Mar 2023
Risposto: Star Strider il 29 Mar 2023
I'm trying to animate a simple spring-mass system vibrating freely. I managed to animate a block vibrating according to the response of a free vibration using the method of continuos plots using a for loop. Now, i want to include the spring in the system so as to complete the movie. However, I'm having a hard time trying to do that, I'm a beginner in MATLAB, so I don't know much about the various functions available in it. I couldn't get the right source to help me out.

Risposta accettata

Star Strider
Star Strider il 29 Mar 2023
One approach —
L = 1600; % Vector Lengths
t = linspace(0, 16*pi-(1/L), L); % Eight Cycles
r = 1; % Radius
x = r*cos(t); % X-Coordinate
y = r*sin(t); % Y-Coordinate
tz = linspace(0, t(end), fix(L/2)); % Length Governs Frequency Of Oscillation
zv = (1+sin(tz))*4; % Oscillation
figure
for k = 1:numel(z)
z = linspace(0, zv(k), L)+2; % Define Spring Length For This Cycle
plot3(x, y, z, 'LineWidth',2.5) % Draw Spring
axis([-1.5 1.5 -1.5 1.5 0 10]) % Fix Axis Limits
grid on % Draw Grid
drawnow % Animate
end
It does not show the animation here, however it does work.
Experiment with it to get the result you want. (I did my best to document it in the comments.) In the loop, the ‘z’ value controls the length for each iteration, using the ‘zv’ value, however ‘zv’ can be anything. It does not have to be the vector I used here to test the code, so in reality it will be related to something in your animation. I have drawn it vertically here, however by changing the order of the plot3 call arguments, you can put it in different orientations. You can also use the rotate function to put it in other orientations, if necessary.
.

Più risposte (0)

Categorie

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

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by