Challenge

Week 3 - Solving second order ODEs

explaination of the code.

1)starting with

clear all

close all

clc

2)and then further giving the input parameters

3)now, coding initial conditions and time points

3)and the code to solve the ode.

4)next is the code to plot, and further lined with the code to animation.

5)ending the code with videofile and movie.

% Program to solve Second Order Differential equation

close all
clc
close all

% Input Parameters for the given equation
l = 1;
m = 1;
b = 0.05;
g = 9.81;

% Initial Condition
theta_0 = [0;5];

% Time points
time_span = linspace(0,10,100);

% Solving ODE
[t,results] = ode45(@(t,theta) ode_func1(t,theta,b,g,l,m),time_span,theta_0);

% Plotting
plot(t,results(:,1));
hold on
plot(t,results(:,2));
xlabel('Time');
ylabel('Plot');

legend('Angular displacement','Angular velocity');

% Creating Animation
ct = 1;
for i = 1:length(results(:,1))
    x0 = 0;
    y0 = 0;
    
    x1 = 1*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    
    figure(2)
    plot([-1.5,1.5],[0,0],'linewidth',3,'color','r');
    hold on
    plot([x0 x1],[y0 y1],'linewidth',1,'color','g');
    plot(x1,y1,'marker','o','markerface','b','markersize',15);
    axis([-2,2,-2,2]);
    title('Simple Pendulum Oscillation');
    pause(0.01);
    
    M(ct) = getframe(gcf);
    ct = ct+1;
    hold off
end

movie(M)
videofile = VideoWriter('Simple_Pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile
%ode_func
% function of time and theta

function  [dtheta_dt] = ode_func1(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

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.