Challenge

Week 3 - Solving second order ODEs

AIM:

To Write a program in Matlab\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.

PROCEDURE:

1. Start the program with clear,close all and clc.

2. The basic inputs such as m,b,l,g are noted down and typed in.

3. For theta_0, a matrix is created with angular displacement and velocity which are 0 and 3 respectively.

4. For t_span(time), linspace is used to generate 100 values between 0 and 20 seconds.

5. A predefined function clled ode45 is used in order to to calculate the second order differenciation starting with t and theta and a seperate function called ode_func.

6. A seperate function is created called the ode_func in which the values of theta1 nd theta2 are calculated using the given formula in the challenge.

7. Now a plot is plotted with t and results

i) for the first graph, t and the results's first column are plotted.

ii) for the second graph, t and the results's second column are plotted.

8. Now in order to create a movie first a for function is used from 1 to length(results) and is called i.

9. The values of x0,x1,y0 and y1 are typed in. since the graph is plotted in the 4th quadrant y1's value become -l*cos(THETA).

10. A graph is first plotted for the bar and then using the markerfacecolor the color of the pendulum is generated

11. The size of the pendulum is generated using the marker function and is kept 20.

12. The axes are set.

13. Now, counter(ct) is specified and an array for ct is created under the name (M).

14. The video file is then created and then is named simple_pendulum.avi and saved as an AVI file.

15. Hence the graph and the videofile are created and the output is achieved.

PROGRAM:

clear all

close all

clc

theta_0 = [0;5];

b = 0.05;

g = 9.8;

l = 1;

m= 0.1;

t_span = linspace(0,10,100);

[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

plot(t,results(:,1))

hold on

plot(t,results(:,2))

xlabel('time');

ylabel('plot');

ct=1;

for i = 1:length(results);

THETA = results(i);

x0 = 0;

y0 = 0;

x1 = l*sin(THETA);

y1 = -l*cos(THETA);

figure(2)

plot(x1,y1,'o','markers',20,'markerfacecolor','r')

hold on

plot([x0 x1],[y0 y1],'linewidth',3);

plot([-0.5 0.5],[0 0],'linewidth',10)

axis([-1.5 1.5 -2 0.5]);

pause(0.03)

hold off

M(ct) = getframe(gcf)

ct=ct+1;

end

movie(M)

videofile = VideoWriter('simple_pendulum.avi','uncompressed AVI');

open(videofile)

writeVideo(videofile,M)

close(videofile)

PROGRAM 2(ODE_FUNC):

function [dtheta_dt] = ode_func(t,theta,b,g,l,m)

theta1 = theta(1);

theta2 = theta(2);

dtheta1_dt = theta2

dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1)

dtheta_dt = [dtheta1_dt; dtheta2_dt];

end

ERRORS FACED AND HOW I OVERCAME IT:

1. I faced a lot of errors while creating the animation of the pendulum since i made a lot of mistakes in the for loop function.

I overcame it by troubleshooting and by referring to my previous notes of for loop that I have taken.

OUTPUT:

The graph has been plotted and the videofile of the pendulum has been created and therefore, the given challenge has been executed sucessfully.

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.