How can I create a surface plot of a function of 3 variables?
이전 댓글 표시
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.
답변 (2개)
Azzi Abdelmalek
2016년 4월 29일
편집: Azzi Abdelmalek
2016년 4월 29일
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)
카테고리
도움말 센터 및 File Exchange에서 Surfaces and Volumes에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!