Challenge

Week 3 - Solving second order ODEs

% Program for solving 2nd order ODE
clear all
close all
clc
 
%Initial variable assignment
b=0.05;
g=9.81;
l=1;
m=1;
 
%Initial parameter
theta1=[0;3];
 
%time points
time_span=linspace(0,20,100);
 
%Inbuilt ODE solver
[t,result]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),time_span,theta1);
figure(1)
plot(t,result(:,1));
hold on
plot(t,result(:,2));
xlabel('time');
ylabel('Amplitude');
 
%Programm for animation using for loop
ct=1;
for i=1:length(result(:,1))
    x0=0;
    y0=0;
    x1=1*sin(result(i,1));
    y1=-1*cos(result(i,1));
    figure(2)
    plot([-1 1],[0 0],'linewidth',6,'color','r');
    axis([-2 2 -2 2]);
    line([x0 x1],[y0 y1],'linewidth',2,'color','cyan');
    hold on;
    plot(x1,y1,'o','Markersize',20,'Markerfacecolor','b','markeredgecolor','g');
    xlabel('time');
    ylabel('Amplitude');
    hold off;
    pause(0.1)
    z(ct)=getframe(gcf);
    ct=ct+1;
end
 
movie(z)
videofile=VideoWriter('pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,z)
close(videofile)

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.