This is the code i have inserted and im getting this. Where is the mistake in my coding and how do I fix this?
gridspacing=-8:0.5:8;
Y=-8:0:8;
Z=-8:1.5:8;
[x,Y] = meshgrid (-8:0.5:8);
Z=sqrt(exp(-x.^2-Y.^2))
surf(x,Y,Z,x.*exp(Y))
Z(R==0)=1
z = sin (R)/R

4 件のコメント

Shubham Gupta
Shubham Gupta 2019 年 10 月 24 日
What result were you expecting? Why are you defining gridspacing, Y & Z even though you are redefing Y & Z and not using gridspacing? You have not defined R?
Briana Pass
Briana Pass 2019 年 10 月 24 日
I was supposed to use these steps to create The attached image. This is my first time coding and I am trying to figure out my mistakes to prevent similar ones in the future.Screen Shot 2019-10-23 at 9.26.49 PM.png
  1. Use the meshgrid function with the following data: -8: 0.5: 8
  1. Use the following equation: R=x2+y2 treat x and y as vectors
  2. Use the Z=sin RR
  3. Hint: you will need the following: Z(R==0)=1
  4. Use the mesh function for you variables
Katie
Katie 2019 年 10 月 24 日
In your Y vector, your step size is 0 so it's an empty vector. Are you trying to pre-allocate the memory for the Y matrix that is generated by the meshgrid function? If so, you can use the following:
Y=zeros(length(gridspacing),length(gridspacing));
However, this will not change the plotted result. If you could provide some more info about what you're expecting to see, that would be great!
Briana Pass
Briana Pass 2019 年 10 月 24 日
If you look at my above response I attached my coding instructions and a picture of how my mesh grid should look. Thank you!

サインインしてコメントする。

 採用された回答

Katie
Katie 2019 年 10 月 24 日

1 投票

Hi! Thanks for providing extra details! Below are the steps I used with explanations for each. I also labeled them in accordance with the steps in the instructions you provided.
close all; clear all; clc; %close all open figure windows, clear the variables, and clear the command window
%Step 1:
gridspacing = -8: 0.5: 8; %setup the grid spacing vector that will be used to make the meshgrid
[X Y]=meshgrid(gridspacing); %create the mesh grid
%Step 2:
R=sqrt(X.^2 + Y.^2); %Create a 2D set of values from the X and Y grids created with meshgrid
In the instructions you provided, it says that R=X^2 + Y^2. If you use this equation, you get a plot that does not match the figure you have provided. If you use the R equation in the instructions, you do get the right answers. Additionally, it says to treat X and Y like vectors. I think by this they're hinting that you should use element-wise powers (.^ rather than ^) on the matrices, not use vectors for x and y.
%Step 3:
Z=sin(R)./R; %use element-wise division to do this calculation on each grid point
Z(R==0)=1; %in any location that R is equal to 0, set Z=1
%Step 4:
figure;
mesh(x,y,Z); %surf gives you a similar plot but it's shaded in rather than a wire mesh.
The default colormap is going to be perula, while it looks like the colormap for the example figure you show is jet. You can easily change this with one line of code after you use the mesh function:
colormap('jet');
test.PNG
Hope this helps!

1 件のコメント

Briana Pass
Briana Pass 2019 年 10 月 24 日
Thank you so much this helps!!!!!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2019 年 10 月 24 日

コメント済み:

2019 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by