Gram-Schmidt Vector Projection & Orthogonalization Calculator (2D)

Decompose vector v into parallel projection proj_u(v) and orthogonal Gram-Schmidt component w.

Orthogonal Gram-Schmidt Residual ||v_⊥|| 4 magnitude
Scalar Projection (u·v / ||u||, signed) 3 length
Angle Between Vectors u and v 53.13 degrees
Inner Dot Product (u · v) 12 u·v
Inputs
Try:
u_x
First vector u x-component.
u_y
First vector u y-component.
v_x
Second vector v x-component.
v_y
Second vector v y-component.
Results & Graph
Orthogonal Gram-Schmidt Residual ||v_⊥||
4 magnitude
Scalar Projection (u·v / ||u||, signed)
3 length
Angle Between Vectors u and v
53.13 degrees
Inner Dot Product (u · v)
12 u·v

Orthogonal Gram-Schmidt Residual ||v_⊥||

Graph 1 • How the inputs feed Gram-Schmidt & Projection
Gram-Schmidt Orthogonalization, Vector Projection & Cosine Angle Calculator • Custom Domain Schematic (Linear Algebra & Vectors) Invariants: uVectorX, uVectorY (Gram-Schmidt & Project) U Vector X: 4 u_x • U Vector Y: 0 u_y ORTHOGONAL GRAM-SCHMIDT RE 4 magnitude • Scalar Projection (u·v: 3 length • Angle Between Vectors : 53.13 degrees Eq: proj_u(v) = [ (u · v) / ||u||² ]
Graph 2 • Orthogonal Gram-Schmidt Residual ||v_⊥|| as U Vector X changes (40% to 160% of current)
0 magnit 2.20 magnit 4.40 magnit 4 1.60u_x 4 2.40u_x 4 3.20u_x 4 4u_x 4 4.80u_x 4 5.60u_x 4 6.40u_x
Graph 3 • U Vector X and U Vector Y together Compares Orthogonal Gram-Schmidt Residual ||v_⊥|| when U Vector Y is shifted +25% (solid) vs. −25% (dashed).
0 magnit 0.820 magnit 1.64 magnit --- U Vector Y @ 75% 1.10 -30% 0.724 -15% 0.371 +0% 0.0450 +15% 0.252 +30%
Summary: Projecting v=(3,4) onto u=(4,0) gives proj_u(v)=(3, 0) and orthogonal Gram-Schmidt vector w=(0, 4) with angle θ = 53.13°.
Summary: Projecting v=(3,4) onto u=(4,0) gives proj_u(v)=(3, 0) and orthogonal Gram-Schmidt vector w=(0, 4) with angle θ = 53.13°.
Interactive chart & math engine powered by CalculatorGraph

Formula: Gram-Schmidt & Projection Formula

proju(v) = [ (u · v) / ||u||² ] × u
uorth = v − proju(v)
cos(θ) = u · v||u||·||v||
proj_u(v) = [ (u · v) / ||u||² ] × u; u_orth = v − proj_u(v); cos(θ) = (u · v) / (||u||·||v||)
  • uVectorX: U Vector X (u_x)
  • uVectorY: U Vector Y (u_y)
  • vVectorX: V Vector X (v_x)
  • vVectorY: V Vector Y (v_y)

Frequently Asked Questions

How does the Gram-Schmidt Orthogonalization, Vector Projection & Cosine Angle Calculator compute the solution?

It evaluates proj_u(v) = [ (u · v) / ||u||² ] × u; u_orth = v − proj_u(v); cos(θ) = (u · v) / (||u||·||v||) directly in your browser and updates three custom SVG visualizations in real time.

How do I check that the Gram-Schmidt result is correct?

Take the dot product of the residual with u. It should be zero. On the defaults the residual is (0, 4) and u is (4, 0), and 0 times 4 plus 4 times 0 is 0. Then confirm that the projection plus the residual adds back to v: (3, 0) plus (0, 4) gives (3, 4). Both checks take seconds and catch almost every sign slip.

Why does the dot product change but nothing else when I scale u?

The dot product grows in proportion to the length of u, but the projection divides by the squared length, so the scale cancels. On the presets, u of 2.4, 4 and 6 along the x axis gives dot products of 7.2, 12 and 18, yet the projection stays (3, 0) and the residual stays (0, 4).

What is the difference between the projection and the residual?

The projection is the part of v that points along u. The residual is the part left over, which is perpendicular to u. Together they add up to v exactly. On the default inputs the projection is (3, 0) and the residual is (0, 4), lengths 3 and 4, matching the sides of a 3, 4, 5 right triangle.

Can the scalar projection be negative?

Yes. The page shows the signed scalar projection, the dot product divided by the length of u, and it is negative whenever the angle exceeds 90 degrees. With v = (minus 3, 4) and the default u it shows minus 3, and the projection vector is (minus 3, 0). The residual is unaffected and stays (0, 4). For the plain length of the projection, drop the sign.

How do I turn the result into unit vectors?

Divide each vector by its length. For u = (3, 1) and v = (2, 2), u has length about 3.162 and the residual (minus 0.4, 1.2) has length 1.265, so the unit vectors are roughly (0.949, 0.316) and (minus 0.316, 0.949). Their dot product is close to zero and each length is close to 1.

What does a residual of zero mean?

It means v lies along the same line as u, so the two vectors are linearly dependent. With u = (1, 2) and v = (2, 4) the page shows an angle of 0 degrees and a residual length of 0. Gram-Schmidt cannot build a second basis vector from that pair; you need a v pointing in a different direction. Nearly zero residuals deserve the same caution, since rounding then dominates.

Does it matter which vector I enter as u?

Yes. The first vector is kept as it is and the second is adjusted. Entering u = (4, 0) and v = (3, 4) gives a residual of (0, 4). Swapping them gives a projection of (1.44, 1.92) and a residual of (2.56, minus 1.92). Both are valid perpendicular pairs, but they are different bases for the plane.

Why does the page reject a zero vector?

A zero vector has no direction, so the projection onto it and the angle it makes with another vector are both undefined. Dividing by its squared length would mean dividing by zero. The page therefore returns an error for u = (0, 0) or v = (0, 0). Enter two vectors with at least one non-zero component each and the outputs become meaningful.

How is this used in least squares?

Fitting a single direction to data by least squares is a projection. The projection of v onto u is the closest point to v on the line through u, and the residual is the error that remains, perpendicular to that line. Its length, 4 on the defaults, is the smallest possible distance from v to the line. The same idea, repeated over many columns, underlies the QR method for regression.

Can I use this calculator for 3D vectors?

Only when both vectors have a zero z component, because the page reads x and y alone. For general 3D vectors, apply the same formula by hand with three components: include the z products in the dot product and squared length, then subtract the projection from each component. A third vector needs a further step against both earlier ones.

Splitting v = (3, 4) along u = (4, 0)

With the default inputs, u points along the x axis with length 4, and v is the familiar 3, 4, 5 vector. The calculator breaks v into two pieces: one that lies along u, and one that is perpendicular to it.

The dot product comes first. Four times three plus zero times four is 12, which the page shows as the inner dot product. Dividing by the squared length of u, which is 16, gives a scale factor of 0.75. Multiply u by 0.75 and you get the projection, (3, 0). Its signed length along u, 3, appears as the scalar projection, which is the dot product divided by the length of u.

The perpendicular piece is whatever is left: v minus the projection, so (3, 4) minus (3, 0), which is (0, 4). Its length, 4, is the headline answer, labelled the orthogonal Gram-Schmidt residual. The angle between u and v is 53.13 degrees, the angle whose cosine is 12 divided by 4 times 5, or 0.6.

Because u lies on an axis, the default is almost too easy: the projection is just the x part of v and the residual is the y part. It is a good starting point for checking the formula, and a less tidy example follows.

One Gram-Schmidt step, worked on (3, 1) and (2, 2)

Gram-Schmidt on two vectors is a single projection. Keep the first vector as it is and call it u1. Replace the second vector with u2 equal to v2 minus the projection of v2 on u1. The result is a pair of perpendicular vectors that span the same plane as the original pair.

Take u = (3, 1) and v = (2, 2). The dot product is 6 plus 2, which is 8. The squared length of u is 9 plus 1, which is 10. The scale factor is 0.8, so the projection is 0.8 times (3, 1), or (2.4, 0.8). Subtract that from v and the residual is (minus 0.4, 1.2). Entering these four numbers on the page returns the same vectors, a residual length of 1.265, a scalar projection of 2.53 and an angle of 26.57 degrees.

Always check the result by taking the dot product of the residual with u. Here that is 3 times minus 0.4, plus 1 times 1.2, which is minus 1.2 plus 1.2, exactly 0. If that check does not come out at zero, apart from tiny rounding, the subtraction went wrong somewhere, most often a sign on one component.

The calculator reports the residual's length but not its direction as a unit vector, so normalising is a manual step, covered in the next section.

Normalising to an orthonormal pair

Gram-Schmidt in a textbook usually ends with an orthonormal basis: perpendicular vectors that each have length 1. To get there, divide each vector by its own length.

On the default inputs this is trivial. u = (4, 0) has length 4, so the first unit vector is (1, 0). The residual (0, 4) has length 4, so the second unit vector is (0, 1). The algorithm has recovered the standard axes.

On the (3, 1) and (2, 2) example the numbers are less round. The length of u is the square root of 10, about 3.162, so the first unit vector is about (0.949, 0.316). The residual has length 1.265, so the second unit vector is minus 0.4 and 1.2 each divided by 1.265, about (minus 0.316, 0.949). Check the pair: each has length 1 within rounding, and their dot product is close to zero.

You can normalise at the end or after each step. In two dimensions it makes no difference. In longer calculations, normalising as you go keeps the numbers from growing or shrinking and makes the projection formula simpler, since dividing by a squared length of 1 does nothing.

Why stretching u leaves the answer unchanged

Only the x component of u changes in the comparison presets, from 2.4 through 4 to 6, with v fixed at (3, 4). The dot product moves from 7.2 to 12 to 18, but the projection stays at (3, 0), the residual stays at (0, 4) with length 4, and the angle stays at 53.13 degrees.

That is not a fault. Projection depends only on the direction of u, not its length. Doubling u doubles the dot product and quadruples the squared length, so the scale factor halves and the projected vector ends up exactly where it was. Even u = (1, 0) gives the same result.

Reversing u does not change the projection either. Set u to (minus 4, 0) and the projection is still (3, 0), because the scale factor turns negative and flips the reversed vector back. What changes is the angle, which becomes obtuse, the supplement of 53.13 degrees, and the dot product, which becomes minus 12.

So if you want to see the outputs move, change v or rotate u by giving it a y component. Stretching u is a useful demonstration of invariance and nothing more.

Order matters: projecting u onto v instead

Gram-Schmidt keeps the first vector fixed and adjusts the second, so the order you enter them in changes the basis you get. Swap the defaults so that u = (3, 4) and v = (4, 0).

The dot product is still 12 and the angle is still 53.13 degrees, since both are symmetric. Everything else changes. The scale factor is 12 over 25, so the projection is (1.44, 1.92) and the residual is (2.56, minus 1.92), with length 3.2. The scalar projection becomes 2.4, which is 12 divided by the length of the new u, 5.

Both answers are valid orthogonal bases for the plane. They just point in different directions. In practice the first vector is the one whose direction you want to keep exactly, such as the first column of a matrix in a QR decomposition, or the direction of a slope when resolving a force into components along and across it.

Obtuse angles and a negative scalar projection

When the angle between u and v is greater than 90 degrees, the projection points opposite to u. Set v to (minus 3, 4) with the default u and the page shows a dot product of minus 12, an obtuse angle (the supplement of 53.13 degrees) and a projection of (minus 3, 0) in the summary sentence.

The scalar projection line reads minus 3. It is signed: the dot product divided by the length of u, sometimes called the component of v along u. The minus sign says the shadow of v falls on the opposite side of the origin from u. If you need the length of the projected vector instead, drop the sign: it is 3.

The residual is unaffected by the sign. It is still (0, 4) with length 4, and its dot product with u is still 0.

Parallel, perpendicular and zero vectors

Three special cases are worth running once so you recognise them. If v is already perpendicular to u, the projection vanishes. Try u = (3, 4) and v = (4, minus 3): the dot product is 0, the angle is 90 degrees, the projection is (0, 0) and the residual is all of v, with length 5.

If v is parallel to u, the residual vanishes instead. Try u = (1, 2) and v = (2, 4): the angle is 0 degrees, the projection is the whole of v and the residual is (0, 0). This is the signal that the two vectors are linearly dependent. They span only a line, so Gram-Schmidt cannot produce a second basis vector, and normalising a zero residual would mean dividing by zero.

The third case is a zero vector. Projection onto a zero vector is undefined, and so is the angle between a zero vector and anything else. The page refuses u = (0, 0) or v = (0, 0) with an error asking for two non-zero vectors, so you will not get a misleading 90 degrees from it.

Only two components, so 3D needs a second round

The page name now ends in 2D, and that is accurate: it takes x and y components only, so it works in the plane. The formulas carry over to three dimensions without change: add the z products to the dot product and to the squared length, and subtract the projection component by component.

Gram-Schmidt on three vectors in space takes two rounds. Keep u1 equal to v1. Make u2 by removing from v2 its projection on u1, exactly as this page does. Then make u3 by removing from v3 its projections on both u1 and u2. Check by confirming all three pairwise dot products are zero.

You can use this page for the second step of a 3D problem only if both vectors happen to have a zero z component. For general 3D work, do the arithmetic by hand or with matrix software. When the vectors are nearly parallel, the classical method can lose accuracy through rounding, and the modified version, which projects out each earlier vector one at a time from the running result, is the usual remedy in numerical code.