Challenge

Week 4.1 - Solving second order ODEs

https://youtu.be/U-vX-HCam1Y

#1.maincode
clear all 
close all
clc

%given input in challenge
b=0.05;
g=9.81;
l=1;
m=1;

%intial condition;
theta_0=[0;3];
t_span= linspace(0,20,100);

%results

%using ode45 solver to execute the condtion
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m), t_span, theta_0);


% creating seperate figure output for 2d waves which shows relation cp vs temp
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('temperature') %labeling the axis.
ylabel('cp')


T1=results(:,1);
ct=1;
for i=1:length(T1) % using for loop for the execution of equation for pendulum
     
    % x and y value for fixed side,
    x0=0;
    y0=0;
    % x and y value for given b,g,l and m at the other end or pendulum side..
    x1=l*sin(T1);
    y1=-l*cos(T1);

    figure(2)  % figure which shows the animation of pendulum
    plot ([x0 y0], [x1 y1])
    hold on
    plot(x1,y1,'marker','o')%used marker command for sphere
    M(ct)=getframe(gcf);
    ct=ct+1;

end

% using movie command to record the moving animation of the pendulum. 
movie(M)
videofile= VideoWriter('C4_1.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile, M)
close(videofile)

#2. function code

% using funtion command to execute the ODE in the main code.
function [dtheta_dt]= ode_func (t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);

dtheta1_dt = theta2;  % first 
dtheta2_dt=-b/m*theta2-g/l*sin(theta1); %second
dtheta_dt=[dtheta1_dt; dtheta2_dt]; % combining both equation results them in a one array

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.