Week 3 - Solving second order ODEs
Report--
Steps
- A separate function is written for solving the differential equation.
- pendulum parameters are initialized.
- To obtain the motion equation, time and angular displacement are mentioned.
- the results are obtained by solving the differential equation using ODE45.
- 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
), y-coordinate=L(1-cos(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) - for every value of theta obtained from the equation x and y coordinates are calculated using the above formulae and stored.-----(first for loop)
- Later for every value of x and y graph is plotted.----(second for loop)
- to create an animation look,ct is used.
- Avi video file is created by using 'videowriter'.
ERRORS--
- Index exceeds the number of array elements. the index must not exceed.
- Array indices must be positive or local values.
- Error in odearguments.
- Dot indexing is not supported for this type of variable.
- plot was not coming correctly.
Steps were taken to overcome errors--
- 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.
- Error due to arguments in the ode function and it was correctedby giving the correct arguments.
- There was a dot operator in the formulas, by removing that dot operator error was corrected.
- Plot was not meeting the expectations so by making more changes to the code it was corrected.