plot a 3-d figure (rhombus inside a cricle)

3 次查看(过去 30 天)
Hello everybody.
I have an assignment to plot shape inside other shape. The assignment is related to electromagnetic fields, I need to describe the potential behavior from low potential to high potential. anyway all of this doesn't matter. because if someone will help with the code of the plot, I'll handle the rest.
The figure I need is a rhombus inside a circle a 3-d plot of course. I added an example of square inside a circle to illustrate my meaning.
THANK YOU VERY MUCH!

采纳的回答

Teja Muppirala
Teja Muppirala 2011-4-25
Just solve Laplace's equation: Uxx + Uyy = 0.
This makes the plot you have shown. Doing it with a rhombus instead of a square should be pretty straight forward.
N = 25;
NN = 2*N+1;
boxh = 10;
[xgrid,ygrid] = meshgrid(-N:N);
r = xgrid.^2 + ygrid.^2;
maskouter = find(r >= N^2);
maskinner = find(abs(xgrid) <= boxh & abs(ygrid) <= boxh);
L = speye(NN^2);
for n = setdiff(1:NN^2,[maskinner(:) ; maskouter(:)])
L(n,n + [-NN -1 0 1 NN]) = [1 1 -4 1 1];
end
RHS = zeros(NN^2,1);
RHS(maskinner) = 1;
U = reshape(L\RHS,NN,NN);
surf(U);

更多回答(3 个)

Sean de Wolski
Sean de Wolski 2011-4-25
Pseudocode (I don't want to do your whole assignment for you):
  1. Use the formula for a circle to create a logical circle the size you want (both circle size and matrix size) http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F . (M in the code below)
  2. Create a new variable that is the double precision equivalent of the circle. (C in the code below)
  3. Set the square in the middle of the circle (the logical circle) to false (M)
  4. Find the row/col coordinates of both sets of points in the map (true and false) ([rhave chave] and [rwant cwant])
  5. Use griddata and some fun logical indexing expressions to overwrite those values. I'll provide the code for that.
  6. Use mesh() to visualize
C(M) = griddata(rhave,chave,C(~M),rwant,cwant,'linear');
And to prove that I'm not talking out of my ass:
You can scale it as you wish at this point because the minimum value is zero and the square is 1; any multiplication will be a linear scale.
Good Luck!
  3 个评论
Sean de Wolski
Sean de Wolski 2011-4-25
Matt you're absolutely right! His obviously non-convex while mine is. There was nothing in the problem statement about that though; he said he'd take care of it. (This was a fun Monday morning wake-up challenge)
Andrew Newell
Andrew Newell 2011-4-25
Actually, the problem statement did say that it is a potential, as in Teja's answer.

请先登录,再进行评论。


Andrew Newell
Andrew Newell 2011-4-25
Suppose your potential function is z = V(x,y). Define V so that it can input matrices for x and y, e.g.,
V = @(x,y) 1./(x.^2+y.^2+1);
then create the matrices for your problem area, e.g.,
x = -40:1:40; y = -40:1:40;
[x,y] = meshgrid(x,y);
z = V(x,y);
Now you can plot it:
surf(x,y,z)
See surf,surfc for ways to adjust the appearance of the plot.
  1 个评论
Sean de Wolski
Sean de Wolski 2011-4-25
or do that inside bsxfun, i.e.:
z = bsxfun(@V,-40:40,(-40:40).');
Which means in theory (a detailed enough V function), this entire plot could be one-lined!

请先登录,再进行评论。


eli
eli 2011-4-25
Thank you all guys. In a matter of fact I eventually succeeded by myself. All I had to do is to think better of the part which sets 100v in the matrix(stubbornness is sin :)). I appreciate your response. If someone interested I can upload the entire code.
  1 个评论
Andrew Newell
Andrew Newell 2011-4-25
For the benefit of other people reading this question, I recommend you accept Teja's response.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by