Challenge

Week 3 - Solving second order ODEs

Aim :

To programe a ode function for a given function.

Given Function :

d2θ/dt2 + b/m dθ/dt +g/l sinθ = 0;

Given Condiction :

initial displacement = 0

given angualr velosity = 3 rad/sec

time interval = 20 sec.

Given Inputs :

length of pendulam (l) = 1 meter;

mass (m) = 0.1 kg.

damping co-efficient = 0.05;

gravity = 9.81.

Main Program :

clear all
close all
clc

%input parameters
l = 1;
b = 0.05;
g = 9.81;
M = 0.1;

%given condiction
con_0 = [0,3];

%given time
t_span = linspace(0,20,500);

%ode function
[t,result] = ode45(@(t,con) formula_fun(t,con,g,l,b,M),t_span,con_0);

%ploting
    figure(1)
    plot(t,result(:,1),'color', 'r')
    axis([0 15 -10 10])
    hold on
    plot(t,result(:,2), 'color', 'b')
    xlabel('time')
    ylabel('plot')
    grid on
    legend('displacement','velocisy')
    
%video ploting
ct = 1;
for j = 1:length(t_span);

    x0 = 0;
    y0 = 0;

    x1 = l*sin(result(j,1));
    y1 = -l*cos(result(j,1));
    figure(2)
    plot([x0 x1],[y0 y1],'LineWidth',2);
    pause(0.025)
    grid on
    axis([-3 3 -3 3])
    m(ct) = getframe(gcf);
    ct = ct+1;
end
movie(m)
videofile = VideoWriter('pendulam_chalange.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,m)
close(videofile)

Function Program :

function [dtheta_dt] = formula_fun(t,con,g,l,b,m)
theta1 = con(1);
theta2 = con(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
[dtheta_dt] = [dtheta1_dt;dtheta2_dt];
end

Explanaction :

  1. initialy the program clear all content in work space and command window and close all opened files respect to matlab.
  2. lnow the program reads the given input values and condiction which is length, mass, dampinf co-efficient, gravity.
  3. the condictions are set to arra which is :: con_0 = [0,3]. herse 0 is initial displacement,3 is angular velocity.
  4. the number of time is set to with in 0 to 20 sec of 500 values.
  5. now the ode45 solver is used to derive the equation. here the inputs and condictions are given in the function formula_fun with the con_0 and t_span.
  6. these values are saved in [t,results]. were t contian t_span value,result contioan output value of angular displacement and angular velocity.
  7. when function get called the input values are automativaly assigned to the respective variables.
  8. all these values are stored in [d2θ/dt2].
  9. here variable theta1 contain the initial displacement 0,theta2 contain the initial angular velosity 3 rad/sec.
  10. the given function is a secound order equation so it is splited in to simple first order equation manualy.
  11. ther fore dθ1/dt = theta2 :: which is initial angular velosity.
  12. dθ2/dt = -b/m * theta2 - g/l * sin(theta1). :: which is angular displacement respect to velosity.
  13. both values are saved in [d2θ/dt2] as initialy declared.
  14. now the result variable contain t_span times of displacement ,velosity value.
  15. now to plot:: plot(t,result(:,1) - gives the plot relaction between time to angular displacement.
  16. now to plot:: plot(t,result(:,2) - gives the plot relaction between time to angular velosity.
  17. legend commend helps to give the respective name for plots.
  18. to find the displacement for end of the pendulam point for loop created with respect to t_span time
  19. j = 1:length(t_span) these syntex helps to repet the loop until the time given limit ends.
  20. now initial cordinatie(first end of pendulam) give as x0 = 0,yo = 0.
  21. ne secound end is change its possition withrespect to angular displacement for each time sec.
  22. it is given by x1 = sin(result(j,1)); and y1 = -cos(result(j,1)); here j represent the t_span counte and the first value of displacement in result matrix.
  23. For each time loop runs displacemnt value change accourdingly.
  24. these value get to plot by(plot commend followed by([x0 y0],[x1 y1],'linewidth',2).
  25. for each loop seperate plots will be ploted.these seperate plots are called as frame.
  26. these frames are collected in m(ct) variable with getframe(gcf) commend.
  27. now the value for ct increased by 1 :: ct = ct+1;
  28. so the loop continues until the j - t_span of 20 sec eaches.
  29. these frames are converted to movie.
  30. videofile is a variable created to store video in the .avi formate.
  31. open commend helps to open the video file.
  32. VideoWriter commend helps to collect all frames and create a movie file.
  33. close commend helps to close the video file.

Note : my orginal video file got 534mb so i compressed the video on onloine. thanking you.....

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.