Matlab and Pendulum function - Need Help - Thank you.

function T= pendulum( L, a0 )
%PENDULUM Summary of this function goes here
% Detailed explanation goes here
%initializing variables
alpha = a0;
delta_t = 1*10^-6;
g = 9.8;
stop = false;
theta = a0;
t = 0;
omegalast =0
%Set stop = falsE so that loop stop isn't true at start
while stop == false;
%increment t for each step into loop
t = t+ delta_t;
%compute alpha using given formula
alpha = - g/L*sin(theta);
%compute omega as alpha times timestep, delta_t
omega = alpha*delta_t;
%theta is then theta plus omega times delta_t
theta = theta + omega*delta_t;
%check to see if pendulum speed has
%slowed down from the last timestep
%This indicatest that pendulum is now past the
%minimum point so stop program
if abs(omega) <abs(omegalast)
stop = true;
end
%IF THE SOFTWARE SHOULD STOP IT IS NOW SET TO DO SO
%SET OMEGALAST EQUAL TO OMEGA FOR CHECK IN NEXT TIME STEP
omegalast = omega;
end
%COMPUTE PERIOD AS 4 TIMES ELAPSED TIME TO BOTTOM OF PENDULUM
T = 4*t;
end
This isn't producing the right answers,and I don't know why. Perhaps a physic or math major can help me?
The problem is stated as:
* Write a function called pendulum that is called like this: T = pendulum(L,a0), where all arguments are scalars and a0 is a positive number less than π. The function calculates the period T of a simple pendulum, which is the time required for a weight attached to a rod of length L and negligible weight to start from rest, swing with no friction under the influence of gravity from an initial angle a0, to – a0 and back to a0 again, as shown in the figure. The motion is determined by physics using the following definitions, where units [square brackets] are provided but are not needed: θ = angle [radians]

7 commentaires

<p>*Problem Statement*
%
% *** Write a function called pendulum that is called like
this: T = pendulum(L,a0), where all arguments are
scalars and a0 is a positive number less than π. The
function calculates the period T of a simple pendulum,
which is the time required for a weight attached to a rod
of length L and negligible weight to start from rest, swing
with no friction under the influence of gravity from an initial angle a0, to – a0 and back to a0 again,
as shown in the figure. The motion is determined by physics using the following definitions, where
units [square brackets] are provided but are not needed:
θ = angle [radians]
DJ V
DJ V le 6 Déc 2016
Modifié(e) : DJ V le 6 Déc 2016
Problem Statement
<pre class="language-matlab">*** Write a function called pendulum that is called like
this: T = pendulum(L,a0), where all arguments are
scalars and a0 is a positive number less than π. The
function calculates the period T of a simple pendulum,
which is the time required for a weight attached to a rod
of length L and negligible weight to start from rest, swing
with no friction under the influence of gravity from an initial angle a0, to – a0 and back to a0 again,
as shown in the figure. The motion is determined by physics using the following definitions, where
units [square brackets] are provided but are not needed:
θ = angle [radians]
Perhaps a little easier to read with indentation:
function T= pendulum( L, a0 )
%PENDULUM Summary of this function goes here
% Detailed explanation goes here
%initializing variables
alpha = a0;
delta_t = 1*10^-6;
g = 9.8;
stop = false;
theta = a0;
t = 0;
omegalast =0
%Set stop = false so that loop stop isn't true at start
while stop == false;
%increment t for each step into loop
t = t+ delta_t;
%compute alpha using given formula
alpha = - g/L*sin(theta);
%compute omega as alpha times timestep, delta_t
omega = alpha*delta_t;
%theta is then theta plus omega times delta_t
theta = theta + omega*delta_t;
%check to see if pendulum speed has
%slowed down from the last timestep
%This indicatest that pendulum is now past the
%minimum point so stop program
if abs(omega) <abs(omegalast)
stop = true;
end
%IF THE SOFTWARE SHOULD STOP IT IS NOW SET TO DO SO
%SET OMEGALAST EQUAL TO OMEGA FOR CHECK IN NEXT TIME STEP
omegalast = omega;
end
%COMPUTE PERIOD AS 4 TIMES ELAPSED TIME TO BOTTOM OF PENDULUM
T = 4*t;
end
<pre class="language-matlab">*** Write a function called pendulum that is called like
this: T = pendulum(L,a0), where all arguments are
scalars and a0 is a positive number less than π. The
function calculates the period T of a simple pendulum,
which is the time required for a weight attached to a rod
of length L and negligible weight to start from rest, swing
with no friction under the influence of gravity from an initial angle a0, to – a0 and back to a0 again,
as shown in the figure. The motion is determined by physics using the following definitions, where
units [square brackets] are provided but are not needed:
θ = angle [radians]
DJ V
DJ V le 6 Déc 2016
Modifié(e) : DJ V le 6 Déc 2016
CONTINUED
<pre class="language-matlab">*** Write a function called pendulum that is called like
this: T = pendulum(L,a0), where all arguments are
scalars and a0 is a positive number less than π. The
function calculates the period T of a simple pendulum,
which is the time required for a weight attached to a rod
of length L and negligible weight to start from rest, swing
with no friction under the influence of gravity from an initial angle a0, to – a0 and back to a0 again,
as shown in the figure. The motion is determined by physics using the following definitions, where
units [square brackets] are provided but are not needed:
θ = angle [radians]
DJ V
DJ V le 6 Déc 2016
Modifié(e) : DJ V le 6 Déc 2016
CODE WITH INDENTS:
function T= pendulum( L, a0 )
%PENDULUM Summary of this function goes here
% Detailed explanation goes here
%initializing variables
alpha = a0;
delta_t = 1*10^-6;
g = 9.8;
stop = false;
theta = a0;
t = 0;
omegalast =0
%Set stop = false so that loop stop isn't true at start
while stop == false;
%increment t for each step into loop
t = t+ delta_t;
%compute alpha using given formula
alpha = - (g/L)*sin(theta);
%compute omega as alpha times timestep, delta_t
omega = alpha*delta_t;
%theta is then theta plus omega times delta_t
theta = theta + omega*delta_t;
%check to see if pendulum speed has
%slowed down from the last timestep
%This indicatest that pendulum is now past the
%minimum point so stop program
if abs(omega) <abs(omegalast)
stop = true;
end
%IF THE SOFTWARE SHOULD STOP IT IS NOW SET TO DO SO
%SET OMEGALAST EQUAL TO OMEGA FOR CHECK IN NEXT TIME STEP
omegalast = omega;
end
%COMPUTE PERIOD AS 4 TIMES ELAPSED TIME TO BOTTOM OF PENDULUM
T = 4*t;
end

Connectez-vous pour commenter.

 Réponse acceptée

DJ - you've missed two important points in the problem description. The first is the
...the angular velocity omega is increased by the product of the angular acceleration and delta t..
This means that instead of
omega = alpha*delta_t;
you would initialize omega to zero (outside of your while loop since the pendulum is initially at rest) and then increase it (on each iteration) as
omega = omega + alpha*delta_t;
And the algorithm
...continues until the pendulum has passed its lowest point, at which theta equals zero...
So you would break out of your while loop once theta is near or less than zero.

2 commentaires

DJ V
DJ V le 6 Déc 2016
That was it. Thanks a lot!
please sir if you can showcase your final and correct code here, it would be very helpful.

Connectez-vous pour commenter.

Plus de réponses (3)

Try this code:
function [T] = pendulum(L,a0)
T = 0;
if L > 0
dt = 1.e-6;
a_velocity = 0;
g = 9.8;
theta = abs(a0);
while theta > 0
a_acceleration = g*sin(theta)/L;
a_velocity = a_velocity + dt * a_acceleration;
theta = theta - dt * a_velocity;
T = T + dt;
end
T = T*4;
end
end
Huan Yang
Huan Yang le 8 Juin 2017

1 vote

function T=pendulum(L,a0) cta=a0; t=10^-6; w=0; T=0; while cta>0 T=T+t; a=-9.8/L*sin(cta); w=w+a*t; cta=cta+w*t; end
T=T*4;
end

1 commentaire

Thanks Huan Yang but there is a lil bit mistake in this code. In order to fix this code to work properly is to just add to while loop L~= 0 since the division cannot be infinite and this is exact solution.
....
while cta>0 && L~= 0 may fix your solution.

Connectez-vous pour commenter.

Weronika Izdebska
Weronika Izdebska le 4 Sep 2018

0 votes

function T = pendulum(L,angle0) T = 0; if L > 0 dt = 1e-6; g = 9.8; angle = abs(angle0); omega = 0; T = 0; while angle > 0 a = g*sin(angle)/L; omega = omega + dt * a; angle = angle - dt * omega; T = T + dt; end T = T * 4; end end

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by