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:
forward summation

Subject: forward summation

From: Chris

Date: 17 Sep, 2010 04:33:04

Message: 1 of 3

I want to sum the expression forwards (and backwards):

n
summation : 1/(i^4)
i=1

right now I have:

function [forward backward] = summ(k)
format long %shows 7 digits after decimal
sum = 0; % start sum at zero
sum2 = 0;
               for h = 1:k
                    forward = sum + 1/(k^4); %forward summation
               end

               for h = -1:-k
                    backward = sum2 + 1/(k^4); %backward summation
               end
end

I know the forward sum with 38 terms should be 1.082318 but my result comes out to be 4.795850246698537e-007 and I cannot figure out why.
Is my summation wrong?

Subject: forward summation

From: Roger Stafford

Date: 17 Sep, 2010 05:03:04

Message: 2 of 3

"Chris " <ridered300tr@hotmail.com> wrote in message <i6ur20$2lt$1@fred.mathworks.com>...
> I want to sum the expression forwards (and backwards):
>
> n
> summation : 1/(i^4)
> i=1
>
> right now I have:
>
> function [forward backward] = summ(k)
> format long %shows 7 digits after decimal
> sum = 0; % start sum at zero
> sum2 = 0;
> for h = 1:k
> forward = sum + 1/(k^4); %forward summation
> end
>
> for h = -1:-k
> backward = sum2 + 1/(k^4); %backward summation
> end
> end
>
> I know the forward sum with 38 terms should be 1.082318 but my result comes out to be 4.795850246698537e-007 and I cannot figure out why.
> Is my summation wrong?
- - - - - - - -
  Your "sum" 4.795850246698537e-007 is simply 1/38^4. You have set up your for-loop improperly in more than one respect. It should be

 sum = 0;
 for k = 1:100000
  sum = sum + 1/k^4;
 end

However, it would be more accurate with respect to accumulating round off errors if you summed backwards:

 sum = 0;
 for k = 100000:-1:1
  sum = sum + 1/k^4;
 end

This sum would be extremely close to pi^4/90 if you do it this way.

  Your other for-loop is completely wrong. You need to do some more study of matlab basics.

Roger Stafford

Subject: forward summation

From: Ross W

Date: 17 Sep, 2010 05:59:07

Message: 3 of 3

"Chris " <ridered300tr@hotmail.com> wrote in message <i6ur20$2lt$1@fred.mathworks.com>...
> I want to sum the expression forwards (and backwards):
>
> n
> summation : 1/(i^4)
> i=1
>
> right now I have:
>
> function [forward backward] = summ(k)
> format long %shows 7 digits after decimal
> sum = 0; % start sum at zero
> sum2 = 0;
> for h = 1:k
> forward = sum + 1/(k^4); %forward summation
> end
>
> for h = -1:-k
> backward = sum2 + 1/(k^4); %backward summation
> end
> end
>
> I know the forward sum with 38 terms should be 1.082318 but my result comes out to be 4.795850246698537e-007 and I cannot figure out why.
> Is my summation wrong?

Hi

I guess this code is being used to check the effect of changing k, the total number of terms.

your first loop says

               for h = 1:k
                    forward = sum + 1/(k^4); %forward summation
               end

but where it says k^4, you should have h^4

your second loop has the same problem. it also has another problem - if you want to go backwards then you need to use a FOR-loop from k to 1 with increments of -1. read the help to find out how.

Ross

Tags for this Thread

No tags are associated with 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