Given: Create both a contour and surface plot for the following function.
Find:
  • Create an x vector between -2 and 2, with a step size of .2
  • Create a y vector between -2 and 2, with a step size of .2
  • Create a matrix X and Y that contains a grid of coordinate pairs from x and y
  • Create a matrix Z that is the same size as X and Y
Create the following plots, with labels
  • Contour plot
  • Surface plot
Issue: I have what I believe to be the correct code, however, it's giving me an error stating not enough input arguments...
My Solution: What am I missing here?
x=-2:0.2:2
y=x
[X,Y]=meshgrid(x,y)
Z=x.*exp.^-(x.^2+y.^2)
contour(X,Y,Z)
surf(X,Y,Z)

 채택된 답변

the cyclist
the cyclist 2024년 3월 19일
편집: the cyclist 2024년 3월 19일

1 개 추천

You used exp as if it were a constant (e) that is raised to a power. Instead, you should have used it as a function.
You also used x and y instead of X and Y, when calculating Z.
x=-2:0.2:2;
y=x;
[X,Y]=meshgrid(x,y);
Z=X.*exp(-(X.^2+Y.^2));
figure
contour(X,Y,Z)
figure
surf(X,Y,Z)

댓글 수: 3

Spaceman
Spaceman 2024년 3월 20일
편집: Spaceman 2024년 3월 20일
Eureka! I figured it out after the fact, still new to this whole realm. I am very thankful we live in an age of information such that if I can't understand something after trying it 9 different ways, someone can show me the correct, 10th way. Thank you.
Also do we need parentheses around (X.^2+Y.^2)? As it works just to same without. I only ask because I would put them, but I feel I abuse them.
One last weird question, why when we use a semicolon after a figure, plot, etc... Does it still pop up/display? A semicolon supresses everything I thought.
If you don't use parentheses, you are relying on the defined operator precedence. Sometimes, parentheses are better, just for clarity.
Semicolons suppress command window output, not all output. A semicolon such as
surf(X,Y,Z);
is superfluous.
Spaceman
Spaceman 2024년 4월 8일
Thank you for taking the time to clear that up!

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

추가 답변 (0개)

카테고리

제품

릴리스

R2023b

태그

질문:

2024년 3월 19일

댓글:

2024년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by