Challenge

Week 3 - Solving second order ODEs

Aim = solve ode in Matlab.

objective = write program in matlab that solves ode.this ODE represents the equation of motion of a simple pendulum with damping.


clear all
close all
clc

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


%initial condition
theta_0 = [0,3];

%timimg
t_span = linspace(0,20,500);

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


%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')




ct = 1;
for i=1:length(t_span)
    theta = results(:,1)
    x0=0;
    y0=0;
    x=l*sin(theta(i));
    y=-l*cos(theta(i));
    
    %plotting
    
   figure(2)
   plot([-1 1],[0,0],'linewidth',6,'color','b')
   hold on
   
   %allocting co-ordinates for fixed support
   plot(0,0,'o','Markersize',10,'color','b','Markerfacecolor','b') 
   
   %allocting co-ordinates for pendulum strin
   plot([0 x],[0 y],'linewidth',2,'color','b') 
   axis([-1 1.5 -1.5 1])
   plot(x,y,'o','Markersize',10,'markerfacecolor','r')
   hold off
  
M(ct) = getframe(gcf)
ct =ct+1;
end

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




ode_func =

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


Error = 

1 without using 'ode45' run the program.


steps to overcome error = 

1 use 'ode45' while solving ode.



conclusion = by using matlab command like(ode45,function)., we are able to solve ode,and create animaton of pendulum.
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.