Week 3 - Solving second order ODEs
To write code in MATLAB that can animate a simple pendulum motion
Steps:
- Derive the equation of motion
- The position it. The angular displacement at 3, or the ‘x’ as it in the code
- Make a function in another file which called ‘ode_func’
- Plot the angular displacement with respect to Time
- Make an animation of the pendulum motion
During the first (file) Programming
The Objective here is to calculate the parameters that is involved and also plotting it.
- Inserting the inputs: Mass of the ball, Damping coefficient, length of the pendulum, gravity
- Inserting the initial condition
- Inserting the time span points
- Solve ODE45 function
- Plotting the code and animate it
THE YOUTUBE LINK OF THE VIDEO:
https://www.youtube.com/watch?v=7YqsixqmO7Y&feature=youtu.be
Problems or Errors that has been faced during the program:
- Faced minor errors, such as the position of marker of the pendulum
clear all close all clc %Inputs b = 0.05; m =1; l=1 ; g = 9.81; %Initial Condition x0 = [0 2] % Time points t = linspace(0,20,500) %Solve ODE45 [t x] = ode45('ode_func',t,x0); %plotting plot(t,x(:,1)); hold on; plot(t,x(:,2)); xlabel('TIME') ylabel('Angular Displacement') % Animate the motion of the PENDULUM X0 = 0; Y0 = 0; ct =1; for i=1:length(t) X1= l*sin(x(i,1)); Y1= -l*cos(x(i,1)); figure(2) plot([-1 1],[0 0],'linewidth',6,'Color','b'); axis ([-1.5 1.5 -1.5 1.5]); line([X0 X1],[Y0 Y1]); hold on; plot (X0, Y0,'O','markersize',2,'MarkerFaceColor','r'); plot(X1,Y1,'O','markersize',10,'MarkerFaceColor','g'); title('Simple Pendulum'); grid on; hold off; F(ct) = getframe(gcf); ct= ct+1; end movie(F); video = VideoWriter('Simple_Pendulum.avi','Uncompressend.AVI'); open(video) writeVideo(videofile,F) close(video)