Challenge

Week 3 - Solving second order ODEs

Challenge 6 Solving 2nd order ODE

clear all

close all

clc

b=0.05;

g=9.81;

l=1;

m=1;

Constant Defined as given in challenge

% initial condition

theta_0=[0;2];(initial condition of displacement)

% time point

t_span=linspace(0,20,200); (time array)

% slove ODE ( call ode function)

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

%ploting

plot(t,results(:,1))

hold on

plot(t,results(:,2))

xlabel('plot')

ylabel('time')

grid on

title('Pendulum Motion')

% iteration using for loop to create

ct=1;

theta_1 = results(:,1);

for i = 1:length(results(:,1))(for loop is use to repeat result i)

    % initial coordinates

    x_start = 0;

    y_start = 0;

    theta_2 = theta_1(i);

    % final coordinates

    x_end = 1*sin(theta_2);

    y_end = -1*cos(theta_2);

    % ploting for the animation

    figure(2)

    % base of pendulum

    plot([-1,1],[0,0],'linewidth',4,'color','k')

    hold on

    % string the pendulum

    line([x_start x_end],[y_start y_end],'linewidth',4,'color','b');

    hold on

    %bob of pendulum

    plot(x_end,y_end,'marker','o','markersize',15,'markerfacecolor','k');

    axis ([-2 2 -2 2 ])

    grid on

    % pause(0.03)

    hold off

    M(ct)=getframe(gcf);

    ct=ct+1;

end

% creating movie from capture frame

movie(M)

videofile = VideoWriter('simple_pendulum.avi');

open(videofile);

writevideo(videofile,M);

close(videofile);

function [dtheta_dt]=ode_func(t,theta,b,g,l,m)(Function is used call the values)

theta1= theta(1)

theta2= theta(2)

dtheta1_dt=theta2;

dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);

dtheta_dt=[dtheta1_dt;dtheta2_dt];(column matrix)

end

 

 

PE1 : is error occurred tresult is taken as (;.0) instead of (;’1)

clear all 
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
% initial condition 
theta_0=[0;2];
% time point 
t_span=linspace(0,20,200); 
% slove ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%ploting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('time')
grid on
title('Pendulum Motion')
% iteration using for loop to create 
ct=1;
theta_1 = results(:,1);
for i = 1:length(results(:,1))
    % initial coordinates
    x_start = 0;
    y_start = 0;
    theta_2 = theta_1(i);
    % final coordinates 
    x_end = 1*sin(theta_2);
    y_end = -1*cos(theta_2);
    % ploting for the animation 
    figure(2)
    % base of pendulum
    plot([-1,1],[0,0],'linewidth',4,'color','k')
    hold on
    % string the pendulum
    line([x_start x_end],[y_start y_end],'linewidth',4,'color','b');
    hold on
    %bob of pendulum
    plot(x_end,y_end,'marker','o','markersize',15,'markerfacecolor','k');
    axis ([-2 2 -2 2 ])
    grid on
    % pause(0.03)
    hold off
    M(ct)=getframe(gcf);
    ct=ct+1;
end
% creating movie from capture frame
movie(M)
videofile = VideoWriter('simple_pendulum.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
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.