Challenge

Week 3 - Solving second order ODEs

  1. AIM:

Solving second order ODEs

  1. OBJECTIVES:

Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping;

Write a program in Matlab that will simulate the pendulum motion.

  1. GIVEN:

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

Use,

L=1 meter,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Simulate the motion between 0-20 sec, for angular displacement=0, angular velocity=3 rad/sec at time t=0.

  1. SECOND ODE EQUATION SOLUTION:

5. CODE EXPLAINATION STEP WISE:

Start the program writing by creating new script;

Always use the comments in the program for the validation of specific inputs and thus the commands have been use through the program;

i. Inputs;

Input variables for function script (fun_ode in my case)

b = damping factor, l = length, m = mass and g = gravity;

By changing the b (damping factor), we can see the changes in the oscillation (harmonious).

ii. Now give the Initial condition and time variable

Initial condition will have two scalar vales Angular displacement and Angular velocity;

Whereas time variable will be in column vector, this will give us the movement for specific time span.

iii. Use the function in new script to define the variables in this program easily for 2nd ODE.

Name the function and save it with the exact name, then provide it the variables of time and angle (displacement).

As shown above in the equation, provide the variables to theta;

iv. Write the solved ODE with the main function and provide new variables to record the output;

Use matlab inbuilt command ode45 (ODE solver) and give it the main function variables and then give the assigned variables as shown in code, also add the initial condition and time span variables.

v. Now do the plot for the output;

Plot will show the angular velocity and angular displacement with respect to time, as result will have 2 column vector (theta and velocity).

vi. Now start the plotting for animation;

Start with counter variable as it will record the step for get frame command.

Use for loop, to get the all the points for the displacement variable.

Thus use the length function for the new function as shown in the code;

Provide the variables for the start and the end of the line, this point are the fixed points.

Now, use the loop function to give the end points for the bob of the pendulum which will be moving for the provided time.

Use sin and –cos for the motion direction. (Use –cos for the y2 coordinate to keep the bob at the down side of the plot, otherwise the pendulum will look upside down)

vii. Plotting the animation

This commands are in for loop only;

Now start with figure2 command to plot the animation in separately.

Start with plot of fixed line (wall), give the coordinates and use hold on command to stich the next command for the plot,

Now use the line command and give the x and y coordinate for start and end points.

Now use plot command to plot the bob at the end of the line, use marker command for highlighting the bob.

At the end give the axis command as per your output and also use hold off command to end the hold.

Before ending for loop, use pause command and also provide the +1 increment to counter.

And now end for loop.

viii. Record the animation with movie command and save it.

Use the movie command for the getframe variable

Give the variable to the file (videofile) and use videowriter command for write the animation.(use m file name as exact)

Now use the open command to open the file and write the video by giving the inputs of filename and getframe variable.

And at the end, close the file.

6. RESULT:

I have recorded the output of the 2nd ODE and also created the animation of the pendulum.

7. ERRORS OR ISSUES

Above are the screenshots of the error or issues I faced,

For first error, the result variables I was using wasn’t recorded properly thus I couldn’t plot the second plot, but when I corrected the result variable the issue got resolved.

Second, it is not an issue but something I think is important. As we learn by following we tend to follow each and everything and thus when we use new values somethings doesn’t come right. Thus, I found that my animation bob was crouching the axis line and thus I changed it to the center by measure my output of the result.

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.