Challenge

Week 3 - Solving second order ODEs

% Aim
to solve the second order ODE using Matlap to simulate the motion of pedulum using Matlap
%SOLVING 2ND ORDER ODE USING MATLAP
close all
clear all
clc
% values given
b = 0.05; (damping coefficient)
g = 9.81; (gravity)
l = 1; (length of pendulum)
m = 0.1; (mass)
1. solving a second order ODE we split into second order ode then we find the overall solution
2. function order is using to split second order ODE into 2 order ODEs the function operator name is ODE_func
%initial condition
theta_0=[0;5];
* theta is a initial condition the values are given
%time points
t_span = linspace(0,20,200);
* we use the linspace command to create the the 200 values in between 0 to 20
%solve ODE
[t,results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
* we create the individual command window and we created the ode_function using to calculate the values of ode_function
* formula using in ode_func
*ode45 (inbuild function in matlap)
we dodnt the value of so we create the ode_function
open a new window and create the command and run the programme
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
we using the to find out the ode_func
(dtheta1/dtheta) = theta2
(dtheta2/dtheta) = -(b/m)*theta - (g/l)*sintheta1
we find out the ode_func and apply the main function we get the results
* fugure (1)
* plot the values of time and distance inwritten using plot command in the figure (1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));
xlabel('time')
ylabel('plot')
we got a coffect plot output
xaxis represent(time)
yaxis represents(plot)
* next to create the movie output
* for the simultion of pendulum motion we used for loop to given values of the coordinates of the position of end points of the pendulum
* the origin points [x0 ,y0] are (0,0) thats the given points
*new values of 'res' is given to find the other end points of pendulum to oscillaites with respective time
% to defind point of pendulum and plot it
x0 = 0;
y0 = 0;
plot(x0,y0,'marker', 'o')
% to extract the first column of the result and assigned it to 'y'
y = results(:,1);
%to create images for each time values and stored it in array 'm',
ct = 1
for i=1:length(t_span)
%y1=y(i)
x1 = 1*sin(y(i));
y1 = -1*cos(y(i));
plot([x0 x1],[y0 y1],'linewidth',3,'marker','o')
hold on
plot(x1, y1, 'marker','o', 'color','g','markersize',30, 'markerfacecolor', 'g')
hold off
title({'simple harmonic motion','of the pendulum'},'color','r')
axis([-1 1 -2 0])
pause(0.01)
M(ct)= getframe(gcf);
ct = ct+1
end
%to create animation of S.H.M of simple pedulum
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
* movie command using to make videofile in uncompressed file that saved in the system
%movie explanation
we got the values of x0, y0 = 0 this is constant
we calculate the x1,y1
thereis using formula to find out the values
x1 = l*sintheta
y1 = l*costheta
using the formula we got the values of x1, y1
marksize is using the thickness of line x0 to y1
markfacecolour to using end of the pendulum colour
and put the title command
% ode_main function command
% output of video file in pendulum i attached the link in below
* conclusion
*we calaculate the simple pendulum whith respective given values and findout the incorrect values of time and ditance of pendulum in given values
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.