Question

MATLAB code for a double pendulum. Please explain each lines for these codes pls. -------------------------------------...

MATLAB code for a double pendulum. Please explain each lines for these codes pls.

----------------------------------------------------------------------------
clc
close all
clear all

%---------Parameters------------------------------------------------------

L1=1; L2=1 ; M_1=2 ; M_2=1; G=9.8;

%---------initial condition-----------------------------------------------

tspan=30;
theta1=3;
theta1_prime=0;
theta2=2.5;
theta2_prime=0;

y0=[theta1 theta1_prime theta2 theta2_prime];
[t,y]=ode45(@pend, [0 ,tspan],[ 3 0 2 0]);


%---position of mass 1 and mass 2----------------------------------------

x1=L1*sin(y(:,1));
y1=-L1*cos(y(:,1));
x2=L1*sin(y(:,1))+l2*sin(y(:,3));
y2=-L1*cos(y(:,1))-l2*cos(y(:,3));

%------visualizing the result---------------------------------------------

figure(1)
plot(x1,y1,'linewidth',2)
hold on
plot(x2,y2,'r','linewidth',2)
h=gca;
get(h,'fontSize')
set(h,'fontSize',14)
xlabel('X','fontSize',14);
ylabel('Y','fontSize',14);
title('Chaotic Double Pendulum','fontsize',14)
fh = figure(1);
set(fh, 'color', 'white');

figure(2)
plot(y(:,1),'linewidth',2)
hold on
plot(y(:,3),'r','linewidth',2)
h=gca;
get(h,'fontSize')
set(h,'fontSize',14)
legend('\theta_1','\theta_2')
xlabel('time','fontSize',14);
ylabel('theta','fontSize',14);
title('\theta_1(t=0)=2.5 and \theta_2(t=0)=1.0','fontsize',14)
fh = figure(2);
set(fh, 'color', 'white');


%----movie of double pendulum--------------------------------------

figure(3)
Ncount=0;
fram=0;

for i=1:length(y)
Ncount=Ncount+1;
fram=fram+1;
plot(0, 0,'.','markersize',20);
hold on
plot(x1(i),y1(i),'.','markersize',20);
plot(x2(i),y2(i),'.','markersize',20);
hold off
line([0 x1(i)], [0 y1(i)],'Linewidth',2);
axis([-(L1+l2) L1+l2 -(L1+l2) L1+l2]);
line([x1(i) x2(i)], [y1(i) y2(i)],'linewidth',2);
h=gca;
get(h,'fontSize')
set(h,'fontSize',12)
xlabel('X','fontSize',12);
ylabel('Y','fontSize',12);
title('Chaotic Motion','fontsize',14)
fh = figure(3);
set(fh, 'color', 'white');
F=getframe;
end

movie(F,fram,20)
  
%-----------------------------------------------------------------------   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the explanation of each line. I have commented against the lines

%--------------------------------------------------------------------------

%-------------- Double Pendulum--------------------------------------------

%--------------------------------------------------------------------------

clc

close all

clear all

%---------Parameters------------------------------------------------------

l1=1; l2=2 ; m1=2 ; m2=1; g=9.8;

%---------initial condition-----------------------------------------------

tspan=50;

theta1=1.6;

theta1_prime=0;

theta2=2.2;

theta2_prime=0;

y0=[theta1 theta1_prime theta2 theta2_prime];

[t,y]=ode45(@pend, [0 ,tspan],[ 2.5 0 1 0]); %This gives the time vs y vectors

%---position of mass 1 and mass 2----------------------------------------

x1=l1*sin(y(:,1)); %This would give the x coordinates of the mass 1

y1=-l1*cos(y(:,1)); %This would give the y coordinates of the mass 1

x2=l1*sin(y(:,1))+l2*sin(y(:,3));  %This would give the x coordinates of the mass 2

y2=-l1*cos(y(:,1))-l2*cos(y(:,3));  %This would give the y coordinates of the mass 2

%------visualizing the result---------------------------------------------

figure(1)

plot(x1,y1,'linewidth',2) %This would plot out the entire motion of mass 1. The linewidth would be 2.

hold on

plot(x2,y2,'r','linewidth',2) %This would plot out the entire motion of mass 2. The linewidth would be 2.

h=gca; %This would get the current axis handles. If there is no axis, then matlab makes axis and returns it

get(h,'fontSize')

set(h,'fontSize',14) %This is the fontsize of the various markings on the handle

xlabel('X','fontSize',14); %Labels the x-axis

ylabel('Y','fontSize',14); %Labels the y-axis

title('Chaotic Double Pendulum','fontsize',14) %The title of the graph is set here

fh = figure(1);

set(fh, 'color', 'white'); %These two expressions would set and show the above figure.

figure(2)

plot(y(:,1),'linewidth',2) %This would plot the 1st column of y matrix with linewidth 2

hold on

plot(y(:,3),'r','linewidth',2) %This would plot the 3rd column of y matrix with linewidth 2

h=gca;

get(h,'fontSize')

set(h,'fontSize',14)

legend('\theta_1','\theta_2') %This would show the legend showing which lines is theta_1 and theta_2

xlabel('time','fontSize',14);

ylabel('theta','fontSize',14);

title('\theta_1(t=0)=2.5 and \theta_2(t=0)=1.0','fontsize',14)

fh = figure(2);

set(fh, 'color', 'white');

%----movie of double pendulum--------------------------------------

figure(3)

Ncount=0;

fram=0; %Initial counts are intialized

for i=1:length(y)

Ncount=Ncount+1; %After each point, the count is increased

fram=fram+1; %This indicates the frame number.

plot(0, 0,'.','markersize',20); %The initial point is marked with an '.'

hold on %The code in between hold on and hold off would stay on.

plot(x1(i),y1(i),'.','markersize',20); %Plots the postions of 1st mass over time.

plot(x2(i),y2(i),'.','markersize',20); %Plots the positions of 2nd mass over time.

hold off

line([0 x1(i)], [0 y1(i)],'Linewidth',2); %A line will be made with x points as 0 to x1(i) and y points as 0 to y1(i).

axis([-(l1+l2) l1+l2 -(l1+l2) l1+l2]); %Sets the limits for x and y axis to be shown on screen

line([x1(i) x2(i)], [y1(i) y2(i)],'linewidth',2);

h=gca;

get(h,'fontSize')

set(h,'fontSize',12)

xlabel('X','fontSize',12);

ylabel('Y','fontSize',12);

title('Chaotic Motion','fontsize',14)

fh = figure(3);

set(fh, 'color', 'white');

F=getframe;

end

movie(F,fram,20) %This would show the various frames on the screen for a set interval and it would give an illusion of a movie as the frames would be shown on the screen very fast

%-----------------------------------------------------------------------

If any doubts, please comment below.

If this answer helped you, please leave a thumbs up :)

Add a comment
Know the answer?
Add Answer to:
MATLAB code for a double pendulum. Please explain each lines for these codes pls. -------------------------------------...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please help me with this MATLAB CODE and explain to me what each line does and...

    please help me with this MATLAB CODE and explain to me what each line does and what is used for? leave your comments as words, not as pictures. ..................................................................................................................................................................... clear all; close all; % For a script file, you better start with clear all and close all                        % However, for a fucntion, you better NOT to start                        % with them %% End of cell mode example %% Plot function t = 0:0.1:5; x1 = sin(2*5*t); x2 = cos(3*7*t);...

  • Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current...

    Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current program stops the animation and then has the crank move backward. I need the crank to instead to move in full 360 degrees circular motion. Exactly like how a slide-crank mechanism works in real life. thank you and here is the code. %%Code Begins Here%% L1=0.2; L2=0.45; W=0.5; tt=linspace(0,15); for i=1:length(tt)     x2=@(t) L2+L1*sin(W*t);     y2=0;     X2=x2(tt(i));     Y2=y2;     sol= fsolve(@(x,x2,y2) root2d(x,X2,Y2),...

  • write a matlab code that can plot 3 excel files? this plot know can run one...

    write a matlab code that can plot 3 excel files? this plot know can run one excel file of 20crackf1. clc; clear all; close all; data1=xlsread('20f1','spectrum - PXI1Slot4_ai0','A8:B10009');% Importing excel file x=data1(:,1); y=data1(:,2); plot(x,y); xlim([0 100]); xlabel('Frequency in Hz'); ylabel('Amplitude in g'); title('FFT'); hold on; data2=xlsread('20crackf1','a1','A1:B5000'); x1=data2(:,1); y1=data2(:,2); plot(x1,y1); xlim([0 100]);

  • How can I get my while loop to run until my condition in my if statement...

    How can I get my while loop to run until my condition in my if statement is met? (In MATLAB) clc%clears screen clear all%clears history close all%closes all files format long %Euler's Method %Initial conditions and setup %Projectile motion x=3;%randi(3); y=3;%randi(3); figure('color','white'); plot(x,y,'bo'); hold on wind = (-10 + 20*rand(1)); fprintf('The wind speed is %2.2i\n',wind); v0= input('Initial velocity(m/s): '); a= input('Angle of projection(in degrees): '); a=a*pi/180; h=.01; cd=.1; %cdy=.1; %cdx=(rand*2-1)/2; %cdy=(rand*2-1)/2; m=1.5; g=9.8; %acceleration due to gravity in m/s^2 %td=2*v0*sin(a)/g;...

  • i got it incomplete sorry I put this by mistake. can this be canceled? 4. Use MATLAB or Scilab to implement linea...

    i got it incomplete sorry I put this by mistake. can this be canceled? 4. Use MATLAB or Scilab to implement linear regression, and plot the points and the curve for the following data sets. x [018 12 27) and y [12345 Input: (copy and paste the MATLAB or Sclab commandin the folowing bax) cic X1-101 8 12 271: Y1-11 234 5] Y-transpose(Y1) for n-1:5 X(n,2)-X1(n) end format long ycalc-S(2) X+S(1); scatter(X1,Y1) hold on plot(X,ycalc) xlabel(X) ylabel(Y) title('Linear Regression Relation...

  • Is the function being called correctly? format shorteng r = linspace(0.1,4,500); % Distance in Angstrom re...

    Is the function being called correctly? format shorteng r = linspace(0.1,4,500); % Distance in Angstrom re = 0.7414; % Equilibrium Seperation D = 38292; % Dissoc Enrgey [cm^-1] twoBeta = 1.4426; Vm = morse_potential(D, twoBeta, re, r); % Call Morse Potential Function plot(r,Vm, 'g-','Linewidth',2) xlabel('Distance [A]') ylabel('Potential Energy [cm^-1]') title('Morse Potential of Hydrogens') ylim([0,5e4]) grid 'on' hold on D2 = 38292; % Equiibrium distance re2 = 0.7413; % Width of the potential well for H2 twoBeta2 = 1.4433; r =...

  • I wrote a Matlab program for the figure below. When I plot the waves, they look...

    I wrote a Matlab program for the figure below. When I plot the waves, they look the same. Why do the two waves frequencies look same and How do I avoid it? (I really do need this part of the question answered.) N = 200; % Total number of time domain samples in simulation. Fs = 100 ;% sampling frequency. F1 = 10; % frequency of wave - 1. F2 = 90; % frequency of wave - 2. phi =...

  • Then we can use the cylinder function to revolve those profiles around the z-axis and plot them. ...

    Please help me with this MATLAB programming problem! Im using MATLAB 2018 if that helps. We were unable to transcribe this imageThen we can use the cylinder function to revolve those profiles around the z-axis and plot them. t Now revolve it around the z-axis figure (4) x1,yl,z1 cylinder (profilel) clf: surf (xl, yl, z1) axis vis3d; shading faceted; colormap (jet) xlabel ) ylabel (Cy) zlabel (z 02 [x2.y2.z21cylinder (profflez): hold on: surf (x2, y2, z2) 10 -10 10 Note...

  • I have been trying this problem for over 8 hours. How do I make the Matlab code for the given problem. It has to move. - Use the . You may use cif to clear the figure for the next instant of time...

    I have been trying this problem for over 8 hours. How do I make the Matlab code for the given problem. It has to move. - Use the . You may use cif to clear the figure for the next instant of time The goal is the same as before except this time you will plot a moving 3 bar lin mechanism shown below kage. Use the L2 (k2,ya L3 The equations that govern the motion of this linkage are:...

  • Use the circuit below to complete the given MATLAB script to plot the three currents I1,...

    Use the circuit below to complete the given MATLAB script to plot the three currents I1, I2, and I3. Run MATLAB then pick an answer from the choices below that matches the MATLAB plot. MATLAB uncomplete script %phasor plot clc; clear all; close all; Z1=2; Z2=2j; Z3=2-2j; %no need to use the * as in 6*j %Vs=8/_45 theta=45*pi/180; %convert theta to radians R=8; [a,b]=pol2cart(theta,R); %note theta first in the arguments Vs=a+j*b; %cartesian form Z23= ___________; % Complete Zeq=____________ ; %...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT