Plotting surface tangent to surface plot
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (2)
Star Strider
le 6 Fév 2023
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.
.
0 commentaires
MarKf
le 6 Fév 2023
Modifié(e) : MarKf
le 6 Fév 2023
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
0 commentaires
Voir également
Catégories
En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
