Project

Solving second order ODEs

Objective:To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.

\"\"

Code:

clear all
close all
clc

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

%initial condition

theta_0=[0;3];

%time condition
t_span=linspace(0,20,1000);

%solve ode
[t, results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
 figure (1)
 hold on
 plot(t,results(:,1))
 plot(t,results(:,2))
 xlabel(\'time\')
 ylabel(\'plot\')
 
 figure (2)
 hold on
 x0 = 0
 y0 = 0
 ctr = 1
 for i =1:length(t)
     x1 = l*sin(results(i,1));
     y1 = -l*cos(results(i,1));
     plot([x0,x1],[y0,y1],\'linewidth\',5,\'color\',\'b\')
     hold on
     plot(x1,y1,\'o\',\'markersize\',10,\'markerfacecolor\',\'red\')
     axis([-1.2 1.2 -1.2 1.2])
     grid on 
     hold off
     pause(0.04)
     M(ctr)=getframe(gcf);
     ctr=ctr + 1;
 end
 movie(M)
 videofile=VideoWriter(\'forwardwriter.avi\',\'Uncompressed avi\')
 open(videofile)
 writeVideo(videofile,M)
 close(videofile)

Step 1: Provide predetermined/known values.

Step 2 :Initial condition are provided were 0 is the time and 3 is the angular velocity.

Step 3: Values of intervals ( 0 to 20 in 1000 steps )

Step 4:solving the ode equation using ode45 ([t,y] = ode45(odefun,tspan,y0), where tspan = [t0 tf], integrates the system of differential equations y′=f(t,y) from t0 to tf with initial conditions y0. Each row in the solution array y corresponds to a value returned in column vector t.)

Function defined :

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

Step 5: Plotting the results

Figure 1:

\"\"

Steo 6:Make a figure 2 ,provide origin for pendulum

Step 7: Use counter to create animation

Step 8: Use forloop till the length of time stored in results.

Step 9:Findout position of pendulum and plot line from origin to pendulum which can be considered as thread .

Step 10 : Plot the pendulum ball using marker size greater than line

Step 11: Create a movie .

Conclusion :

Animation was created and how to solve ODE was demonstrated .

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.