Main Content

Calculate Tangent Plane to Surface

This example shows how to approximate gradients of a function by finite differences. It then shows how to plot a tangent plane to a point on the surface by using these approximated gradients.

Create the function f ( x , y ) = x 2 + y 2 using a function handle.

f = @(x,y) x.^2 + y.^2;

Approximate the partial derivatives of f ( x , y ) 关于 x and y by using thegradient函数。选择一个有限的不同ence length that is the same as the mesh size.

[xx,yy] = meshgrid(-5:0.25:5); [fx,fy] = gradient(f(xx,yy),0.25);

The tangent plane to a point on the surface, P = ( x 0 , y 0 , f ( x 0 , y 0 ) ) , is given by

z = f ( x 0 , y 0 ) + f ( x 0 , y 0 ) x ( x - x 0 ) + f ( x 0 , y 0 ) y ( y - y 0 ) .

Thefxandfymatrices are approximations to the partial derivatives f x and f y . The point of interest in this example, where the tangent plane meets the functional surface, is(x0,y0) = (1,2). The function value at this point of interest isf(1,2) = 5.

To approximate the tangent planezyou need to find the value of the derivatives at the point of interest. Obtain the index of that point, and find the approximate derivatives there.

x0 = 1; y0 = 2; t = (xx == x0) & (yy == y0); indt = find(t); fx0 = fx(indt); fy0 = fy(indt);

Create a function handle with the equation of the tangent planez.

z = @(x,y) f(x0,y0) + fx0*(x-x0) + fy0*(y-y0);

Plot the original function f ( x , y ) , the pointP, and a piece of planezthat is tangent to the function atP.

surf(xx,yy,f(xx,yy),'EdgeAlpha',0.7,'FaceAlpha',0.9) holdonsurf(xx,yy,z(xx,yy)) plot3(1,2,f(1,2),'r*')

Figure contains an axes object. The axes object contains 3 objects of type surface, line.

View a side profile.

view(-135,9)

Figure contains an axes object. The axes object contains 3 objects of type surface, line.

See Also

Related Topics