Challenge

Week 4.1 - Solving second order ODEs

aim

In Engineering, ODE is used to describe the transient behavior of a system. 

 there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of theses systems.

function

function [dtheta_dt] = ODE_func(t,theta,b,m,l,g);
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
 
 
This function implements a Runge-Kutta method with a variable time step forecient computation. ode45is designed to handle the following general problem: dx =f(t;x); x(t0) =x0; (1)
 
ode main
clear all
close all
clc

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

% initial condition 
theta_0 =[0;3];

%time point

t_span =linspace(0,20,120);

% solve ODEs

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

plot ( t,results(:,1));
hold on 
plot (t,results(:,2));
ylabel('t_span')
xlabel('time')
code explanation

In the first few lines, we are importing various modules that are required to run the program. Scipy is used to solve the ODE, matplotlib to plot the graphs, and Numpy to obtain values in a specific range.

After that, we described a model function that follows the governing principle. This function will be called later when solving the ODE. We are returning the final dtheta value back to the called variable. Initial conditions are then defined as per the requirement

To produce the animation, we are taking the theta value in a range and finding the position of the pendulum at that angle. Using basic arithmetic, the positions are obtained and the graphs are plotted. To save the figures separately, a counter was initalized. 

simplysupported_beam

clear all 
close all
clc 

%inputs

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

%initial condition

theta_0 = [0;3]

%time point

t_span = linspace(0,20,120)

%solving ode

[t,results] = ode45(@(t,theta) ODE_func(t,theta,b,m,l,g),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('temp')
title('simplysupported_beem')

%animation
ct = 1;
figure(2)
theta_1 = results(:,1)
for i = 1:length(theta_1)
    x0 = 0;
    y0 = 0;
    theta_2 = theta_1(i);
    x1 = l*sin(theta_2);
    y1 = -l*cos(theta_2)
    plot([-2 2],[0 0])
    hold on
    plot([x0 x1],[y0 y1],'LineWidth',3)
    
    plot( x1,y1,'marker','o','Color','r','MarkerSize',40,'MarkerFaceColor','y');
   
    axis([-2 2 -2 2])
    pause(0.03)
    M(ct) = getframe(gcf)
   hold off
    ct = ct+1
end
movie(M)
videofile = VideoWriter('simplysupported_beem.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

* In case of simulation project :-

Let we take tha animation command start with counter ct
Now make the simply supported beem to take tha input perameter like theta_1 = (:,3)
And using for loop command to make tha animation to using tha inputs
Let we assume the orrigin x0 and y0 to x1 and y1 it first x0 = 0 and y0 = 0 and x1 = l*sin(theta_2) and y1 = -l*cos(theta_2)
And plot tha straight line take tha point at [-2 2] and [0,0] it hold on
It will be join the link at downwards at assume tha point at x1 and y1
And we assume tha pendulam of bob used a markar command and tha circle it assume at 'o'
make the plot at animate the simply supported beem
In plot( x1,y1,'marker','o','Color','r','MarkerSize',40,'MarkerFaceColor','y') and take tha axis [-2 2 -2 2]

* In case of design project :-

* finally use the movie command at full feel of animation of simply supported beem Movie(m)
*At the movie command was use at open videofile and write video and close videofile

Results:

We can observe the damping of the pendulum in the above plots. From the animation, we can see the simple pendulum's motion which is just as expected.

Conclusion

The required plots are obtained and the animation was rendered. 

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.