Challenge

Week 3 - Solving second order ODEs

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;
g = 9.81;
l = 1;
m = 1;

%initial conditions
theta_0 = [0;3];

%time points
t_span = linspace(0,20,500);
% solve ODE
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t, results(:,1))
hold on
plot(t, results(:,2))
xlabel('time')
ylabel('plot')

%origin



%loop for animation
ct=1;
figure(2)

for i=1:length(results(:,1))
    x_0=0;
    y_0=0;
    x1 = l*sin(results(i,1));
    y1 = - l*cos(results(i,1));
    %specifying the point of the ball
   plot([-1.5 1.5],[0 0])
   hold on 
   plot([x_0 x1],[y_0 y1],'LineWidth',1,'Color','b')
   axis([-1.5 1.5 -1.5 1.5 ])
   plot(x1,y1,'o','MarkerSize',20,'color','r')
   pause(0.03)
   M(ct) = getframe(gcf)
   hold off
   ct=ct+1
end

movie(M)
videofile= VideoWriter('simple pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
   

Explanation for 2nd Order ODE_;

1.here as an example i took the simple pendulum damping theory for 2nd order ODE.
2.enter input variables.
3.enter initial conditions
4.enter time points.
5.enter ODE equation.
6.and another code written for ode_funct in another file.
7.plot the graph between t,results with first column and hold on and with second column.
8. xlabel and ylabel as plot,time.

ANIMATION-

9.starting with for loop and giving positions of ball.
10.giving equations to position the ball.
11.plot the animation with given limits.
12. creating a movie with M and giving count for it.
13.now saved it in videofile

-i got errors in 16 line and trying to correct.

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.