**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Anyone could help me solving this problem or could you refer me to any video or link that can help. I have been trying to solve it for couple of hours. Your help will be appreciated.

1 view (last 30 days)

Show older comments

##### 8 Comments

Walter Roberson
on 19 May 2017

Steven Lord
on 19 May 2017

Wasi von Deutschland
on 19 May 2017

Edited: Walter Roberson
on 19 May 2017

function T= pendulum( L, a0 )

delta_t = 1*10^-6;

g = 9.8;

t=0;

theta = a0;

while theta==0;

t = t- delta_t;

acc = - g/L*sin(theta);

vel=acc*t;

theta = theta + vel*t;

end

T = 4*t;

end

this is my code what I have done so far.

Walter Roberson
on 19 May 2017

Walter Roberson
on 20 May 2017

Wasi von Deutschland
on 20 May 2017

should be t=t+delta_t....? but my Auto grader says it's incorrect

Walter Roberson
on 20 May 2017

### Answers (1)

Walter Roberson
on 21 May 2017

You have

theta = a0;

while theta==0;

Your input, a0, is said to be a positive number less than pi. As it is a positive number, the test theta == 0 will fail, so the body of

while theta==0

is never going to execute.

I suggest you consider theta>0 instead of theta==0

##### 22 Comments

Wasi von Deutschland
on 22 May 2017

it doesn't work so far, but I think it's because of t.

function T= pendulum( L, a0 )

delta_t = 1*10^-6;

g = 9.8;

t=0;

theta = a0;

while theta>0;

delta_t=0:delta_t;

acc = - g/L*sin(theta);

vel=acc*t;

theta = theta + vel*t;

end

T = 4*t;

end

Walter Roberson
on 22 May 2017

Why are you changing delta_t ? Why are you not changing t?

Note:

delta_t = 1*10^-6;

delta_t=0:delta_t;

together would be like

delta_t = 0:1*10^-6

which in turn means

delta_t = 0 : 1 : 1*10^-6

and with 1*10^-6 being less than 0+1, there is only room for a single value in the range, namely 0 itself. So your

delta_t=0:delta_t;

is the same as

delta_t = 0;

which is clearly wrong.

Wasi von Deutschland
on 22 May 2017

Actually it's not fine. Could you please tell me where I'm still having problem?

function T= pendulum( L, a0 )

delta_t=1*10^-6;

g = 9.8;

theta = a0;

while theta>0;

t =0:1 : delta_t;

vel=acc*delta_t;

theta = theta + vel*delta_t;

acc = - g*sin(theta)/L;

end

T = 4*t;

end

Walter Roberson
on 23 May 2017

I went through and explained why 0:1:delta_t is going to give you just 0.

You should not be assigning t a range inside the loop. You should be adding delta_t to t, like we discussed before.

Meanwhile... you have not initialized acc.

Wasi von Deutschland
on 23 May 2017

I'm really sorry to say but I still have some confusion. I'm very beginner

- t should not be inside the loop that what you meant.
- t for first periodic movement is equals to 0 and then t=t+delta_t
- theta should be inside loop while theta>0 or while == 0;

Walter Roberson
on 23 May 2017

Just making this pair of corrections to what you wrote earlier, and not attempting to verify that any of the rest of the code is correct:

function T= pendulum( L, a0 )

delta_t = 1*10^-6;

g = 9.8;

t=0;

theta = a0;

while theta>0;

t = t + delta_t;

acc = - g/L*sin(theta);

vel = acc*t;

theta = theta + vel*t;

end

T = 4*t;

end

You should consider, though, that your change in angle does not depend upon your current velocity multiplied by the entire time that you have been swinging (t): your change in angle depends upon your current velocity multiplied by the change in time over which you are swinging (delta_t)

Wasi von Deutschland
on 23 May 2017

Could you please verify the whole code? I don't think I can figure out spending couple of hours more. It's been 3 days so far.

function T= pendulum( L, a0 )

delta_t = 1*10^-6;

g = 9.8;

t=0;

theta = a0;

while theta>0;

t = t + delta_t;

acc = - g/L*sin(theta);

vel = acc*delta_t; %fix delta_t for velocity

theta = theta + vel*t; % current velocity multiplied by change in time

end

T = 4*t;

end

Walter Roberson
on 23 May 2017

Your line

theta = theta + vel*t; % current velocity multiplied by change in time

is not multiplying by the change in time, it is multiplying by the entire time the system has been in operation.

Wasi von Deutschland
on 23 May 2017

should it be like this?, what would you say about the rest of the code.

theta = theta + vel*delta_t;

Wasi von Deutschland
on 24 May 2017

loop doesn't end. It's busy. I am using this code

function T= pendulum( L, a0 )

delta_t = 1*10^-6;

g = 9.8;

t=0;

theta = a0;

while theta>0;

t = t + delta_t;

acc = - g/L*sin(theta);

vel = acc*delta_t;

theta = theta + vel*delta_t;

end

T = 4*t;

end

Walter Roberson
on 24 May 2017

Wasi von Deutschland
on 25 May 2017

Sorry but I didn't understand you. did you mean velocity should have been increased by adding t

vel=acc*delta_t+t

Wasi von Deutschland
on 25 May 2017

I don't know what should I say.? I really appreciate your effort but my answer is still incorrect.

function T= pendulum( L, a0 )

delta_t = 1*10^-6;

g = 9.8;

t=0;

theta = a0;

while theta<=0

t = t + delta_t;

acc = - g/L*sin(theta);

vel = vel + acc*delta_t;

theta = theta+ vel*delta_t;

end

T = 4*t;

end

Walter Roberson
on 25 May 2017

Wasi von Deutschland
on 26 May 2017

The one other thing I've noticed is vel is not defined how can it be used?

Wasi von Deutschland
on 26 May 2017

Walter Roberson
on 26 May 2017

Your code appears to return 4e-6 when called with argument 0, 1. That is a completely reasonable answer for the method of calculation that you are directed to use.

It is not a fair question: although the theoretic answer is that a pendulum of length 0 has infinite velocity and 0 period, it is also the case that a pendulum of length 0 is a point source and so cannot be said to move through any angle. Your given task is not to calculate the theoretical answer but rather to simulate the movement, and simulation requires a minimum of one time-step.

Their description of the calculation specifically talks about counting until the pendulum has "passed through" its lowest point, and if you can say with any assurance that the pendulum is "at" 1 radian then it takes a time-step to evolve to be "at" any other angle. If a pendulum of length 0 is "at" 1 radian at time 0, then no seconds later it cannot have changed to be "at" a different angle. If you want to talk about a 0 length pendulum then you would have to say that the angle is undefined, which would correspond to pendulum(1, nan) and your code returns 0 for that.

### See Also

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)