Challenge

Week 4.1 - Solving second order ODEs

https://youtu.be/o1bQUztTtAs

%function that gives or returns dtheta/dt 

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

clear all
close all
clc

b = 0.05 %damping co.eff 
g = 9.81  %gravity
l = 1  %length of pendulum
m = 1  %mass of ball

%initial conditions  angular displacement =0 and angular velocity=3
theta_0 = [0,3]   

%time points  0 to 20 sec
t_span = linspace(0,20,200);   

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

%Plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))

xlabel('TIME')
ylabel('PLOT')
hold off
saveas(1,'Time_plot.png')

%Animation

x0=0
y0=0
x1=-2
y1=0
x2=2
y2=0
ct=1
for i = 1:length(t)
    x=l*sin(results(i))
    y=-l*cos(results(i))
    
    
    plot([x1,x0,x2],[y1,y0,y2],'linewidth',8)
    hold on
    plot([x0,x],[y0 y],'linewidth',2)
    hold on
    plot(x,y,'o','markersize',15,'markerfacecolor','m')
    axis([-3 3 -3 0])
    hold off
    pause(0.05)
    d(ct)=getframe(gcf);
    ct=ct+1;
    
end
movie(d)
videofile=VideoWriter("Simple_pendulam.avi","uncompressed AVI");
open(videofile)
writeVideo(videofile,d)
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.