How can I create a surface plot of a function of 3 variables?
Show older comments
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.
Answers (2)
Azzi Abdelmalek
on 29 Apr 2016
Edited: Azzi Abdelmalek
on 29 Apr 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 Comment
Kamil Jiwa
on 29 Apr 2016
Categories
Find more on Surfaces and Volumes in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!