How do I fix my meshgrid?

조회 수: 40(최근 30일)
Briana Pass
Briana Pass 2019년 10월 24일
댓글: Briana Pass 2019년 10월 24일
This is the code i have inserted and im getting this. Where is the mistake in my coding and how do I fix this?
[x,Y] = meshgrid (-8:0.5:8);
z = sin (R)/R
  댓글 수: 4
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 2019년 10월 24일
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:
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:
Hope this helps!
  댓글 수: 1
Briana Pass
Briana Pass 2019년 10월 24일
Thank you so much this helps!!!!!!!

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by