Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

December 13, 2014

10 трюков, упрощающих математические операции

Александр Мураховский
9 декабря 2014   Александр Мураховский

Не так давно на Лайфхакере вышла рецензия на книгу «Магия чисел», в которой содержится огромное количество математических трюков. Книга не оставила нас равнодушными, и мы выбрали из неё 10 самых интересных советов по упрощению математических операций.

Недавно, прочитав книгу «Магия чисел», я почерпнул огромное количество информации. В книге рассказывается о десятках трюков, которые упрощают привычные математические операции. Оказалось, что умножение и деление в столбик — это прошлый век, и непонятно, почему этому до сих пор учат в школах.

Я выбрал 10 самых интересных и полезных трюков и хочу поделиться ими с вами.

Умножение «3 на 1» в уме
Умножение трёхзначных чисел на однозначные — это очень простая операция. Всё, что нужно сделать, — это разбить большую задачу на несколько маленьких.

Пример: 320 × 7

Разбиваем число 320 на два более простых числа: 300 и 20.
Умножаем 300 на 7 и 20 на 7 по отдельности (2 100 и 140).
Складываем получившиеся числа (2 240).

Возведение в квадрат двузначных чисел
Возводить в квадрат двузначные числа не намного сложнее. Нужно разбить число на два и получить приближенный ответ.

Пример: 41^2

Вычтем 1 из 41, чтобы получить 40, и добавим 1 к 41, чтобы получить 42.
Умножаем два получившихся числа, воспользовавшись предыдущим советом (40 × 42 = 1 680).
Прибавляем квадрат числа, на величину которого мы уменьшали и увеличивали 41 (1 680 + 1^2 = 1 681).
Ключевое правило здесь — превратить искомое число в пару других чисел, которые перемножить гораздо проще. К примеру, для числа 41 это числа 42 и 40, для числа 77 — 84 и 70. То есть мы вычитаем и прибавляем одно и то же число.

Мгновенное возведение в квадрат числа, оканчивающегося на 5
С квадратами чисел, оканчивающихся на 5, вообще не нужно напрягаться. Всё, что нужно сделать, — это умножить первую цифру на число, которое на единицу больше, и добавить в конец числа 25.

Пример: 75^2

Умножаем 7 на 8 и получаем 56.
Добавляем к числу 25 и получаем 5 625.

Деление на однозначное число
Деление в уме — это достаточно полезный навык. Задумайтесь о том, как часто мы делим числа каждый день. К примеру, счёт в ресторане.

Пример: 675 : 8

Найдём приближенные ответы, умножив 8 на удобные числа, которые дают крайние результаты (8 × 80 = 640, 8 × 90 = 720). Наш ответ — 80 с хвостиком.
Вычтем 640 из 675. Получив число 35, нужно разделить его на 8 и получить 4 с остатком 3.
Наш финальный ответ — 84,3.
Мы получаем не максимально точный ответ (правильный ответ — 84,375), но согласитесь, что даже такого ответа будет более чем достаточно.

Простое получение 15%
Чтобы быстро узнать 15% от любого числа, нужно сначала посчитать 10% от него (перенеся запятую на один знак влево), затем поделить получившееся число на 2 и прибавить его к 10%.

Пример: 15% от 650

Находим 10% — 65.
Находим половину от 65 — это 32,5.
Прибавляем 32,5 к 65 и получаем 97,5.

Банальный трюк
Пожалуй, все мы натыкались на такой трюк:

Задумайте любое число. Умножьте его на 2. Прибавьте 12. Разделите сумму на 2. Вычтите из неё исходное число.
Вы получили 6, верно? Что бы вы ни загадали, вы всё равно получите 6. И вот почему:

2x (удвоить число).
2x + 12 (прибавить 12).
(2x + 12) : 2 = x + 6 (разделить на 2).
x + 6 − x (вычесть исходное число).
Этот трюк построен на элементарных правилах алгебры. Поэтому, если вы когда-нибудь услышите, что кто-то его загадывает, натяните свою самую надменную усмешку, сделайте презрительный взгляд и расскажите всем разгадку. :)

Магия числа 1 089
Этот трюк существует не одно столетие.

Запишите любое трёхзначное число, цифры которого идут в порядке уменьшения (к примеру, 765 или 974). Теперь запишите его в обратном порядке и вычтите его из исходного числа. К полученному ответу добавьте его же, только в обратном порядке.
Какое бы число вы ни выбрали, в результате получите 1 089.

Быстрые кубические корни
Для того чтобы быстро считать кубический корень из любого числа, понадобится запомнить кубы чисел от 1 до 10:

12345678910
1827641252163435127291 000
Как только вы запомните эти значения, находить кубический корень из любого числа будет элементарно просто.

Пример: кубический корень из 19 683

Берём величину тысяч (19) и смотрим, между какими числами она находится (8 и 27). Соответственно, первой цифрой в ответе будет 2, а ответ лежит в диапазоне 20+.
Каждая цифра от 0 до 9 появляется в таблице по одному разу в виде последней цифры куба.
Так как последняя цифра в задаче — 3 (19 683), это соответствует 343 = 7^3. Следовательно, последняя цифра ответа — 7.
Ответ — 27.
Примечание: трюк работает только тогда, когда исходное число является кубом целого числа.

Правило 70
Чтобы найти число лет, необходимых для удвоения ваших денег, нужно разделить число 70 на годовую процентную ставку.

Пример: число лет, необходимое для удвоения денег с годовой процентной ставкой 20%.

70 : 20 = 3,5 года

Правило 110
Чтобы найти число лет, необходимых для утроения денег, нужно разделить число 110 на годовую процентную ставку.

Пример: число лет, необходимое для утроения денег с годовой процентной ставкой 12%.

110 : 12  = 9 лет

Математика — волшебная наука. Я даже немного смущён тем, что такие простые трюки смогли меня удивить, и даже не представляю, сколько ещё математических фокусов можно узнать.

По материалам книги «Магия чисел»

ЭЛЕКТРОННАЯ КНИГА
ЭЛЕКТРОННАЯ КНИГА НА АНГЛИЙСКОМ ЯЗЫКЕ
©

November 4, 2014

Different Coordinate Systems on the same Plane

The Origin and the Coordinate Lines define the nature of the coordinate system formed by the coordinate plane. These two decide the definition for the location of other points on the line. These decide the physical length between the origin and a point in the plane.

The distance between any two points in the plane would not be affected by these changes (until the unit length relating to the coordinate lines is changed).

Different coordinate systems can be defined on the same plane by varying the position of the "Origin" (O) and the Coordinate Lines x’x and y’y’.

Varying the Coordinate System

This can be done by

  • Translation of Axis
    Changing the position of the "Origin" only without changing the "Unit Length" and without tilting the coordinate lines.

    This is done by ensuring that the coordinate lines stay parallel to their original locations while the origin is shifted to the new location.

    The darker lines indicate the new position. To indicate the change the coordinate lines are represented using capital letters.

    Bringing about a change in the coordinate system in this manner is called "Translation of Axis".

    Notice that the coordinates of the points "P", "A", "C" and "Q" have changed with the change in the location of the origin.

    Where "(h,k)" represents the coordinates of the new "Origin" in the old coordinate system.

    Coordinates of a point in the new coordinate system
    = (Coordinates of the point in the old system)
    − (Respective Coordinates of the new origin)

    ⇒ X = x − (h) and Y = y − (k)
    ⇒ (X,Y) = [{x − (h)}, {y − (k)}]
    [Where "(x,y)" and "(X,Y)" represent the coordinates of the same point in the old and the new coordinate systems respectively.]

    The origin has been shifted to (2, 1) ⇒ h = 2 and k = 1

    ⇒ Coordinates (in the new system) of

    Point "A" ⇒ (XA, YA)=[{(xA) − h}, {(yA) − k}]
    =[{(2) − (2)}, {(− 2) − (1)}]
    =[{2 − 2}, {− 2 − 1}]
    =(0, − 3)
    Point "C" ⇒ (XC, YC)=[{(xC) − h}, {(yC) − k}]
    =[{(− 4) − (2)}, {(3) − (1)}]
    =[{− 4 − 2}, {3 − 1}]
    =(− 6, 2)
  • Rotation of Axis
    Changing the position of the "axes" only without changing the "Unit Length" and without changing the location of the origin.

    This is done by rotating the axis only about the origin.

    The darker lines indicate the new position. To indicate the change the coordinate lines are represented using capital letters.

    Bringing about a change in the coordinate system in this manner is called "Rotation of Axis".

    Notice that the coordinates of the points "P", "A", "C" and "Q" have changed with the change in the location of the origin.

    Where "θ" represents the angle by which the axes are rotated in an anti clockwise direction.

    The Coordinates of a point in the new coordinate system is given by the following relations:

    • X-Coordinate
      X = x cos θ + y sin θ
    • Y-Coordinate
      Y = − x sinθ + y cosθ

    ⇒ (X,Y) = [{x cos θ + y sin θ}, {x sin θ + y cos θ}]
    [Where "(x,y)" and "(X,Y)" represent the coordinates of the same point in the old and the new coordinate systems respectively.]

    The axes have been rotated by 45o ⇒ θ = 45o

    ⇒ Coordinates (in the new system) of

    Point "A" ⇒ (Xa, Ya)=[{(xa cos θ) + (ya sin θ)}, {(− xa sin θ) + (ya cos θ)}]
    =[{(2 cos 45o) + (− 2 sin 45o)},
    {(− 2 sin 45o) + ( (− 2) sin 45o)}]
    =
    [{(2 ×
    1
    √2
    ) + (− 2 ×
    1
    √2
    )}, {(− 2 ×
    1
    √2
    ) + (− 2 ×
    1
    √2
    )}]
    =
    [{
    2
    √2
    2
    √2
    }, {−
    2
    √2
    2
    √2
    }]
    =
    (0, −
    4
    √2
    )
    =(0, − 2√2)
    Point "C" ⇒ (Xc, Yc)=[{(xc cos θ) + (yc sin θ)}, {(− xc sin θ) + (yc cos θ)}]
    =[{((− 4) cos 45o) + (3 sin 45o)},
    {(− ((−4) sin 45o) + ( (3 sin 45o)}]
    =
    [{(− 4 ×
    1
    √2
    )+(3 ×
    1
    √2
    )}, {(− (− 4) ×
    1
    √2
    )+(3 ×
    1
    √2
    )}]
    =
    [{−
    4
    √2
    +
    3
    √2
    }, {−
    4
    √2
    +
    3
    √2
    }]
    =
    [{− 2√2 +
    3
    √2
    }, {− 2√2 +
    3
    √2
    }]
  • Translation and Rotation of Axis
    Changing the position of both the "axes" and the origin without changing the "Unit Length".

    This is done by rotating the axis and shifting the origin.

    Translation FirstRotation FirstFinal Resultant

    This can be done by rotating first and then translating (Or) by translating first and then rotating. In both the cases the result would be the same.

    The darker lines indicate the new position. To indicate the change the coordinate lines are represented using capital letters.

    Bringing about a change in the coordinate system in this manner is called "Translation and Rotation of Axis".

    The coordinates of the points "P", "A", "C" and "Q" change with the change in the coordinate system.

    Where "θ" represents the angle by which the axes are rotated in an anti clockwise direction and (h,k) the coordinates of the New Origin with respect to the old coordinate system.

    The Coordinates of a point in the new coordinate system is given by the following relations:

    • X-Coordinate
      X = (x − h) cos θ + (y − k) sin θ
    • Y-Coordinate
      Y = − (x − h) sin θ + (y − k) cos θ

    ⇒ (X,Y) = [{(x − h) cos θ + (y − k) sin θ}, {− (x − h) sin θ + (y − k) cos θ}]
    [Where "(x,y)" and "(X,Y)" represent the coordinates of the same point in the old and the new coordinate systems respectively.]

    The axes have been rotated by 45o ⇒ θ = 45o

    The origin has been shifted to (− 2, 2) ⇒ h = − 2 and k = 2

    ⇒ Coordinates (in the new system) of

    Point "A" ⇒ (Xa, Ya)=[{((xa − h) cos θ) + ((ya − k) sin θ)},
    {((− (xa − h) sin θ) + ((ya − K) cos θ)}]
    ={ [ (2√2) + (− 2√2)], [ {− (2√2)} + {(− 2√2)} ]}
    =[2√2 − 2√2], [ − 2√2 − 2√2]
    =(0, − 4√2)

    A (2, − 2) ⇒ xa = 2 and ya = − 2.

    The origin has been shifted to (− 2, 2) ⇒ h = − 2 and k = 2

    ((xa − h) cos θ)=
    [(2) − (− 2) ×
    1
    √2
    )]
    =
    [(2 + 2) ×
    1
    √2
    ]
    =
    [
    4
    √2
    ]
    =2 √2
    ((ya − k) sin θ)=
    [(− 2) − (2) ×
    1
    √2
    )]
    =
    [(− 2 − 2) ×
    1
    √2
    ]
    =
    [−
    4
    √2
    ]
    =− 2 √2

    ⇒ Coordinates (in the new system) of

    Point "C" ⇒ (Xc, Yc)=[{((xc − h) cos θ) + ((yc − k) sin θ)},
    {((− (xc − h) sin θ) + ((yc − K) cos θ)}]
    =
    [ (− √2) + (
    1
    √2
    ) ], [ − (− √2) + (
    1
    √2
    )]
    =
    (
    −2 + 1
    √2
    ,
    2 + 1
    √2
    )
    =
    (−
    1
    √2
    ,
    3
    √2
    )

    C (− 4, 3) ⇒ xc = − 4 and yc = − 3.

    The origin has been shifted to (− 2, 2) ⇒ h = − 2 and k = 2

    ((xc − h) cos θ)=
    [{ (− 4) − (− 2) } ×
    1
    √2
    )]
    =
    [(− 4 + 2) ×
    1
    √2
    ]
    =
    [−
    2
    √2
    ]
    =− √2
    ((yc − k) sin θ)=
    [{(3) − (2)} ×
    1
    √2
    )]
    =
    [(1) ×
    1
    √2
    ]
    =
    [
    1
    √2
    ]

©

September 29, 2014

Drawing a 95% confidence interval in R

Posted on August 5, 2013 by Nathan Lemoine

I’m writing a post on how to draw a in 95% confidence interval in R by hand. I spent an hour or so trying to figure this out, and most message threads point someone to the ellipse() function. However, I wanted to know how it works.

The basic problem was this. Imagine two random variables with a bivariate normal distribution, called y, which is an x 2 matrix with n rows and 2 columns. The random variables are described by a mean vector mu and covariance matrix S. The equation for an ellipse is:

(y – mu) S^1 (y – mu)’ = c^2

The number c^2 controls the radius of the ellipse, which we want to extend to the 95% confidence interval, which is given by a chi-square distribution with 2 degrees of freedom. The ellipse has two axes, one for each variable. The axes have half lengths equal to the square-root of the eigenvalues, with the largest eigenvalue denoting the largest axis. A further description of this can be found in any multivariate statistics book (or online).

To calculate the ellipse, we need to do a few things: 1) convert the variables to polar coordinates, 2) extend the new polar variables by the appropriate half lengths (using eigenvalues), 3) rotate the coordinates based on the variances and covariances, and 4) move the location of the new coordinates back to the original means. This will make more sense when we do it by hand.

First, generate some data, plot it, and use the ellipse() function to make the 95% confidence interval. This is the target interval (I use it to check myself. If my calculations match, hooray. If not, I screwed up).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
library(mvtnorm) # References rmvnorm()
library(ellipse) # References ellipse()
set.seed(17)
 
# Set the covariance matrix
sigma2 <- matrix(c(5, 2, 2, 5), ncol=2)
 
# Set the means
mu <- c(5,5)
 
# Get the correlation matrix
P <- cov2cor(sigma2)
 
# Generate the data
p <- rmvnorm(n=50, mean=mu, sigma=sqrt(sigma2))
 
# Plot the data
plot(p)
 
# Plot the ellipse
lines( ellipse( P, centre = c(5,5)) , col='red')

Second, get the eigenvalues and eigenvectors of the correlation matrix.

1
2
evals <- eigen(P)$values
evecs <- eigen(P)$vectors

Third, make a vector of coordinates for a full circle, from 0 to 2*pi and get the critical value (c^2).

1
2
3
4
5
6
# Angles of a circle
a <- seq(0, 2*pi, len=100)
 
# Get critical value
c2 <- qchisq(0.95, 2)
c <- sqrt(c2)

The vector A above are angles that describe a unit circle. The coordinates of a unit circle are found by x = cos(a) and y = sin(a) (use trigonometry of a triangle to get this, where the hypotenuse = 1). We need to extend the unit circle by the appropriate lengths based on the eigenvalues and then even more by the critical value.

1
2
3
4
5
# Get the distances
xT <- c * sqrt(evals[1]) * cos(a)
yT <- c * sqrt(evals[2]) * sin(a)
 
M <- cbind(xT, yT)

If you plot M, you’ll get an ellipse of the appropriate axes lengths, but centered on 0 and unrotated. Rotate the ellipse using the eigenvectors, which describe the relationships between the variables (more appropriately, they give the directions for the vectors of the major axes of variation). Use the equation u*M’ (write this out to see why this works).

1
2
3
# Covert the coordinates
transM <- evecs %*% t(M)
transM <- t(transM)

The final step is to move the rotated ellipse back to the original scale (centered around the original means) and plot the data.

1
lines(transM + mu)

This gives the following plot, with the red line being the output from the ellipse() function.
And that’s that! Hopefully this helps someone like me who spent hours looking but couldn’t find anything.
©

September 23, 2014

Basic Probability Distributions in R

We look at some of the basic operations associated with probability distributions. There are a large number of probability distributions available, but we only look at a few. If you would like to know what distributions are available you can do a search using the command help.search(“distribution”).

Here we give details about the commands associated with the normal distribution and briefly mention the commands for other distributions. The functions for different distributions are very similar where the differences are noted below.

For this chapter it is assumed that you know how to enter data which is covered in the previous chapters.

To get a full list of the distributions available in R you can use the following command:
help(Distributions)

For every distribution there are four commands. The commands for each distribution are prepended with a letter to indicate the functionality:

“d” returns the height of the probability density function
“p” returns the cumulative density function
“q” returns the inverse cumulative density function (quantiles)
“r” returns randomly generated numbers

 

The Normal Distribution

There are four functions that can be used to generate the values associated with the normal distribution. You can get a full list of them and their options using the help command:
> help(Normal)
The first function we look at it is dnorm. Given a set of values it returns the height of the probability distribution at each point. If you only give the points it assumes you want to use a mean of zero and standard deviation of one. There are options to use different values for the mean and standard deviation, though:

> dnorm(0)
[1] 0.3989423
> dnorm(0)*sqrt(2*pi)
[1] 1
> dnorm(0,mean=4)
[1] 0.0001338302
> dnorm(0,mean=4,sd=10)
[1] 0.03682701
>v <- c(0,1,2)
> dnorm(v)
[1] 0.39894228 0.24197072 0.05399097
> x <- seq(-20,20,by=.1)
> y <- dnorm(x)
> plot(x,y)
> y <- dnorm(x,mean=2.5,sd=0.1)
> plot(x,y)
The second function we examine is pnorm. Given a number or a list it computes the probability that a normally distributed random number will be less than that number. This function also goes by the rather ominous title of the “Cumulative Distribution Function.” It accepts the same options as dnorm:
> pnorm(0)
[1] 0.5
> pnorm(1)
[1] 0.8413447
> pnorm(0,mean=2)
[1] 0.02275013
> pnorm(0,mean=2,sd=3)
[1] 0.2524925
> v <- c(0,1,2)
> pnorm(v)
[1] 0.5000000 0.8413447 0.9772499
> x <- seq(-20,20,by=.1)
> y <- pnorm(x)
> plot(x,y)
> y <- pnorm(x,mean=3,sd=4)
> plot(x,y)
If you wish to find the probability that a number is larger than the given number you can use the lower.tail option:
> pnorm(0,lower.tail=FALSE)
[1] 0.5
> pnorm(1,lower.tail=FALSE)
[1] 0.1586553
> pnorm(0,mean=2,lower.tail=FALSE)
[1] 0.9772499
The next function we look at is qnorm which is the inverse of pnorm. The idea behind qnorm is that you give it a probability, and it returns the number whose cumulative distribution matches the probability. For example, if you have a normally distributed random variable with mean zero and standard deviation one, then if you give the function a probability it returns the associated Z-score:
> qnorm(0.5)
[1] 0
> qnorm(0.5,mean=1)
[1] 1
> qnorm(0.5,mean=1,sd=2)
[1] 1
> qnorm(0.5,mean=2,sd=2)
[1] 2
> qnorm(0.5,mean=2,sd=4)
[1] 2
> qnorm(0.25,mean=2,sd=2)
[1] 0.6510205
> qnorm(0.333)
[1] -0.4316442
> qnorm(0.333,sd=3)
[1] -1.294933
> qnorm(0.75,mean=5,sd=2)
[1] 6.34898
> v = c(0.1,0.3,0.75)
> qnorm(v)
[1] -1.2815516 -0.5244005  0.6744898
> x <- seq(0,1,by=.05)
> y <- qnorm(x)
> plot(x,y)
> y <- qnorm(x,mean=3,sd=2)
> plot(x,y)
> y <- qnorm(x,mean=3,sd=0.1)
> plot(x,y)
The last function we examine is the rnorm function which can generate random numbers whose distribution is normal. The argument that you give it is the number of random numbers that you want, and it has optional arguments to specify the mean and standard deviation:

> rnorm(4)
[1]  1.2387271 -0.2323259 -1.2003081 -1.6718483
> rnorm(4,mean=3)
[1] 2.633080 3.617486 2.038861 2.601933
> rnorm(4,mean=3,sd=3)
[1] 4.580556 2.974903 4.756097 6.395894
> rnorm(4,mean=3,sd=3)
[1]  3.000852  3.714180 10.032021  3.295667
> y <- rnorm(200)
> hist(y)
> y <- rnorm(200,mean=-2)
> hist(y)
> y <- rnorm(200,mean=-2,sd=4)
> hist(y)
> qqnorm(y)
> qqline(y)

 

The Chi-Squared Distribution

There are four functions that can be used to generate the values associated with the Chi-Squared distribution. You can get a full list of them and their options using the help command:
> help(Chisquare)
These commands work just like the commands for the normal distribution. The first difference is that it is assumed that you have normalized the value so no mean can be specified. The other difference is that you have to specify the number of degrees of freedom. The commands follow the same kind of naming convention, and the names of the commands are dchisq, pchisq, qchisq, and rchisq.
A few examples are given below to show how to use the different commands. First we have the distribution function, dchisq:
> x <- seq(-20,20,by=.5)
> y <- dchisq(x,df=10)
> plot(x,y)
> y <- dchisq(x,df=12)
> plot(x,y)
Next we have the cumulative probability distribution function:
> pchisq(2,df=10)
[1] 0.003659847
> pchisq(3,df=10)
[1] 0.01857594
> 1-pchisq(3,df=10)
[1] 0.981424
> pchisq(3,df=20)
[1] 4.097501e-06
> x = c(2,4,5,6)
> pchisq(x,df=20)
[1] 1.114255e-07 4.649808e-05 2.773521e-04 1.102488e-03
Next we have the inverse cumulative probability distribution function:
> qchisq(0.05,df=10)
[1] 3.940299
> qchisq(0.95,df=10)
[1] 18.30704
> qchisq(0.05,df=20)
[1] 10.85081
> qchisq(0.95,df=20)
[1] 31.41043
> v <- c(0.005,.025,.05)
> qchisq(v,df=253)
[1] 198.8161 210.8355 217.1713
> qchisq(v,df=25)
[1] 10.51965 13.11972 14.61141
Finally random numbers can be generated according to the Chi-Squared distribution:
> rchisq(3,df=10)
[1] 16.80075 20.28412 12.39099
> rchisq(3,df=20)
[1] 17.838878  8.591936 17.486372
> rchisq(3,df=20)
[1] 11.19279 23.86907 24.81251

©

September 18, 2014

Least squares fitting in Excel

Set up four parallel columns in the spreadsheet:

* X has the x-values.
* Y has the y-values.
* Fit computes the Gaussian values (based on the x-values and three parameters).
* Residual is the difference between the y-values and the fits.

In order to compute the fit, you need to create three cells holding the three gaussian parameters. The formula for the fit must be identical to that used by the other software so you can compare your results with its. The example below names the three parameters kappa0, kappa1, and kappa2--just as in the documentation. The formula in the second row, where the x-value is in cell A2, is

=Kappa0 * EXP(-1*(A2 - Kappa1)^2 / Kappa2)

It is copied down to all the other rows.

This is enough to check the software's results simply by plugging in its reported values of kappa0, kappa1, and kappa2. To see whether they are correct, compute the sum of squared residuals (SSR). A formula for this uses the SUMSQ function ("=SUMSQ(D2:D32)" in the example). If you think a better combination of the parameters will work, plug in that new combination and see whether SSR decreases: if it goes down, the new values are better.

Spreadsheet

You can have this automated for you using Excel's "Solver" tool. Specify that you want to minimize the SSR by varying the three kappas. Start with the solution given by the other software. Solver will try systematically to improve its solution.

Solver dialog

The same method--suitably adapted--works well for least squares, maximum likelihood, and other optimization procedures in statistics, provided the objective function is well-behaved (i.e., differentiable and convex) and you can obtain an excellent starting value. Otherwise, Solver is perfectly capable of reporting inferior solutions or failing altogether: it is wise not to use it as the sole method to solve a problem.
share|improve this answer

answered Jun 4 '11 at 14:50
whuber♦
©

September 9, 2014

March 22, 2010

Discrete Fourier transform - MATLAB

Discrete Fourier transform - MATLAB

fft - Discrete Fourier transform
Syntax

Y = fft(X)
Y = fft(X,n)
Y = fft(X,[],dim)
Y = fft(X,n,dim)
Definition

The functions Y=fft(x) and y=ifft(X) implement the transform and inverse transform pair given for vectors of length by:

where

is an th root of unity.
Description

Y = fft(X) returns the discrete Fourier transform (DFT) of vector X, computed with a fast Fourier transform (FFT) algorithm.

If X is a matrix, fft returns the Fourier transform of each column of the matrix.

If X is a multidimensional array, fft operates on the first nonsingleton dimension.

Y = fft(X,n) returns the n-point DFT. If the length of X is less than n, X is padded with trailing zeros to length n. If the length of X is greater than n, the sequence X is truncated. When X is a matrix, the length of the columns are adjusted in the same manner.

Y = fft(X,[],dim) and Y = fft(X,n,dim) applies the FFT operation across the dimension dim.
Examples

A common use of Fourier transforms is to find the frequency components of a signal buried in a noisy time domain signal. Consider data sampled at 1000 Hz. Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and 120 Hz sinusoid of amplitude 1 and corrupt it with some zero-mean random noise:

Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')

It is difficult to identify the frequency components by looking at the original signal. Converting to the frequency domain, the discrete Fourier transform of the noisy signal y is found by taking the fast Fourier transform (FFT):

NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);

% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

The main reason the amplitudes are not exactly at 0.7 and 1 is because of the noise. Several executions of this code (including recomputation of y) will produce different approximations to 0.7 and 1. The other reason is that you have a finite length signal. Increasing L from 1000 to 10000 in the example above will produce much better approximations on average.
Algorithm

The FFT functions (fft, fft2, fftn, ifft, ifft2, ifftn) are based on a library called FFTW [3],[4]. To compute an -point DFT when is composite (that is, when ), the FFTW library decomposes the problem using the Cooley-Tukey algorithm [1], which first computes transforms of size , and then computes transforms of size . The decomposition is applied recursively to both the - and -point DFTs until the problem can be solved using one of several machine-generated fixed-size "codelets." The codelets in turn use several algorithms in combination, including a variation of Cooley-Tukey [5], a prime factor algorithm [6], and a split-radix algorithm [2]. The particular factorization of is chosen heuristically.

When is a prime number, the FFTW library first decomposes an -point problem into three ( )-point problems using Rader's algorithm [7]. It then uses the Cooley-Tukey decomposition described above to compute the ( )-point DFTs.

For most , real-input DFTs require roughly half the computation time of complex-input DFTs. However, when has large prime factors, there is little or no speed difference.

The execution time for fft depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors.

Note You might be able to increase the speed of fft using the utility function fftw, which controls the optimization of the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support

fft supports inputs of data types double and single. If you call fft with the syntax y = fft(X, ...), the output y has the same data type as the input X.
See Also

fft2, fftn, fftw, fftshift, ifft

dftmtx, filter, and freqz in the Signal Processing Toolbox
References

[1] Cooley, J. W. and J. W. Tukey, "An Algorithm for the Machine Computation of the Complex Fourier Series,"Mathematics of Computation, Vol. 19, April 1965, pp. 297-301.

[2] Duhamel, P. and M. Vetterli, "Fast Fourier Transforms: A Tutorial Review and a State of the Art," Signal Processing, Vol. 19, April 1990, pp. 259-299.

[3] FFTW (http://www.fftw.org)

[4] Frigo, M. and S. G. Johnson, "FFTW: An Adaptive Software Architecture for the FFT,"Proceedings of the International Conference on Acoustics, Speech, and Signal Processing, Vol. 3, 1998, pp. 1381-1384.

[5] Oppenheim, A. V. and R. W. Schafer, Discrete-Time Signal Processing, Prentice-Hall, 1989, p. 611.

[6] Oppenheim, A. V. and R. W. Schafer, Discrete-Time Signal Processing, Prentice-Hall, 1989, p. 619.

[7] Rader, C. M., "Discrete Fourier Transforms when the Number of Data Samples Is Prime," Proceedings of the IEEE, Vol. 56, June 1968, pp. 1107-1108.
©