Challenge

Week 3 - Solving second order ODEs

Aim= To solve second oder ODE in matlab 

code=

% second order ode to solve the pendulam motion
close all 
clear all
clc 
b = 0.05;
g = 9.81;
l = 1;
m = 1;

% initial condition 
theta_0=[0;3];

% time points
t_span=linspace(0,20,100);

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

plot(t,result(:,1))
hold on 
plot(t,result(:,2))


ct=1;
for i=1:length(result(:,1))
    
    x0=0;
    y0=0;
    
    x1=l*sin(result(i,1));
    y1=l*cos(result(i,1));
    
    figure(2)
    %plotting
     plot([1 -1],[0 0],'linewidth',3)
    hold on
    
    plot([-x0 -x1],[-y0 -y1],'linewidth',4)
    axis([-4 4 -4 4])
    plot(-x1,-y1,'.','markersize',35)
    M(ct)=getframe(gcf);
    ct=ct+1;
    hold off
end
movie(M)
videofile=VideoWriter('ode_function_for_loop.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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

Explanation = m=mass, l=length, b=dampping co-efficient, theta in degree, t=time spain etc

converted second order ODE to frist oder 

then by using inbuilt matlab command ODE 45 solved the given above equatio

and then after ODE function is called in main progrrame then the result is plotted 

created the horizontal and vertical line in plot ,bob at the end 

theta value is change by the help of for loop command and then plotted the figure 

hold on the all figures in one frame and then movie is created and that file is saved as .AVI extension

Error= 

in line number 17 at ODE 45 ')'extra prantasis is written

line num 16 comma is missing (:1) 

corrected as (:,1)

Result= https://youtu.be/qBekjD-qgx0

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.