How can I create a surface plot of a function of 3 variables?
Mostrar comentarios más antiguos
I have the following function that describes a quadric surface:
syms f(x, y, z)
f(x, y, z) = -x^2 - (y^2 / 4) + (z^2 / 4) == 1
I'm trying to understand how to plot it. I've tried a few things, like solving the equation for z, and then filling in a matrix containing z values. e.g.
syms g(x, y);
g(x, y) = solve(f(x, y, z), z); % matrix([[-(4*x^2 + y^2 + 4)^(1/2)], [(4*x^2 + y^2 + 4)^(1/2)]])
xrange = -10:10;
yrange = -10:10;
zrange = zeros(numel(xrange), numel(yrange));
for i = 1:numel(xrange)
for j = 1:numel(yrange)
zrange(i, j) = g(xrange(i), yrange(j));
end
end
Unfortunately, this code does not because z can take on multiple values for a given (x, y) value. MATLAB rightly complains:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Any tips? Do I need to make xrange and yrange into matrix values? If so, then what should they look like? If not, then what are some other things I can look into?
Thanks.
Respuestas (2)
Azzi Abdelmalek
el 29 de Abr. de 2016
Editada: Azzi Abdelmalek
el 29 de Abr. de 2016
syms f(x, y, z)
f(x, y, z) = -x^2 - (y^2 / 4) + (z^2 / 4) == 1
syms g(x, y);
g(x, y) = solve(f(x, y, z), z); % matrix([[-(4*x^2 + y^2 + 4)^(1/2)], [(4*x^2 + y^2 + 4)^(1/2)]])
xrange = -10:10;
yrange = -10:10;
zrange = zeros(numel(xrange), numel(yrange));
for i = 1:numel(xrange)
for j = 1:numel(yrange)
a=g(xrange(i), yrange(j));
zrange1(i, j) = double(a(1));
zrange2(i,j)=double(a(2));
end
end
close
surf(xrange,yrange,zrange1)
hold on
surf(xrange,yrange,zrange2)
1 comentario
Kamil Jiwa
el 29 de Abr. de 2016
Categorías
Más información sobre Surfaces and Volumes en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!