Challenge

Week 3 - Solving second order ODEs

Report--

Steps

  1. A separate function is written for solving the differential equation.
  2. pendulum parameters are initialized.
  3. To obtain the motion equation, time and angular displacement are mentioned.
  4. the results are obtained by solving the differential equation using ODE45.
  5. for plotting the graph of pendulum movement, vertical and horizontal positions of the endpoint of the pendulum are calculated by the formula shown.x-coordinate=sin(w*t
    %code for function
    
    function [dtheta_dt]=ode_func(t_span,theta)%objective function
    b=0.05;
    g=9.81;
    l=1;
    m=1;
     
    theta1=theta(1);     %dtheta/dt
    theta2=theta(2);     %dtheta^2/(dt)^2
    dtheta_dt(1)=theta2;%angular velocity
    dtheta_dt(2)=-(b/m)*theta2-(g/l)*sind(theta1);
    dtheta_dt=dtheta_dt(2)+dtheta_dt(1)*(b/m)+sind(theta)*(g/l);
    end
    
    
    
    %main
    
    
    b=0.05; 
    g=9.81;
    l=1; %length of string
    m=1; %mass of thr bob
    w=3; %frequency
    
    
    
    
    theta_0=linspace(-90,90,20);
    %time
    
    
    
    %Fixed point of the pendulum.
    d0=0;
    v0=1;
    %Solving differential equation.
    
    t_span=linspace(0,20,20);
    [t,theta]=ode45('ode_func',t_span,theta_0);
    tic
    ct=1;
    
    %for loop for plotting in one direction
    for i=length(t(:,1))
    
        d=-l*sind(3*t(:,1));    %x coordinate
        v=l*(1-cosd(3*t(:,1))); %y coordinate
        
        
        for i=1:length(t(:,1)) 
            plot([0 d(i)],[1 v(i)],'LineWidth',3,'color','b')
            hold on
            scatter(d(i),v(i),'o','LineWidth',10)
            hold off
            axis([-2 2 -2 2])
            pause(0.1)
            S(ct)=getframe(gcf);
            ct = ct+1;   
        end
        
    end
    
    %for loop for plotting in another direction.
    ct=1;
    
    for i=1:length(t(:,1))
        d=l*sind(3*t(:,1));     %x coordinate
        v=l*(1-cosd(3*t(:,1))); %y coordinate
        
        
        for i=1:length(theta) 
            plot([0 d(i)],[1 v(i)],'LineWidth',3,'color','b')
            hold on
            scatter(d(i),v(i),'o','LineWidth',10)
            hold off
            axis([-2 2 -2 2])
            pause(0.1)
            S(ct)=getframe(gcf);
            ct = ct+1;
             
        end   
    
    end
    
    
    time=toc
    movie(S) 
    videofile=VideoWriter('pendulum')  %creating a video file
    open(videofile)
    writeVideo(videofile,S)
    close(videofile)
    ), y-coordinate=L(1-cos(w*t)
  6. for every value of theta obtained from the equation x and y coordinates are calculated using the above formulae and stored.-----(first for loop)
  7. Later for every value of x and y graph is plotted.----(second for loop)
  8. to create an animation look,ct is used.
  9. Avi video file is created by using 'videowriter'.

ERRORS--

  1. Index exceeds the number of array elements. the index must not exceed.
  2. Array indices must be positive or local values.
  3. Error in odearguments.
  4. Dot indexing is not supported for this type of variable.
  5. plot was not coming correctly.

Steps were taken to overcome errors--

  1. Error(1&2) was due to a number of array elements, a number of expected results didn't match with the input. By correcting the matrix error was corrected.
  2. Error due to arguments in the ode function and it was correctedby giving the correct arguments.
  3. There was a dot operator in the formulas, by removing that dot operator error was corrected.
  4. Plot was not meeting the expectations so by making more changes to the code it was corrected.

https://youtu.be/4G9jDYHA5Ho

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.