Challenge

Week 3 - Solving second order ODEs

%%CREATING 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

%%given data

b=0.1;
g=9.8;
L=1;
M=1;

%%variable

theta_0=[0;3];


t_span=linspace(0,20,100);

%%CALLING THE FUNCTION

[t,thetas]=ode45(@(t,theta) ode_func(t,theta,b,g,L,M),t_span,theta_0)

%%plotting displacement

figure(1)
plot(t,thetas(:,1))
xlabel('time')
ylabel('diplacement')

%%plotting velocity

figure(2)
plot(t,thetas(:,2))
xlabel('time')
ylabel('velocity')

% %PLOTTONG THE PENDULUM

x1(1:length(t_span),1)=0;
y1(1:length(t_span),1)=0;


x2=-L*sin(thetas(:,1));
y2=-L*cos(thetas(:,1));

%%FOR CREATING THE ANNIMATION
ct=3
for i=1:length(t_span)
X1=x1(i,1)
Y1=y1(i,1)
X2=x2(i)
Y2=y2(i)
figure(ct)
hold on
plot([X1 X2],[Y1 Y2],'linewidth',4,'color','r')

plot(X2,Y2,'ro','linewidth',10,'markersize',10);
color=[0.1 0.1 0.1]
plot([(-6) 6],[0 0],'linewidth',5,'color',color)
axis([(-2) 2 (-2) 1]);
pause(0.1);

pendulum(ct)=getframe
ct=ct+1;
end

%%CREAING MOVIE

movie= VideoWriter('recording');
open(movie);
writeVideo(movie,pendulum);
close(movie);

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.