Challenge

Week 3 - Solving second order ODEs

 Solving second order odes's using matlab

now we are considering a simple pendulum whis is a second order ode

we shoul spit the given ode to girst order ode in all conditions to solve our program.

this can be split into two first order ode .

first we assume theta=theta1

then,

dtheta_dt=dtheta1_dt=theta2

so,

dtheta1_dt=theta2

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

so we do programming as usual,

clear all
close all
clc

providing the inputs,

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

initial_condition
theta_0=[0;5];

time_points
t_span=linspace(0,20,50);

next step is solving ode;

we use a function keyword.for that we make a function;

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)*sintheta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

this function is attached or called to the initial program.so,

it looks like this,

solving ode,

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

plotting the graph,

figure1

plot(t,results(:,1))
hold_on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

after this we will try to solve the animation of the pendulum,

c=1;
theta_0=result(:,1);
for n=1:length(result(:,1))

Intial position of string
x0=0;
y0=0;

End position of string
theta_1=theta_0(n);
x1=sin(theta_1);
y1=-cos(theta_1);

Ploting the pendulum
figure(2)

base line of pendulum
plot([-2 2],[0,0],'linewidth',4,'color','b')
hold on

string of the pendulum
plot([x0 x1],[y0 y1],'linewidth',2,'color','r')
hold on

mass suspended on the pendulum
line(x1,y1,'marker','o','markersize',15,'color','r')
axis([-3 3 -3 3])
pause(0.03)
M(c)=getframe(gcf)
c=c+1;
end

so we can create th eanimation of this pendulum figure,for that we use this,

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

so the program is done.

clear all
close all
clc

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

%initial_condition
theta_0=[0;5];

%time_points
t_span=linspace(0,20,50);

%solve_ode

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

%plotting
%figure1
plot(t,results(:,1))
hold_on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

%to_create_animation_of_simple_pendulum
c=1;
theta_0=result(:,1);
for n=1:length(result(:,1))
    
    %Intial position of string
    x0=0;
    y0=0;
    
    %End position of string
    theta_1=theta_0(n);
    x1=sin(theta_1);
    y1=-cos(theta_1);
    
    %Ploting the pendulum
    figure(2)

    %base line of pendulum
    plot([-2 2],[0,0],'linewidth',4,'color','b')
    hold on

    %string of the pendulum
    plot([x0 x1],[y0 y1],'linewidth',2,'color','r')
    hold on

    %mass suspended on the pendulum
    line(x1,y1,'marker','o','markersize',15,'color','r')
    axis([-3 3 -3 3])
    pause(0.03)
    M(c)=getframe(gcf)
    c=c+1;
end

%Animation creating process

movie(M)
videofile=VideoWriter('Pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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.