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 using a function handle.
f = @(x,y) x.^2 + y.^2;
Approximate the partial derivatives of
关于
and
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, , is given by
Thefx
andfy
matrices are approximations to the partial derivatives
and
. 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 planez
you 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
, the pointP
, and a piece of planez
that 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*')
View a side profile.
view(-135,9)