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

조회 수: 3 (최근 30일)
eli
eli 2011년 4월 24일
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에서 General PDEs에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by