Challenge

Week 3 - Solving second order ODEs

AIM:

To animate the motion of the pendulum bob by solving second order ode.

OBJECTIVE:

Animation of the pendulum.

GOVERNING EQUATION:

Let’s convert the above second-order differential equation into a first-order differential equation.

FUCTION CODE:

  1. function [dtheta_dt] = ode_fun(t,theta,b,g,l,m)
  2. theta1 = theta(1)
  3. theta2 = theta(2)
  4. dtheta1_dt = theta2
  5. dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1)
  6. dtheta_dt = [dtheta1_dt;dtheta2_dt]
  7. end

The above governing equation is converted into a function

MAIN CODE:

  1. clear all
  2. close all
  3. clc
  4. %initial and boundary conditions
  5. b = 0.1; %damping coefficient kg/s
  6. g = 9.81; %acceleration due to gravity m/s
  7. l = 1; %length of the cord in m
  8. m = 0.1; %mass of the bob in kg
  9. theta_0 = [0;5];
  10. t_span = linspace(0,10,100);
  11. % solver
  12. [t,results] = ode45(@(t,theta)ode_fun(t,theta,b,g,l,m),t_span,theta_0);
  13. %ploting
  14. figure(1)
  15. plot(t,results(:,1))
  16. hold on
  17. plot(t,results(:,2))
  18. hold off
  19. ylabel('angular displacement')
  20. xlabel('time')
  21. %animating using the data
  22. ct = 1;
  23. for i = 1:length(results(:,1))
  24. theta = results(i,1);
  25. x0 = 0;
  26. y0 = 0;
  27. x1 = 1*sin(theta);
  28. y1 = -1*cos(theta);
  29. %plotting the bob
  30. figure(2)
  31. plot([-1,1],[0,0],"b-","linewidth",5)
  32. hold on
  33. plot([x0,x1],[y0,y1],"b-","linewidth",2)
  34. axis([-2 2 -2 2])
  35. plot(x1,y1,"o","MarkerSize",20,"MarkerFaceColor","r")
  36. hold off
  37. grid on
  38. axis([-2 2 -2 2])
  39. pause(0.01)
  40. M(ct) = getframe(gcf);
  41. ct = ct+1;
  42. end
  43. %making plots into video
  44. Video = VideoWriter("pedulum_animation");
  45. open(Video)
  46. writeVideo(Video,M)
  47. close(Video)

EXPLAINING THE CODE:

  1. Load the initial conditions.
  2. Ode45 is a solver that uses range kutta method to solve first order differential equation. The initial conditions and boundary conditions are given to the solver. The function that is crated from the first order differential equation is also mentioned.
  3. We plot the results with respective to t.
  4. Now to animate the pendulum we have to plot the every position the bob has been to do that we use for loop. The values of the results are used as inputs for the loop and the position of the bob is determined and plotted. We save the plots in a file using getframe function.
  5. Videowriter function is used to video data from an array.
  6. writeVideo is used to write video into a file.

ERRORS:

There are no major errors occurred.

RESULTS:

The animation is complete and the link is here

https://youtu.be/K0_yC6o2aek

the plot figure 1

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.