Ploting vector perpendicular to a surface in a specific point

3 Ansichten (letzte 30 Tage)
Hello.
I want to plot the gradient of a surface in a specific point.
I know the gradient is perpendicular to the level curve of a defined function like:
chosing a z and determinating the points of the pairs we can obtain the slope of the tangent in a two dimension plane, and it works fine.
The problem is in the 3D plot. I make the function , so i have the "level surface 0", and i want the orthogonal vector at the point . So, by theory, gives me the orthogonal vector at .
These, also, are the coefficients of the tangent plane .
The surface is perfect ploted, the tangent plane too, but i can't plot the orthogonal vector at , and the vector value is the gradient value . I thought that quiver3 with the arguments and it was going to work. At this point i have tried several proccedures but none of them returns me the surface orthogonal vector.
Here is my code, (the error is at almost the end of the code in the quiver3 function)
clear;
clc;
close all;
% https://la.mathworks.com/help/matlab/math/calculate-tangent-plane-to-surface.html?lang=en
% function definition
f = @(x,y) x.^2 + y.^2;
% creating the mesh
DD = .5;
[xx,yy] = meshgrid(-10:DD:10);
% dfx & dfy are numerical approx to the gradient
[dfx,dfy] = gradient(f(xx,yy),DD);
% Point to be plot the tangent plane and the perpendicular vector
x0 = 1;
y0 = 2;
% looking for the value of the gradient at the point x0,y0
t = (xx == x0) & (yy == y0);
indt = find(t); % retunrs the index position of x0 and y0
% numeric value of the gradient in the position
dfx0 = dfx(indt);
dfy0 = dfy(indt);
% tangent plane definition
z = @(x,y) f(x0,y0) + dfx0*(x-x0) + dfy0*(y-y0);
hh = figure(1);
set(hh,'color','w','Position',[1920*.4 1080*.5 1920*.5 1080*.4])
subplot(1,2,1)
% plot the function
surf(xx,yy,f(xx,yy),'EdgeAlpha',0.7,'FaceAlpha',0.9)
view(3)
hold on
% plot the tangent plane
surf(xx,yy,z(xx,yy))
hold off;
axis([-10 10 -10 10 0 100])
subplot(1,2,2)
surf(xx,yy,f(xx,yy),'EdgeAlpha',0.7,'FaceAlpha',0.9)
hold on
surf(xx,yy,z(xx,yy))
plot3(1,2,f(1,2),'ro','MarkerSize',17,'MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6]);
% ------- HERE IS THE ERROR --------
quiver3(x0,y0,f(x0,y0),dfx0,dfy0,-1,0); % -> here is the error
% ------- I THINK IS THE QUIVER FUNCTION MY PROBLEM -------
view(3)
hold off;

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 21 Aug. 2020
Bearbeitet: Bruno Luong am 21 Aug. 2020
You are tricked by the scaling. If you want to observe the orthogonal by eye you should set the same scaling for x-y-z
quiver3(x0,y0,f(x0,y0),dfx0,dfy0,-1,0); % -> here is the error
% ------- I THINK IS THE QUIVER FUNCTION MY PROBLEM -------
axis equal % <=== HERE FIX YOUR "PROBLEM"
view(3)
You course your parabolid is going high, si x-y plane will shrink relatively.
  2 Kommentare
Diego Lazcano Colodrero
Diego Lazcano Colodrero am 21 Aug. 2020
Thank you bruno. Your answer it is correct.
It's a pity that it cannot see more friendly, like others software
I tried with camera positions but I can't make it look firndly for people who are learning math.
Any sugestions will be greatfully thank.
Thank you so much.
Regards.-
Diego Lazcano Colodrero
Diego Lazcano Colodrero am 21 Aug. 2020
Thanks Bruno, with your tip I managed to get a better picture.
For someone who is looking for these, here are some steps that could help:
  1. first I have plotted with the axis equal command as you mentioned.
  2. then I adjust the view of the figure with the Pan and Rotate 3D buttons, being aware not to change the axis ratios.
  3. without closing the Figure, in the command window I get the view arguments with the command [cel caz] = view
  4. again in the command window, I executed axis to have the actual axis limits of the plot
  5. after that I pasted by hand, the values of cel and caz into the view command in the code; and the values that returned axis. You can see this in the lines 51 and 52 of the code.
  6. Then just run the code, and you will have a very nice plot
Here is the complete code:
clear;
clc;
close all;
% https://la.mathworks.com/help/matlab/math/calculate-tangent-plane-to-surface.html?lang=en
% function definition
f = @(x,y) x.^2 + y.^2;
% creating the mesh
DD = .5;
[xx,yy] = meshgrid(-10:DD:10);
% dfx & dfy are numerical approx to the gradient
[dfx,dfy] = gradient(f(xx,yy),DD);
% Point to be plot the tangent plane and the perpendicular vector
x0 = 1;
y0 = 2;
% looking for the value of the gradient at the point x0,y0
t = (xx == x0) & (yy == y0);
indt = find(t); % retunrs the index position of x0 and y0
% numeric value of the gradient in the position
dfx0 = dfx(indt);
dfy0 = dfy(indt);
% tangent plane definition
z = @(x,y) f(x0,y0) + dfx0*(x-x0) + dfy0*(y-y0);
hh = figure(1);
set(hh,'color','w','Position',[1920*.4 1080*.5 1920*.5 1080*.4])
subplot(1,2,1)
% plot the function
surf(xx,yy,f(xx,yy),'EdgeAlpha',0.7,'FaceAlpha',0.9)
view(-6.0985e+01,6.7418e+00)
hold on
% plot the tangent plane
surf(xx,yy,z(xx,yy))
hold off;
axis([-1.0000e+01 1.0000e+01 -8.9630e+00 1.1037e+01 0 1.0000e+02])
subplot(1,2,2)
surf(xx,yy,f(xx,yy),'EdgeAlpha',0.7,'FaceAlpha',0.9)
hold on
surf(xx,yy,z(xx,yy))
plot3(1,2,f(1,2),'ro','MarkerSize',6,'MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6]);
h = quiver3(x0,y0,f(x0,y0),dfx0,dfy0,-1,'Color','red','LineWidth',1.75,'AutoScale','off','ShowArrowHead','on','MaxHeadSize',34);
% --- copied from [cel caz] = view and axis command from command line ---
axis([-9.5833e+00 6.0065e+00 -9.1437e+00 1.0343e+01 -1.1458e-01 2.7167e+01]);
view(-1.1284e+02,3.5262e+00);
% -----------------------------------------------------------------------
hold off;
% ----------------------- Titles and more ------------------------------
subplot(1,2,1)
title({"Paraboloide $z=f(x,y)=x^2+y^2$","Plano $\pi:\ 2x+4y-z-5=0$"},'interpreter','latex','FontSize',15)
xlabel("$x$",'interpreter','latex','FontSize',12)
ylabel("$y$",'interpreter','latex','FontSize',12)
zlabel("$z$",'interpreter','latex','FontSize',12)
subplot(1,2,2)
title({"Intersecci\'on de $z=f(x,y)=x^2+y^2$","con el Plano $2x+4y-z-5=0$","en el punto $(x_0,y_0,z_0)=(1,2,5)$","y gradiente $\nabla F = (2,4,-1)$"},'interpreter','latex','FontSize',14)
xlabel("$x$",'interpreter','latex','FontSize',12)
ylabel("$y$",'interpreter','latex','FontSize',12)
zlabel("$z$",'interpreter','latex','FontSize',12)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by