Evaluating a series

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Evaluating a series

K D Kavu
Please assist me to do this:
Evaluate the series

1-1/3+1/5-1/7+1/9-...+1/1001

Duncan
Sent from my BlackBerry® wireless device
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Duncan
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Søren Hauberg

On Aug 9, 2012, at 9:22 AM, [hidden email] wrote:

> Please assist me to do this:
> Evaluate the series
>
> 1-1/3+1/5-1/7+1/9-...+1/1001

You can get the positive part of the sequence by doing something like

  1 ./ (1:4:1001)

summing that will give you part of your result. You can do the same for the negative part and subtract that from the positive part.

Søren
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

martin_helm
Am 09.08.2012 09:30, schrieb Søren Hauberg:

> On Aug 9, 2012, at 9:22 AM, [hidden email] wrote:
>
>> Please assist me to do this:
>> Evaluate the series
>>
>> 1-1/3+1/5-1/7+1/9-...+1/1001
> You can get the positive part of the sequence by doing something like
>
>   1 ./ (1:4:1001)
>
> summing that will give you part of your result. You can do the same for the negative part and subtract that from the positive part.
>
> Søren
>
I have not thought very deeply about it, but wouldn't it be more stable
to group every pair of differences together
1/n - 1/(n+2) = 2/( n*(n+2) ) so that only positive numbers are summed
and no subtractions take place?
Just a thought without details, since that sounds like homework somehow
(i also matters in which order the summation is done, not really for
this small example).
 
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

nrjank
In reply to this post by K D Kavu
On Thu, Aug 9, 2012 at 3:22 AM,  <[hidden email]> wrote:
> Please assist me to do this:
> Evaluate the series
>
> 1-1/3+1/5-1/7+1/9-...+1/1001
>
> Duncan
> Sent from my BlackBerry® wireless device
>

While there may be more elegant ways to do this, you're basically
trying to repeat an addition operation of items with changing value.
So, you need two things:

(1) an expression for the changing value. Something as a function of
'n' that gives you the right value (i.e., n=1, you get 1, n=2, you get
-1/3...)
(2) you need a way of adding them all together until you get up to
1/1001.  For programming or scripting languages like Octave, you can
use a looping structure. maybe a for loop if you know how many
elements, or a do-while loop if you want it to run until a condition
is met. from within Octave, to get the syntaxyou can type:

help for
or
help do

But, it sounds like you might need something more basic to get
started. I would recommend you flip through this to get a quick feel
for how to use Octave:

http://people.sc.fsu.edu/~sshanbhag/matlab.primer.pdf
(the filename says matlab, but the document title is "GNU
Octave/Matlab Primer" and it walks you through the basics.

Nick J.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Jordi Gutiérrez Hermoso-2
On 9 August 2012 08:26, Nicholas Jankowski <[hidden email]> wrote:
> On Thu, Aug 9, 2012 at 3:22 AM,  <[hidden email]> wrote:
>> Please assist me to do this:
>> Evaluate the series
>>
>> 1-1/3+1/5-1/7+1/9-...+1/1001
>>
>
> While there may be more elegant ways to do this, you're basically
> trying to repeat an addition operation of items with changing value.

e.g.

    sum(1./(1:4:1001) - 1./(3:4:1001))

?

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

andy buckle


On 9 August 2012 13:38, Jordi Gutiérrez Hermoso <[hidden email]> wrote:
On 9 August 2012 08:26, Nicholas Jankowski <[hidden email]> wrote:
> On Thu, Aug 9, 2012 at 3:22 AM,  <[hidden email]> wrote:
>> Please assist me to do this:
>> Evaluate the series
>>
>> 1-1/3+1/5-1/7+1/9-...+1/1001
>>
>
> While there may be more elegant ways to do this, you're basically
> trying to repeat an addition operation of items with changing value.

e.g.

    sum(1./(1:4:1001) - 1./(3:4:1001))

?

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave

>sum(1./(1:4:1001) - 1./(3:4:1001))
error: operator -: nonconformant arguments (op1 is 1x251, op2 is 1x250)
error: evaluating argument list element number 1
>sum(1./(1:4:1001)) - sum(1./(3:4:1001))
ans =  0.78590

--
/* andy buckle */

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

nrjank
On Thu, Aug 9, 2012 at 8:46 AM, Andy Buckle <[hidden email]> wrote:

>
>
> On 9 August 2012 13:38, Jordi Gutiérrez Hermoso <[hidden email]> wrote:
>>
>> On 9 August 2012 08:26, Nicholas Jankowski <[hidden email]> wrote:
>> > On Thu, Aug 9, 2012 at 3:22 AM,  <[hidden email]> wrote:
>> >> Please assist me to do this:
>> >> Evaluate the series
>> >>
>> >> 1-1/3+1/5-1/7+1/9-...+1/1001
>> >>
>> >
>> > While there may be more elegant ways to do this, you're basically
>> > trying to repeat an addition operation of items with changing value.
>>
>> e.g.
>>
>>     sum(1./(1:4:1001) - 1./(3:4:1001))
>>
>> ?
>>
>> - Jordi G. H.
>> _______________________________________________
>> Help-octave mailing list
>> [hidden email]
>> https://mailman.cae.wisc.edu/listinfo/help-octave
>
>
>>sum(1./(1:4:1001) - 1./(3:4:1001))
> error: operator -: nonconformant arguments (op1 is 1x251, op2 is 1x250)
> error: evaluating argument list element number 1
>>sum(1./(1:4:1001)) - sum(1./(3:4:1001))
> ans =  0.78590
>
> --
> /* andy buckle */

50 points from Gryffindor: for doing the kid's homework for him
instead of leading him to actually learning to use the program.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

andy buckle


On 9 August 2012 14:01, Nicholas Jankowski <[hidden email]> wrote:
On Thu, Aug 9, 2012 at 8:46 AM, Andy Buckle <[hidden email]> wrote:
>
>
> On 9 August 2012 13:38, Jordi Gutiérrez Hermoso <[hidden email]> wrote:
>>
>> On 9 August 2012 08:26, Nicholas Jankowski <[hidden email]> wrote:
>> > On Thu, Aug 9, 2012 at 3:22 AM,  <[hidden email]> wrote:
>> >> Please assist me to do this:
>> >> Evaluate the series
>> >>
>> >> 1-1/3+1/5-1/7+1/9-...+1/1001
>> >>
>> >
>> > While there may be more elegant ways to do this, you're basically
>> > trying to repeat an addition operation of items with changing value.
>>
>> e.g.
>>
>>     sum(1./(1:4:1001) - 1./(3:4:1001))
>>
>> ?
>>
>> - Jordi G. H.
>> _______________________________________________
>> Help-octave mailing list
>> [hidden email]
>> https://mailman.cae.wisc.edu/listinfo/help-octave
>
>
>>sum(1./(1:4:1001) - 1./(3:4:1001))
> error: operator -: nonconformant arguments (op1 is 1x251, op2 is 1x250)
> error: evaluating argument list element number 1
>>sum(1./(1:4:1001)) - sum(1./(3:4:1001))
> ans =  0.78590
>
> --
> /* andy buckle */

50 points from Gryffindor: for doing the kid's homework for him
instead of leading him to actually learning to use the program.

I would not have posted the answer had Jordi not almost posted the answer...

--
/* andy buckle */

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

nrjank
On Thu, Aug 9, 2012 at 9:07 AM, Andy Buckle <[hidden email]> wrote:

>
>
> On 9 August 2012 14:01, Nicholas Jankowski <[hidden email]> wrote:
>>
>> On Thu, Aug 9, 2012 at 8:46 AM, Andy Buckle <[hidden email]> wrote:
>> >
>> >
>> > On 9 August 2012 13:38, Jordi Gutiérrez Hermoso <[hidden email]>
>> > wrote:
>> >>
>> >> On 9 August 2012 08:26, Nicholas Jankowski <[hidden email]> wrote:
>> >> > On Thu, Aug 9, 2012 at 3:22 AM,  <[hidden email]> wrote:
>> >> >> Please assist me to do this:
>> >> >> Evaluate the series
>> >> >>
>> >> >> 1-1/3+1/5-1/7+1/9-...+1/1001
>> >> >>
>> >> >
>> >> > While there may be more elegant ways to do this, you're basically
>> >> > trying to repeat an addition operation of items with changing value.
>> >>
>> >> e.g.
>> >>
>> >>     sum(1./(1:4:1001) - 1./(3:4:1001))
>> >>
>> >> ?
>> >>
>> >> - Jordi G. H.
>> >> _______________________________________________
>> >> Help-octave mailing list
>> >> [hidden email]
>> >> https://mailman.cae.wisc.edu/listinfo/help-octave
>> >
>> >
>> >>sum(1./(1:4:1001) - 1./(3:4:1001))
>> > error: operator -: nonconformant arguments (op1 is 1x251, op2 is 1x250)
>> > error: evaluating argument list element number 1
>> >>sum(1./(1:4:1001)) - sum(1./(3:4:1001))
>> > ans =  0.78590
>> >
>> > --
>> > /* andy buckle */
>>
>> 50 points from Gryffindor: for doing the kid's homework for him
>> instead of leading him to actually learning to use the program.
>
>
> I would not have posted the answer had Jordi not almost posted the answer...
>
> --
> /* andy buckle */

nah... it's like every other academic paper. you deliberately leave
one or two typo's in the equations to force the grad students
'reimplementing' your work to re-derive the equations themselves :)
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Jordi Gutiérrez Hermoso-2
In reply to this post by nrjank
On 9 August 2012 09:01, Nicholas Jankowski <[hidden email]> wrote:
> 50 points from Gryffindor: for doing the kid's homework for him
> instead of leading him to actually learning to use the program.

I am not going to investigate if this alleged "kid" really is asking
the internet to do this his(?) homework or not. And if he is, great,
because I also ask the internet to do mine all the time ("patches
welcome", etc).

Anyways, you suggested using for loops, which are manifestly
unnecessary for this problem.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

andy buckle


On 9 August 2012 14:41, Jordi Gutiérrez Hermoso <[hidden email]> wrote:
On 9 August 2012 09:01, Nicholas Jankowski <[hidden email]> wrote:
> 50 points from Gryffindor: for doing the kid's homework for him
> instead of leading him to actually learning to use the program.

I am not going to investigate if this alleged "kid" really is asking
the internet to do this his(?) homework or not. And if he is, great,
because I also ask the internet to do mine all the time ("patches
welcome", etc).

Anyways, you suggested using for loops, which are manifestly
unnecessary for this problem.

- Jordi G. H.

I was thinking it was the suggestion of loops that made you snap. I was wavering myself.

--
/* andy buckle */

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

nrjank
On Thu, Aug 9, 2012 at 9:51 AM, Andy Buckle <[hidden email]> wrote:

>
>
> On 9 August 2012 14:41, Jordi Gutiérrez Hermoso <[hidden email]> wrote:
>>
>> On 9 August 2012 09:01, Nicholas Jankowski <[hidden email]> wrote:
>> > 50 points from Gryffindor: for doing the kid's homework for him
>> > instead of leading him to actually learning to use the program.
>>
>> I am not going to investigate if this alleged "kid" really is asking
>> the internet to do this his(?) homework or not. And if he is, great,
>> because I also ask the internet to do mine all the time ("patches
>> welcome", etc).
>>
>> Anyways, you suggested using for loops, which are manifestly
>> unnecessary for this problem.
>>
>> - Jordi G. H.
>
>
> I was thinking it was the suggestion of loops that made you snap. I was
> wavering myself.
>
> --
> /* andy buckle */

hah... yeah, thought that would get someone up in arms. it would be
bigger, uglier, slower, BUT more intelligible and educational to a
programming newbie. Was basing my assumption off the level of
capability hinted at by the form of the initial question. :)
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Francesco Potortì
In reply to this post by K D Kavu
>Please assist me to do this:
>Evaluate the series
>
>1-1/3+1/5-1/7+1/9-...+1/1001

Is this a home assignment?

--
Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058 (op.2111)
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Jordi Gutiérrez Hermoso-2
In reply to this post by nrjank
On 9 August 2012 10:45, Nicholas Jankowski <[hidden email]> wrote:
> hah... yeah, thought that would get someone up in arms. it would be
> bigger, uglier, slower, BUT more intelligible and educational to a
> programming newbie.

For this particular problem, the loopless code is more succinct and
easier to understand.

And nothing wrong to teach people from the beginning to avoid loopless
code. That's how it was taught to me, and I think I came out alright.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Sergei Steshenko




----- Original Message -----

> From: Jordi Gutiérrez Hermoso <[hidden email]>
> To: Nicholas Jankowski <[hidden email]>
> Cc: Octave users list <[hidden email]>
> Sent: Thursday, August 9, 2012 6:27 PM
> Subject: Re: Evaluating a series
>
> On 9 August 2012 10:45, Nicholas Jankowski <[hidden email]> wrote:
>>  hah... yeah, thought that would get someone up in arms. it would be
>>  bigger, uglier, slower, BUT more intelligible and educational to a
>>  programming newbie.
>
> For this particular problem, the loopless code is more succinct and
> easier to understand.
>
> And nothing wrong to teach people from the beginning to avoid loopless
> code. That's how it was taught to me, and I think I came out alright.
>
> - Jordi G. H.


Actually, it _is_ wrong to teach people from the beginning loopless code - because it's much easier for the people being taught to miss the fact that

for n = 1:N
  for m = 1:M
    ...
  endfor
endfor

nested loops have O(N * M) computational complexity.

Regards,
  Sergei.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating a series

Przemek Klosowski-7
In reply to this post by K D Kavu
On 08/09/2012 03:22 AM, [hidden email] wrote:
> Please assist me to do this: Evaluate the series
>
> 1-1/3+1/5-1/7+1/9-...+1/1001

This series looks like sum_{k=0}^{500} (-1)^k/(2k+1) . When you write it
out in Octave:

        N=0:500; sum(<appropriate expression on the vector N>)

you should get something close to 0.786. Pay attention to the operators
acting on matrices---you can't just write (-1)^N/(2*N+1) because of the
way the exponentiation operator is defined for matrices in Octave, so
you have to use the element-by-element versions of the operators.

Actually, this problem of evaluating functions on vector arguments pops
in Octave so often that there are facilities to do it automatically:

* vectorize() makes sure a simple expression such as a^b works for
vector arguments.

* inline() makes it easy to construct a function from the expression
created by vectorize().

Therefore, the octave expression

inline(vectorize("(-1)^k/(2*k+1)"))(N)

calculates the elements of the series you want to sum.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave