필터 지우기
필터 지우기

Help sketching equation into 3d graph

조회 수: 2 (최근 30일)
Muhd Farhad
Muhd Farhad 2021년 6월 1일
답변: Star Strider 2021년 6월 1일
What I got so far for the equation
Z=5-sqrt(1-x.^(2)-(y-abs(x)).^2).*cos(30.*(1-x.^(2)-(y-abs(x)).^(2)))
Then I tried these lines
x=linspace(-1,1, 200);
y=linspace(-1, 1.5, 200);
[X,Y]=meshgrid(x,y);
Z=5-sqrt(1-x.^(2)-(y-abs(x)).^2).*cos(30.*(1-x.^(2)-(y-abs(x)).^(2)))
zlim([1,6]);
surf(X,Y,Z);
It gave me "Z must be a matrix, not a scalar or vector." error.

채택된 답변

Star Strider
Star Strider 2021년 6월 1일
The error it throws when I run it here is:
X, Y, Z, and C cannot be complex
This is likely because of the negative values in ‘x’ and ‘y’, however I did not do an in-depth analysis.
Dealing with that produces —
x=linspace(-1,1, 200);
y=linspace(-1, 1.5, 200);
[X,Y]=meshgrid(x,y);
Z=@(x,y) 5-sqrt(1-x.^(2)-(y-abs(x)).^(2).*cos(30*(1-x.^(2)-(y-abs(x)).^(2))));
figure
surf(X,Y,real(Z(X,Y)))
grid on
zlim([1,6])
title('Real Z')
figure
surf(X,Y,imag(Z(X,Y)))
grid on
% zlim([1,6])
title('Imag Z')
figure
surf(X,Y,abs(Z(X,Y)))
grid on
zlim([1,6])
title('|Z|')
figure
surf(X,Y,Z(X,Y))
Error using surf (line 71)
X, Y, Z, and C cannot be complex.
grid on
zlim([1,6])
title('Complex Z')
.

추가 답변 (2개)

Joseph Cheng
Joseph Cheng 2021년 6월 1일
what you'll need to do is use the function meshgrid() which will then create a 2D matrix for both x and y such that you can get the pair combination of x and y of your linspace
[x y]=meshgrid(x,y);
%then do your Z
  댓글 수: 2
Joseph Cheng
Joseph Cheng 2021년 6월 1일
As right now
x = 1x200 matrix
y = 1x200 matrix
so since you're using .* and .^ in your Z equation you'll also get a 1x200 for Z.
using meshgrid to generate the 2D grid of x and y combinations such that you can get the range of Z values for x between -1 and 1 and y over the range of -1 and 1.5.
Muhd Farhad
Muhd Farhad 2021년 6월 1일
Owh sry actually I did insert the meshgrid. Updated my post. But still the error shows up.

댓글을 달려면 로그인하십시오.


Girijashankar Sahoo
Girijashankar Sahoo 2021년 6월 1일
% Size of of your output Z must be 200x200 matix
% Try this code for 3D output
x=linspace(-1,1, 200);
y=linspace(-1, 1.5, 200);
for i=1:length(y)
for k=1:length(x)
Z=5-sqrt(1-x.^(2)-(y-abs(x)).^(2).*cos(30*(1-x.^(2)-(y-abs(x)).^(2))));
end
end
zlim([1,6]);
T=rand(200)
surf(x,y,T);

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by