Challenge

Week 3 - Solving second order ODEs

Hi

Here iam attaching the codes of the second order ode pendulam simulation challenge

% This is to solve the second order ode to find the penulum
clear all;
close all;
clc

%Inputs

g = 9.81;
l=1;
m=0.1;
b=0.05;

%Initial condition
theta_0 = [0;3];


%Time points
t_span = linspace(0,20,500);

% solve ODE 
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
T1 = results(:,1);
ct = 1;

for   i=1:length(T1)
%     T1 = t,results(:,1);
%     T2 = t,results(:,2);
    
    x1 = l*sin(T1(i));
    y1 = -l*cos(T1(i));
  %coordinates

    x0 = 0;
    y0 = 0;

  %plotting

    plot([x0 x1],[y0 y1])
    hold on;
    plot(x1,y1,'marker','o','MarkerSize',30,MarkerFaceColor='r',MarkerEdgeColor='b')
    hold off
    axis([-1 1 -1 1])
    pause(0.03)
    M(ct) = getframe(gcf);
    ct=ct+1;
   

end

%movie
movie(M)
videofile = VideoWriter('pendulam_ode.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Here iam attaching the simulated link of what i did

https://drive.google.com/file/d/12asTzlMAnaAq_rFEGZB9qnO8zHa2myss/view?usp=sharing

Here's the explaination for the codes what i had written

here iam attaching the explination of the cods line by line

1. Title of the program
2. Clear all is the command used to clear the program in editor window
3. Close all is the command used to close the inputs in work space
4.

5. Clc is the command used to clear all commands in the command window
6. title of inputs

7.

8. Gravity value entered.is

9. Length of the Link

10. Mass of the ball in kg.

11. co-efficent value entered.

12.

13. Title

14. theta value which wsa given in the matrix form 1 row and 2 column.

15.

16.

17. Title

18. Equation written to find t_span.

19.

20. Title

21. Equaqtion to find [t, results]. In this equation we have an inbuild command which is called as ode45. this ode 45 command means

Solve non-stiff differential equations, medium order method and
ODE45 is for general use.
And the rest of all is the call function command which is written to find the anguiar displacement and the angular velocity.
22. The results column was named as a variable T1
23. It is to enter the counter value.
24.
25. for loop was started in the variable called in the i and it was formed as a equation
26. title
27. title
28.
29. Equation to plot x1

30. Equation to plot y1

31. title

32.

33. value of x0 entered

34. value of y0 entered

35.

36. title

37.

38. ploting the values to make a line

39. hold on command to make the simulation in one figure

40. In this command marker command is used to make ball shape to plot in the end of the length and the marker size is given by marker size command and the ball was filled with color by the command marker face color and also the marker edge was colored dy the command marker edge color.

41. hold off is the command used to control the animation.

42. this command is to fix the axis

43. pause is the command which is to pause the time interval of the animation in other words it controls the speed of the animation by usin the time interval.

44. to get movie from the simulated file first we have to ensure the get frame command and then that get frame is stored in the variable called M(ct) here the ct is the counter variable and it has the incremental value of 1.

45. counter incremental value

46.

47.

48. end of the for loop.

49. movie title

50. movie command

51. video file in this commandf we have a video format and video name.

52. open video file is to open the video file

53. write video file is to write the video in the mat lab storage location.

54. close video file is the command to close the video file.

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.