How can I create a surface plot of a function of 3 variables?
Afficher commentaires plus anciens
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.
Réponses (2)
Azzi Abdelmalek
le 29 Avr 2016
Modifié(e) : Azzi Abdelmalek
le 29 Avr 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)
Catégories
En savoir plus sur Surfaces and Volumes dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!