Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to define a recursive sequence and then ask Mathematica to print a specific value:

enter image description here

Am I doing something wrong?

share|improve this question

migrated from math.stackexchange.com May 21 '12 at 5:17

This question came from our site for people studying math at any level and professionals in related fields.

1 Answer 1

up vote 9 down vote accepted

First, you need to use "delayed substitution" so that Mathematica knows it needs to wait with evaluating expression until you type G[1]. Secondly, you can't have arithmetic expression on the left-hand side. so the second line should be:

G[y_]:=2G[y-1].
share|improve this answer
    
Thank you very much –  Stefanos May 19 '12 at 12:06
8  
y_ doesn't have anything to do with delayed substitution. It just means any argument (indicated by _), to be named y. If you leave away the underscore the call would work only for the literal symbol y. –  Sjoerd C. de Vries May 21 '12 at 5:24
3  
Also it's not strictly true that you can't have an arithmetic expression on the left hand side; it just gives completely different semantics from the one intended: The original definition would have been used when calling as G[y+1] (assuming y has no value assigned at the point of call). Indeed, when using a pattern, it can even make sense to have arithmetic arguments, e.g.: G[0]:=1;G[i_Integer/;i>0]:=2*G[i-1];G[y_+n_Integer/;n>0]:=2*G[y+n-1] Then you could write G[a+4] and get the result 16*G[a]. –  celtschk May 21 '12 at 8:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.