How to Write a Cosine Function: Step-by-Step

Writing a cosine function means building the equation y = A cos(Bx – C) + D, where each letter controls a specific transformation: stretch, compression, shift, or reflection. Once you know what each piece does, you can write a cosine function from a graph, a data set, or a word problem in four straightforward steps.

The General Form and What Each Part Controls

Every cosine function you’ll write follows this template:

y = A cos(Bx – C) + D

  • A is the amplitude, the height from the centerline to the peak.
  • B is the frequency coefficient, which controls how wide or narrow each cycle is.
  • C is used to calculate the phase shift (horizontal slide left or right).
  • D is the vertical shift, which moves the whole wave up or down.

Your job when “writing” a cosine function is to figure out each of these four values, then plug them into the template. The sections below walk through each one.

Step 1: Find the Amplitude (A)

The amplitude tells you how tall the wave is, measured from the middle of the wave to its peak. If you know the highest and lowest values the function reaches, use this formula:

A = (highest value – lowest value) / 2

For example, if a wave peaks at 3 and bottoms out at -1, the amplitude is (3 – (-1)) / 2 = 2. The wave rises 2 units above its centerline and drops 2 units below it. If the function is flipped upside down (starting at a trough instead of a peak), you make A negative.

Step 2: Find the Vertical Shift (D)

The vertical shift, D, is the centerline of the wave. It’s the horizontal line sitting exactly halfway between the peak and the trough. The formula mirrors the amplitude calculation but uses addition:

D = (highest value + lowest value) / 2

Using the same example where the peak is 3 and the trough is -1: D = (3 + (-1)) / 2 = 1. The entire wave has been shifted up by 1 unit from where a standard cosine wave would sit. When D is 0, the wave is centered on the x-axis, which is the default cosine position.

Step 3: Find B and the Period

The period is the horizontal length of one complete cycle, peak to peak or trough to trough. The coefficient B and the period are linked by this relationship:

Period = 2π / |B|

A standard cosine function has B = 1 and a period of 2π (about 6.28). If you already know the period from a graph or a problem, solve for B by rearranging: B = 2π / period. A wave that completes one full cycle every 4π units, for instance, has B = 2π / 4π = 0.5. A larger B compresses the wave horizontally (more cycles in the same space), while a smaller B stretches it out.

Step 4: Find the Phase Shift (C)

The phase shift slides the wave left or right. In the equation y = A cos(Bx – C) + D, the actual horizontal shift is C/B, not just C alone. A positive result shifts the graph to the right. A negative result shifts it to the left.

Pay close attention to the subtraction sign in front of C. If your equation looks like y = cos(x + π/6), rewrite it as y = cos(x – (-π/6)). Here C = -π/6, so the phase shift is -π/6 divided by 1, meaning the wave moves π/6 units to the left.

When you’re building a function from scratch, the easiest way to find C is to plug in a known point (like the location of the first peak) after you’ve already determined A, B, and D, then solve for C algebraically.

Putting It All Together: A Worked Example

Suppose you’re given a wave that peaks at 5, bottoms out at 1, completes a full cycle every π units, and hits its first peak at x = π/4.

Amplitude: A = (5 – 1) / 2 = 2

Vertical shift: D = (5 + 1) / 2 = 3

Frequency coefficient: B = 2π / π = 2

Now find C. At the first peak, x = π/4 and y = 5. Plug into y = A cos(Bx – C) + D:

5 = 2 cos(2(π/4) – C) + 3, which simplifies to 2 = 2 cos(π/2 – C). Dividing both sides by 2 gives cos(π/2 – C) = 1. Cosine equals 1 when the angle is 0, so π/2 – C = 0, meaning C = π/2.

The final function: y = 2 cos(2x – π/2) + 3.

Modeling Real-World Data

Cosine functions are especially useful for anything that repeats on a cycle: temperature across a year, daylight hours, tides, or a point on a spinning wheel. The process for building a model from observed data follows the same four steps.

Start by identifying the largest and smallest observed values to get A and D. Use the time between two peaks (or two identical points in the cycle) to determine the period and calculate B. Then pick one known data point, substitute it into the equation, and solve for C. This works because real periodic data, even if slightly messy, can be approximated well by a single cosine curve. The fit won’t be perfect for every data point, but it captures the overall pattern.

Cosine in Programming Languages

If you’re writing a cosine function in code rather than on paper, every major language has a built-in cosine tool, but they all expect the input in radians, not degrees.

Python

For a single value, use the math module: math.cos(x). For arrays of values, NumPy is more efficient: numpy.cos(array) takes an array of radian values and returns all the cosine results at once. To convert degrees to radians first, multiply by π/180 or use numpy.radians().

JavaScript

Use Math.cos(x), where x is in radians. The return value falls between -1 and 1. If you’re starting with degrees, convert with degrees * Math.PI / 180 before passing the value in. Passing Infinity or NaN returns NaN.

C++

Include the <cmath> header and call cos(x). The function accepts float, double, or long double arguments (all in radians) and returns a value in the range -1.0 to 1.0.

In every language, the key mistake people make is passing degrees directly. A call like cos(90) does not return 0, because the function interprets 90 as 90 radians, not 90 degrees. Always convert first.