Challenge

Week 3 - Solving second order ODEs

Errors faced while programming:
 Hereby mistake, i give the wrong argument to the ode45 function so that it gives an error. I correct that mistake by writing it properly.
Code Explanation:
 Plotting:
   A line plot represents a mathematical function. It shows how the function changes with respect to the input argument. the trend of any variable that depends on an independent variable (e.g time or frequency) could be shown graphically using line plots.
Syntax: plot(x,y)
 The above is the basic syntax to draw a line plot. Add-ons such as line colour, linewidth, markers, etc could be used by naming them between apostrophes (i.e '') and mentioning their respective values next to them.
  Example: plot(x,y,'color','r','linewidth',3,'marker','o')
Here the value 'r' next to 'color' red and 'o' means all the coordinates will be marked by a circle. 
 For loop:
 A 'loop', in general, refers to anything whose start and end points are connected. That is , once you begin from the start point and proceed further, you will reach the finish line just to find that it once again guides you to the same point where you started. Proceeding further means that you are repeating your same action again & again. 
  Now, in programming point of view, a 'loop' is a set of instructions or commands that repeats itself for a specified number of times (in case of 'For loop') or until the user stated condition becomes false (in case of 'While loop').
Syntax: 
        for variable_name = initial value : final value

        Statement 1;
        Statement 2;

        end
 In loop command, the part of the command following the "=" is called 'statement' where we mention the number of times the group of statements needs to be executed.
 Any loop must contain "end" statement (in the case of MATLAB)to symbolize the end of our set of instructions so the program again checks the condiyion to repeat the process.



 

https://www.youtube.com/watch?v=Khwxc2OzeBc

clear all
close all
clc

%Input

%Damping Coefficient
b = 0.05;
%gravity
g = 9.81;
%Length of the pendulam
l = 1;
%Mass of the ball
m = 0.1;

%Initial condition for displacement and angular velocity
theta_0 = [0,3];

%time intravel points
t_span = linspace(0,20,500);

%ODE results
%call ode_fun function
%ode45 gives result in rad and rad/s.
[t, results] = ode45(@(t,theta) ode_fun(t,theta,b,g,l,m), t_span, theta_0);

figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title('For Simple Pendulam')
xlabel('Time in sec')
ylabel('Angular Velocity(rad/sec) / Displacement(rad)')
legend('Displacement','Angular velocity')


%Animation of pendulam
amplitude = results(:,1);%Store amplitude values in separate matrix for loop
p=1;
for i = 1 : length(amplitude)
    angle = amplitude(i);
    figure(2)
    title('Simple Pendulam')

    %fix point of pendulam
    x0 = 0;
    y0 = 0;
    %another end of pendulam/cordinate of mass
    x1 = 1*sin(angle);
    y1 = -1*cos(angle);

    %plot
    plot([-1.1 1.1],[0 0],'linewidth',3); %celling line
    hold on
    plot([x0 x1],[y0 y1],'color','r','linewidth',3) %string
    plot(x1,y1,'o','markers',20,'markerfacecolor',[1 1 0]) %mass ball
    hold off %removing previous position
    grid on
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.001)
    M(p) = getframe(gcf)
    p = p+1;
end


movie(M)
videofile = VideoWriter('simple_pendulam.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
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.