Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to use Looping in a numerical computation?

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 19 Sep, 2010 00:52:03

Message: 1 of 13

I just want to use looping to carry out the result of a series.

for example: x_initial=1.1
x_n+1=10.01x_n - 10.01.
and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.

I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.

Am I on the right track here?

x = 1.1
for i =1:20
x = 10.1*x-10.01
end
disp(x)

Thanks in advance

Subject: How to use Looping in a numerical computation?

From: Walter Roberson

Date: 19 Sep, 2010 00:57:48

Message: 2 of 13

On 18/09/10 7:52 PM, Ryan wrote:
> I just want to use looping to carry out the result of a series.
> for example: x_initial=1.1
> x_n+1=10.01x_n - 10.01. and I want to find x_20. So I should carry out
> this series 19 times each time using the preceding result.
> I'm not too familiar with looping, so if somebody could help me out, I
> would be eternally grateful.
> Am I on the right track here?
>
> x = 1.1
> for i =1:20
> x = 10.1*x-10.01
> end
> disp(x)

Very close, but that would get you x_21 instead of x_20

Subject: How to use Looping in a numerical computation?

From: Ross W

Date: 19 Sep, 2010 01:02:03

Message: 3 of 13

"Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> I just want to use looping to carry out the result of a series.
>
> for example: x_initial=1.1
> x_n+1=10.01x_n - 10.01.
> and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
>
> I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
>
> Am I on the right track here?
>
> x = 1.1
> for i =1:20
> x = 10.1*x-10.01
> end
> disp(x)
>
> Thanks in advance

in your msg you say x_n+1=10.01x_n - 10.01 but in your code you say x = 10.1*x-10.01

so do you want 10.1 or 10.01?

Ross

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 19 Sep, 2010 01:57:03

Message: 4 of 13

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i73neb$ag7$1@fred.mathworks.com>...
> "Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> > I just want to use looping to carry out the result of a series.
> >
> > for example: x_initial=1.1
> > x_n+1=10.01x_n - 10.01.
> > and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
> >
> > I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
> >
> > Am I on the right track here?
> >
> > x = 1.1
> > for i =1:20
> > x = 10.1*x-10.01
> > end
> > disp(x)
> >
> > Thanks in advance
>
> in your msg you say x_n+1=10.01x_n - 10.01 but in your code you say x = 10.1*x-10.01
>
> so do you want 10.1 or 10.01?
>
> Ross

Oops. Sorry, typo. I want 10.1.

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 19 Sep, 2010 02:00:06

Message: 5 of 13

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i73neb$ag7$1@fred.mathworks.com>...
> "Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> > I just want to use looping to carry out the result of a series.
> >
> > for example: x_initial=1.1
> > x_n+1=10.01x_n - 10.01.
> > and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
> >
> > I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
> >
> > Am I on the right track here?
> >
> > x = 1.1
> > for i =1:20
> > x = 10.1*x-10.01
> > end
> > disp(x)
> >
> > Thanks in advance
>
> in your msg you say x_n+1=10.01x_n - 10.01 but in your code you say x = 10.1*x-10.01
>
> so do you want 10.1 or 10.01?
>
> Ross

Is there a way to do this using the while loop? Maybe that would be easier?

Subject: How to use Looping in a numerical computation?

From: Ross W

Date: 19 Sep, 2010 02:23:04

Message: 6 of 13

"Ryan " <riley127099@hotmail.com> wrote in message <i73qlf$18n$1@fred.mathworks.com>...
> "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i73neb$ag7$1@fred.mathworks.com>...
> > "Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> > > I just want to use looping to carry out the result of a series.
> > >
> > > for example: x_initial=1.1
> > > x_n+1=10.01x_n - 10.01.
> > > and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
> > >
> > > I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
> > >
> > > Am I on the right track here?
> > >
> > > x = 1.1
> > > for i =1:20
> > > x = 10.1*x-10.01
> > > end
> > > disp(x)
> > >
> > > Thanks in advance
> >
> > in your msg you say x_n+1=10.01x_n - 10.01 but in your code you say x = 10.1*x-10.01
> >
> > so do you want 10.1 or 10.01?
> >
> > Ross
>
> Oops. Sorry, typo. I want 10.1.

then yes it looks like you're calculating what you said you wanted to (noting Walter's comment)

Subject: How to use Looping in a numerical computation?

From: Roger Stafford

Date: 19 Sep, 2010 03:06:04

Message: 7 of 13

"Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> I just want to use looping to carry out the result of a series.
>
> for example: x_initial=1.1
> x_n+1=10.01x_n - 10.01.
> and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
>
> I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
>
> Am I on the right track here?
>
> x = 1.1
> for i =1:20
> x = 10.1*x-10.01
> end
> disp(x)
>
> Thanks in advance
- - - - - - - - -
  I think someone is playing tricks on you, Ryan. Each of the x_n's will remain exactly the same.

  The general case for this type of iteration of the form

 x_n+1 = a*x_n - b

can be done without any looping according to the formula

 x_n = a^n*(x_n - b/(a-1)) + b/(a-1);

  The reason is this. The equation

 x_n+1 = a*x_n - b,

is equivalent to

 x_n+1 - c = a*(x_n - c)

where c = b/(a-1). Hence

 x_n - c = a^n*(x_0 - c)
 x_n = a^n*(x_0 - c) + c

  In your case x_0 = 1.1, a = 10.1, b = 10.01, and c = b/(a-1) = 10.01/9.1 . However x_0-c = 1.1 - 10.01/9.1 = 1.1 - 1.1 = 0, so the x_n values remain constant at c = 1.1 .

Roger Stafford

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 19 Sep, 2010 03:14:04

Message: 8 of 13

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i73s68$5qe$1@fred.mathworks.com>...
> "Ryan " <riley127099@hotmail.com> wrote in message <i73qlf$18n$1@fred.mathworks.com>...
> > "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i73neb$ag7$1@fred.mathworks.com>...
> > > "Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> > > > I just want to use looping to carry out the result of a series.
> > > >
> > > > for example: x_initial=1.1
> > > > x_n+1=10.01x_n - 10.01.
> > > > and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
> > > >
> > > > I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
> > > >
> > > > Am I on the right track here?
> > > >
> > > > x = 1.1
> > > > for i =1:20
> > > > x = 10.1*x-10.01
> > > > end
> > > > disp(x)
> > > >
> > > > Thanks in advance
> > >
> > > in your msg you say x_n+1=10.01x_n - 10.01 but in your code you say x = 10.1*x-10.01
> > >
> > > so do you want 10.1 or 10.01?
> > >
> > > Ross
> >
> > Oops. Sorry, typo. I want 10.1.
>
> then yes it looks like you're calculating what you said you wanted to (noting Walter's comment)

Okay thanks to both you and Walter. Do I correct the code by changing the interval of i. Say i=1:19?

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 19 Sep, 2010 03:27:03

Message: 9 of 13

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i73ums$br4$1@fred.mathworks.com>...
> "Ryan " <riley127099@hotmail.com> wrote in message <i73mrj$4j7$1@fred.mathworks.com>...
> > I just want to use looping to carry out the result of a series.
> >
> > for example: x_initial=1.1
> > x_n+1=10.01x_n - 10.01.
> > and I want to find x_20. So I should carry out this series 19 times each time using the preceding result.
> >
> > I'm not too familiar with looping, so if somebody could help me out, I would be eternally grateful.
> >
> > Am I on the right track here?
> >
> > x = 1.1
> > for i =1:20
> > x = 10.1*x-10.01
> > end
> > disp(x)
> >
> > Thanks in advance
> - - - - - - - - -
> I think someone is playing tricks on you, Ryan. Each of the x_n's will remain exactly the same.
>
> The general case for this type of iteration of the form
>
> x_n+1 = a*x_n - b
>
> can be done without any looping according to the formula
>
> x_n = a^n*(x_n - b/(a-1)) + b/(a-1);
>
> The reason is this. The equation
>
> x_n+1 = a*x_n - b,
>
> is equivalent to
>
> x_n+1 - c = a*(x_n - c)
>
> where c = b/(a-1). Hence
>
> x_n - c = a^n*(x_0 - c)
> x_n = a^n*(x_0 - c) + c
>
> In your case x_0 = 1.1, a = 10.1, b = 10.01, and c = b/(a-1) = 10.01/9.1 . However x_0-c = 1.1 - 10.01/9.1 = 1.1 - 1.1 = 0, so the x_n values remain constant at c = 1.1 .
>
> Roger Stafford

Hi Roger,

You're right to a degree. While the "theoretical" result should remain constant at 1.1. The "computational" result is different.

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 19 Sep, 2010 03:33:04

Message: 10 of 13

By the way, is there a way I can call upon the results of each iteration. The way I've arranged my code depicts x as the 20th result of the iteration. But how can I subtract the 19th iteration result from the 20th? Is there a way to do this?

Thanks again.

Subject: How to use Looping in a numerical computation?

From: Roger Stafford

Date: 19 Sep, 2010 04:15:05

Message: 11 of 13

"Ryan " <riley127099@hotmail.com> wrote in message <i73vu7$rln$1@fred.mathworks.com>...
> You're right to a degree. While the "theoretical" result should remain constant at 1.1. The "computational" result is different.
- - - - - - - -
  Any deviations from a constant 1.1 are due solely to round-off errors. The computer cannot represent 10.1 and 10.01 exactly. At each step in an iteration, past round-off errors become multiplied by a factor of 10.1, so after 20 iterations these errors are magnified by something like 10^20. In my opinion such results are totally meaningless. It just means your iteration algorithm is highly unstable with respect to round-off errors.

Roger Stafford

Subject: How to use Looping in a numerical computation?

From: Jason

Date: 19 Sep, 2010 12:12:04

Message: 12 of 13

"Ryan " <riley127099@hotmail.com> wrote in message <i7409g$jik$1@fred.mathworks.com>...
> By the way, is there a way I can call upon the results of each iteration. The way I've arranged my code depicts x as the 20th result of the iteration. But how can I subtract the 19th iteration result from the 20th? Is there a way to do this?
>
> Thanks again.

Inside the for loop, you can always assign the previous result to a new variable before evaluating the new result. You can then add a conditional statement to just display the difference of the new and previous iterations you want.

Jason

Subject: How to use Looping in a numerical computation?

From: Ryan

Date: 20 Sep, 2010 03:12:04

Message: 13 of 13

"Jason" <mrgone@hotmail.com> wrote in message <i74umk$h08$1@fred.mathworks.com>...
> "Ryan " <riley127099@hotmail.com> wrote in message <i7409g$jik$1@fred.mathworks.com>...
> > By the way, is there a way I can call upon the results of each iteration. The way I've arranged my code depicts x as the 20th result of the iteration. But how can I subtract the 19th iteration result from the 20th? Is there a way to do this?
> >
> > Thanks again.
>
> Inside the for loop, you can always assign the previous result to a new variable before evaluating the new result. You can then add a conditional statement to just display the difference of the new and previous iterations you want.
>
> Jason

Thanks Jason,

could you specify how I can go about doing this for more than 2 variables. Essentially, I want to find the absolute value of all the results minus 1.1. Afterwards, I want to subtract each successive iteration from the preceding iteration. So I want to subtract x_20-x_19, x_19-x_18, x_19-x_17. How exactly do I assign different variables to each iteration result. Will I need to drastically alter my coding?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us