Challenge

Week 3 - Solving second order ODEs

Solving the 2nd order diff. equation by using of MATLAB

Aim Of The Project:-

Aim is to write a program that solves the ODE. This ODE represents the equation of motion of a simple pendulum with damping.

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.

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

use,

L=1 metre,

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

MATLAB Program:-

close all

clear all

clc

%Solving 2nd Order Ode

%input

L= 1;

b= 0.05;

m= 1;

g= 9.81;

%initial condition

z0= [0;4]

t_span = linspace(0,20,100);

%solving _2nd Ode

[t,z]= ode45(@(t,theta) ode_fun(L,m,b,g,theta,t),t_span,z0);

%ploting the motion of pendulam

plot(t,z(:,1),'r');

hold on

plot(t,z(:,2),'b')

legend('displacement','velocity')

xlabel('time');

ylabel('plot')

%Animation of pendulam

%counter variable ct


ct= 1;
theta= z(:,1);
for i= 1:length(theta);
tehta1= theta(i);
x0= 0;
y0= 0;
x1= 1*sin(theta1)
y1= -1*cos(theta1);
title('motion of Simple Pendulam')
plot([-1 1],[0 0],'linewidth',7,'color','r')
hold on
plot([x0 x1],[y0 y1],'linewidth',4,'color','b');
hold on

plot(x0,y0,'H','markersize',10,'MarkerEdgeColor','g','MarkerFaceColor','r');

plot(x0,y0,'H','markersize',30,'MarkerEdgeColor','r','MarkerFaceColor','g');

grid on;
axis([-1.5 1.5],[-1.5 0.5]);
pause(0.05);
hold of
M(ct)= getframe(gcf)
ct= ct+1;

end

% collecting the all fram
movie(M)
videofile= VideoWriter('Pendulam_motion.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile,M)
close(videofile)

Animated Video Link:-

https://youtu.be/fdbFZVxaMhg

Code Exaplanation:-

Here we use a most important solver of matlab that is ode45. This is use in matlab to solve ODE's.Maine part is ode45 only solve to first order ode's.

Error Find:-

Two error deducted by me first is the order of value in side the ode will same as the order the value of function.

Second is name of the function and file should be same in matlab.

Referance:-

http://pages.cs.wisc.edu/~cs310-1/modules/Programming/_More%20Advanced%20Functions/ode45%20Function/TopicDiscussion.html

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.