plot a 3-d figure (rhombus inside a cricle)
6 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Teja Muppirala
on 25 Apr 2011
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);
0 Comments
More Answers (3)
Sean de Wolski
on 25 Apr 2011
Pseudocode (I don't want to do your whole assignment for you):
- 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)
- Create a new variable that is the double precision equivalent of the circle. (C in the code below)
- Set the square in the middle of the circle (the logical circle) to false (M)
- Find the row/col coordinates of both sets of points in the map (true and false) ([rhave chave] and [rwant cwant])
- Use griddata and some fun logical indexing expressions to overwrite those values. I'll provide the code for that.
- 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 Comments
Sean de Wolski
on 25 Apr 2011
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
on 25 Apr 2011
Actually, the problem statement did say that it is a potential, as in Teja's answer.
Andrew Newell
on 25 Apr 2011
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)
1 Comment
Sean de Wolski
on 25 Apr 2011
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
on 25 Apr 2011
1 Comment
Andrew Newell
on 25 Apr 2011
For the benefit of other people reading this question, I recommend you accept Teja's response.
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!