Plotting surface tangent to surface plot

조회 수: 4 (최근 30일)
Kevin Abdul
Kevin Abdul 2023년 2월 6일
편집: MarKf 2023년 2월 6일
clc
clear
f=@(x,y)-38+19.*x-62.*y+5.*x.^2-14.*x.*y-3*y.^2;
dfdx=@(x,y)10.*x+19-14.*y;
dfdy=@(x,y)14.*x-6.*y-62;
L=@(x,y,a,b)f(a,b)+dfdx(a,b)*(x-a)+dfdy(a,b)*(y-b);
n=@(x,y)[dfdx(x,y);dfdy(x,y);-1];
a=-4;
b=-2;
p0=[a;b;f(a,b)];
n0=n(a,b);
x=linspace(-9,0.5,1);
y=linspace(-7,0.5,3);
[X,Y]=meshgrid(x,y);
Z=f(X,Y);
T=L(X,Y,a,b);
surf(x,y,Z)
hold on
surf(x,y,T,'FaceColor','b','FaceAlpha',0.4) % Tangentplanet
s=[-1 1];
plot3(p0(1)+s*n0(1),p0(2)+s*n0(2),p0(3)+s*n0(3),'m','linewidth',2)
hold off
xlabel('x'), ylabel('y'), zlabel('z'), box on
axis equal, axis vis3d, rotate3d on 2
clc
clear
f=@(x,y)-38+19.*x-62.*y+5.*x.^2-14.*x.*y-3*y.^2;
dfdx=@(x,y)10.*x+19-14.*y;
dfdy=@(x,y)14.*x-6.*y-62;
L=@(x,y,a,b)f(a,b)+dfdx(a,b)*(x-a)+dfdy(a,b)*(y-b);
n=@(x,y)[dfdx(x,y);dfdy(x,y);-1];
a=-4;
b=-2;
p0=[a;b;f(a,b)];
n0=n(a,b);
x=linspace(-9,0.5,1);
y=linspace(-7,0.5,3);
[X,Y]=meshgrid(x,y);
Z=f(X,Y);
T=L(X,Y,a,b);
surf(x,y,Z)
hold on
surf(x,y,T,'FaceColor','b','FaceAlpha',0.4) % Tangentplanet
s=[-1 1];
plot3(p0(1)+s*n0(1),p0(2)+s*n0(2),p0(3)+s*n0(3),'m','linewidth',2)
hold off
xlabel('x'), ylabel('y'), zlabel('z'), box on
axis equal, axis vis3d, rotate3d on 2
Hi, I have a problem with this code. I gives this error in the console
Error using surf
Z must be a matrix, not a scalar or vector.
Error in studio22 (line 21)
surf(x,y,Z)
How should I go about solving this?

답변 (2개)

Star Strider
Star Strider 2023년 2월 6일
The code defines ‘x’ as a scalar:
x=linspace(-9,0.5,1);
with one element equal to 0.5.
That creates ‘X’ and ‘Y’ as vectors, and makes ‘Z’ a vector as well.
.

MarKf
MarKf 2023년 2월 6일
편집: MarKf 2023년 2월 6일
You should use a matrix, not a scalar or vector, as the error says.
Z (and T) are vectors, probably an issue with how you define x and y with linspace (x being a scalar), which then makes meshgrid not create a grid, which is why then the 2 functions don't return matrices. Not sure what you meant to do, but for example this works:
f=@(x,y)-38+19.*x-62.*y+5.*x.^2-14.*x.*y-3*y.^2;
dfdx=@(x,y)10.*x+19-14.*y;
dfdy=@(x,y)14.*x-6.*y-62;
L=@(x,y,a,b)f(a,b)+dfdx(a,b)*(x-a)+dfdy(a,b)*(y-b);
n=@(x,y)[dfdx(x,y);dfdy(x,y);-1];
a=-4;
b=-2;
p0=[a;b;f(a,b)];
n0=n(a,b);
x=-9:0.5:1; % or x=linspace(-9,0.5,10);
y=-7:0.5:3; % or y=linspace(-7,0.5,10);
[X,Y]=meshgrid(x,y);
Z=f(X,Y);
T=L(X,Y,a,b);
surf(x,y,Z);
hold on
surf(x,y,T,'FaceColor','b','FaceAlpha',0.4) % Tangentplanet
s=[-1 1];
plot3(p0(1)+s*n0(1),p0(2)+s*n0(2),p0(3)+s*n0(3),'m','linewidth',2)
hold off
xlabel('x'), ylabel('y'), zlabel('z'), box on
axis equal, axis vis3d

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by