ATAR Notes: Forum

VCE Stuff => VCE Mathematics => VCE Mathematics/Science/Technology => VCE Subjects + Help => VCE Specialist Mathematics => Topic started by: cara.mel on March 06, 2008, 05:07:56 pm

Title: help
Post by: cara.mel on March 06, 2008, 05:07:56 pm
def  myfactorial(n):
   if (n == 0): return 1
   return n * myfactorial(n - 1)

def mysin(x, n):
   if (n == 0): return x
   return ((-1)**n)/myfactorial(2*n + 1) * x**(2*n + 1) + mysin(x, n - 1)


Me trying to translate
def  myfactorial(n):

   if n = 0
then maybe display 1 in a box? idunno

Is this from the else?   return n * myfactorial(n - 1)
then end if here?

def mysin(x, n):
   If n = 0
then again, I dont get what x is doing
   return ((-1)**n)/myfactorial(2*n + 1) * x**(2*n + 1) + mysin(x, n - 1)
^ else? -1^n is just -1 divided by something 2n+1 * x^(2n+1) + something. =/

Its not obvious to follow, I am stupid :(
Title: Re: help
Post by: cara.mel on March 06, 2008, 06:18:12 pm
please... :(

i have been told i am supposed to know this in year 11 but i didnt read the first chapter of spec 3/4 because i was told i didnt need to know it.
i will also edit above post to show what I understand
Title: Re: help
Post by: Ahmad on March 06, 2008, 06:27:29 pm
Well, one way to understand the factorial function defined here is to test it out. Try calculating myfactorial(5).

You'll see that you end up with something like this:
myfactorial(5) = 5*myfactorial(4) = 5*4*myfactorial(3) = 5*4*3*myfactorial(2) = 5*4*3*2*myfactorial(1) = 5*4*3*2*1*myfactorial(0) = 5*4*3*2*1*1 = 5!
Title: Re: help
Post by: cara.mel on March 06, 2008, 06:29:12 pm
I dont understand that, wouldnt n stay at 5 so youd do 5*5*5*5*5*5*5*5 etc
Title: Re: help
Post by: Neobeo on March 06, 2008, 06:30:27 pm
Basically you just have to note that

And n would not stay at 5 indefinitely since when it recursively calls its own function again, this time it calls with a SMALLER value (in this case 4, then 3, and so on).
Title: Re: help
Post by: cara.mel on March 06, 2008, 06:31:28 pm
write it out in normal code with proper things like ^ and end if in it.

I dont see how n gets smaller, it doesnt say n=n-1 in it

and that doesnt explain the 2nd monster


Edit: I understand what ahmad said but as far as I'm concerned he might have pulled that out of thin air
Title: Re: help
Post by: Ahmad on March 06, 2008, 06:48:44 pm
def  myfactorial(n):
   if (n == 0): return 1
   return n * myfactorial(n - 1)

Note that the n here is local to the function. So when you call myfactorial(5), n=5, but then you call myfactorial(5-1), which is myfactorial(4), and this creates another framework, where the new n is 4, it can't see the n=5 which is in a 'layer' above it, just as the n inside the myfactorial(5) can't see the n in the myfactorial(4).
Title: Re: help
Post by: cara.mel on March 06, 2008, 06:51:50 pm
Can you explain what return means
Title: Re: help
Post by: Neobeo on March 06, 2008, 06:53:41 pm
Here goes nothing...

First monster:
def  myfactorial(n):
   if (n == 0):
   {
      return 1
   }
   else:
   {
      return n * myfactorial(n - 1)
   }

Second monster:
def mysin(x, n):
   if (n == 0):
   {
      return x
   }
   else:
   {
      return + mysin(x, n - 1)
   }

Other stuff:
"return" basically terminates the function and goes back to where the function was called, except now the function will have this particular value which was specified by the return
Title: Re: help
Post by: Ahmad on March 06, 2008, 06:57:38 pm
Well when programming you usually have two things (to simplify things), functions and routines. Routines are little bits of code which are executed whenever you call the routine.

For example,

routine doPrintStuff()
  print "stuff!"

Now everytime you call doPrintStuff() it will print "stuff". You don't expect doPrintStuff() to "return" a value back.

However, if you had a function such as:

function computeSum(integer a, integer b)
  integer total = a + b
  return total

You expect the function to give back a value. So you can do this, print ( computeSum(5,4) ), and you expect 9 to be printed. You wouldn't, on the other hand, try print ( doPrintStuff() ), because doPrintStuff doesn't "return" a value. I hope you get what return does from the context and this discussion.
Title: Re: help
Post by: Ahmad on March 06, 2008, 06:59:41 pm
And yeah, a little caveat is that once you return a value, execution of the function ceases and the flow of the program immediately returns to the location where the function was called.
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:01:39 pm
So if it was 5:

5*factorial 4

factorial 4 = 4* factorial 3

factorial 3 = 3* factorial 2

factorial 2 = 2* factorial 1

factorial 1 = 1* factorial 0

factorial 0 = 1

So then factorial 1 = 1*1 = 1
Factorial 2 = 2*1 = 2
Factorial 3 = 3*2 = 6
Factorial 4 = 4*6=24
Facorial 5 = 5*24 = 120

That works

How does it work with 2 numbers in there, x and n. what is x
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:02:28 pm
Well when programming you usually have two things (to simplify things), functions and routines. Routines are little bits of code which are executed whenever you call the routine.

For example,

routine doPrintStuff()
  print "stuff!"

Now everytime you call doPrintStuff() it will print "stuff". You don't expect doPrintStuff() to "return" a value back.

However, if you had a function such as:

function computeSum(integer a, integer b)
  integer total = a + b
  return total

You expect the function to give back a value. So you can do this, print ( computeSum(5,4) ), and you expect 9 to be printed. You wouldn't, on the other hand, try print ( doPrintStuff() ), because doPrintStuff doesn't "return" a value. I hope you get what return does from the context and this discussion.

where's it return it to
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:11:58 pm
Returns it by replacing the function call by the value.

So with print ( computeSum(5,4) ) you'd end up with print ( 9 ). The 9 was returned by the function, so it replaces the function call as a value.
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:13:24 pm
How does it work with 2 numbers in there, x and n. what is x

The x is the argument of the sine function. So you'd be computing sin(x), same x.
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:17:02 pm
Second monster:
def mysin(x, n):
   if (n == 0):
   {
      return x
   }
   else:
   {
      return + mysin(x, n - 1)
   }

Thank you for translating it neobeo. although I don't understand what it does still. Does x get a number as well as n? Why are there 2 numbers in the ()

edit: and how does it look in normal maths
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:24:16 pm
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:26:49 pm
x gets a number as well, right. The function accepts two arguments, that's why the there are two labels in the argument paranthesis. That happens when you want to pass over more than one piece of information to a function.
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:29:50 pm
1. why/how does that work
2. is it in radian or degree
3. does it keep going forever or does it stop at 90 or so

I will try and work through it, but first:


So if I had:
dim x, y as integer
x=5
y=1
function addnumber(x,y)
x + y
end function

it would return 6?

and if it was
dim x, y as integer
x=5
y=1
function addnumber(x,y)
x^2 + y
end function

it would return 26?
(I dont remember how functions work and where the number goes but anyway.)
Title: Re: help
Post by: AppleXY on March 06, 2008, 07:30:02 pm


HEY, that's the taylor's series.

http://en.wikipedia.org/wiki/Taylor%27s_series

:P
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:35:45 pm
http://en.wikipedia.org/wiki/Taylor%27s_series#List_of_Taylor_series_of_some_common_functions

how did they know to change it for each one. how did they get it from the first one way at the top of the article. it doesnt make sense.

how does a series work. i have been told i should know that from year 11 so im dumb.
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:35:55 pm
dim x, y as integer
x=5
y=1
function addnumber(x,y)
x^2 + y
end function

it would return 26?

Looks good, you might have to write return x^2 + y though, depending on the programming language. (The details are irrelevant really, since it's language specific).

It's in radians and has a radius of convergence of infinity, this means you can plug in any value! However, that would only be true if you used the whole infinite number of terms. If you cut off this series at some point, you'd get more accurate results by plugging in values closer to zero, this is because this taylor series is said to be "centered" at 0. So if you put in x = 10^(10^10), you probably wouldn't get very accurate results with a small finite number of terms.
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:40:02 pm
http://en.wikipedia.org/wiki/Taylor%27s_series#List_of_Taylor_series_of_some_common_functions

how did they know to change it for each one. how did they get it from the first one way at the top of the article. it doesnt make sense.

how does a series work. i have been told i should know that from year 11 so im dumb.

As far as I know Taylor Series do not crop up in high school.

Here's one method of finding the result for .

Suppose that for some numbers :



By plugging in x = 0, you get

Now differentiate both sides of the equation and set x = 0 again, getting:





By repeating this process you can calculate all the coefficients, the pattern will soon emerge!
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:43:12 pm
but aidan said i should know of a series in general and i dont and he wouldnt explain it to me =(

not this type, this is in like week 10 or 11 or so of mth1030.

why are there subscripts, in like a1, I dont follow that.
Title: Re: help
Post by: Neobeo on March 06, 2008, 07:44:00 pm
In VB(script) it would look like

dim x, y as integer
x=5
y=1
function addnumber(x,y)
addnumber = x^2 + y
end function

Here, the "addnumber = x^2 + y" is really assigning the value "x^2 + y" to the function. The difference between this and "return" is that "return" immediately terminates the function. In VB(script) the only way to terminate a function or routine is by "end function" or "exit function".
Title: Re: help
Post by: Ahmad on March 06, 2008, 07:49:00 pm
Subscripts are used to name different constants that we want to calculate. I could've used a, b, c, d etc, but it is much more convenient and systematic to use subscripts to distinguish between them.

Aidan was probably referring to sequences and series which are taught at a rudimentary level in further maths. This would be sequences of numbers, which is basically a function that has the domain of the natural numbers. For example consider f(n) = 3n + 2, with domain the natural numbers. Then you could write the sequences of numbers defined by f by calculating f(1), f(2), ... (5, 8, 11, ...).

You will probably study sequences and series in a more rigorous manner when you take calculus and learn about convergence, and methods of finding whether a series converges/diverges.
Title: Re: help
Post by: cara.mel on March 06, 2008, 07:52:15 pm
so a0=a, a1=b, a2=c etc?
*me tries to understand now I get that*

Edit: wait, how does it work for x=1 then o_O, because how do I find a b c

f(n) = 3n + 2 - how does that work. why does it keep going. what is the point of it then because it would just be infinity when you add them together. (the ... mean it keeps going right?)
Title: Re: help
Post by: Ahmad on March 06, 2008, 08:02:15 pm
If you add together the terms of a sequence, you get a series. You're right, in this case the series would diverge, but that isn't always the case. This occurs for example with f(n) = (1/2)^n.

And yes, ... means it keeps going.
Title: Re: help
Post by: cara.mel on March 06, 2008, 08:03:57 pm
So in those does n go from 1-> infinity, and you keep subbing in new numbers?

What is a sequence and a series
Title: Re: help
Post by: midas_touch on March 06, 2008, 08:24:44 pm
http://en.wikipedia.org/wiki/Taylor%27s_series#List_of_Taylor_series_of_some_common_functions

how did they know to change it for each one. how did they get it from the first one way at the top of the article. it doesnt make sense.

how does a series work. i have been told i should know that from year 11 so im dumb.

The first time I was taught series was last year at uni, so dont worry. Simply, a series is an infinite sum of terms. In the case of the Taylor series, this gives a polynomial representation of a particular function, since each term in the series is a polynomial.
Title: Re: help
Post by: excal on March 06, 2008, 10:57:46 pm
caramel - the code actually uses an algorithmic concept known as 'recursion'...it probably will look foreign to the untrained eye..

mysin(int x, int y) returning a value that contains itself (so a function calling itself [over and over again]).

Title: Re: help
Post by: cara.mel on March 07, 2008, 08:04:43 am
This morning now I am feeling better Ahmad's posts make sense. THank you =D=D :D
Title: Re: help
Post by: Ahmad on March 07, 2008, 06:46:43 pm
No problem caramel, glad to help you. :)
Title: Re: help
Post by: aidansteele on March 26, 2008, 10:44:07 pm
I read the initial code and wondered why the code looked so familiar..!

I never realised my blog caused so much consternation. :(