|
From: Bookie via MathKB.com on 6 May 2008 18:30 Hello, Does anyone know what the problem might be in a code I've written to describe an irregular-shaped mesh grid boundary. I have a region of points to plot against a function z. The "shadow" cast by this region on the x-y plane of my surface plot, is a region bound above by the bottom right quadrant of a circle, and below by the top right quadrant of a circle, and on the left by a vertical line and on the right by a line-y=mx+b. I've compiled my program but it is generating a 4 sided shape that doesn't have any curves at the top or bottom. I've plotted my circles and lines separately to make sure that my equations are accurate. If anyone has any ideas on what's wrong with my code that would be a great help! Matlab code: clear all clc % Mesh points m = 21; n = 21; T = zeros(5,n,m); [ix,iy] = meshgrid(1:m,1:n); minX = a; maxX = b; midX = c; % y-values change over the range of x %Equation of first lower boundary ranging from x = a to c: % y = sqrt(r1^2-(x-X1)^2)-Y1 (top right quadrant) %Equation of 2nd lower boundary ranging from x = c to b: % y = m*x+b %Equation of upper boundary ranging from x = a to b: % y = (-1*sqrt(r2^2-(x-X2)^2)+Y2 (bottom right quadrant) % X1,Y1 and X2,Y2 are centre coordinates of circles (constants) % r1,r2 are circle radii (constants) x = a + (ix-1)*(b-a)/(m-1); % a <= x <= b if a <= x < c; y = (sqrt(r1^2-(x-X1).^2)-Y1) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)- (sqrt(r1^2-(x-X1).^2)-Y1))/(n-1); % circle1 <= y <= circle2 else c <= x <= b; y = (m.*x+b) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)-(m.*x+b))/(n-1); % m*x+b <= y <= circle2 end for i = 1:n for j = 1:m [T(:,i,j)] = Function(x(i,j),y(i,j)); end end figure Z = squeeze(T(1,:,:)); surf(x,y,real(Z)) axis tight %want x & y axes tight so curves in x-y plane are not skewed colormap hsv colorbar Thanks for your help! Bookie -- Message posted via MathKB.com http://www.mathkb.com/Uwe/Forums.aspx/matlab/200805/1
From: Bookie via MathKB.com on 6 May 2008 18:34 Hi, I also get an orange underline at the 3rd line of my if loop: else c <= x <= b; that "<= produces a value that appears to be unused." Thanks Bookie Bookie wrote: >Hello, > > Does anyone know what the problem might be in a code I've written to >describe an irregular-shaped mesh grid boundary. I have a region of points >to plot against a function z. The "shadow" cast by this region on the x-y >plane of my surface plot, is a region bound above by the bottom right >quadrant of a circle, and below by the top right quadrant of a circle, and on >the left by a vertical line and on the right by a line-y=mx+b. I've compiled >my program but it is generating a 4 sided shape that doesn't have any curves >at the top or bottom. I've plotted my circles and lines separately to make >sure that my equations are accurate. If anyone has any ideas on what's wrong >with my code that would be a great help! > >Matlab code: > >clear all >clc >% Mesh points >m = 21; >n = 21; >T = zeros(5,n,m); >[ix,iy] = meshgrid(1:m,1:n); >minX = a; >maxX = b; >midX = c; >% y-values change over the range of x >%Equation of first lower boundary ranging from x = a to c: >% y = sqrt(r1^2-(x-X1)^2)-Y1 (top right quadrant) >%Equation of 2nd lower boundary ranging from x = c to b: >% y = m*x+b >%Equation of upper boundary ranging from x = a to b: >% y = (-1*sqrt(r2^2-(x-X2)^2)+Y2 (bottom right quadrant) >% X1,Y1 and X2,Y2 are centre coordinates of circles (constants) >% r1,r2 are circle radii (constants) >x = a + (ix-1)*(b-a)/(m-1); % a <= x <= b >if a <= x < c; > y = (sqrt(r1^2-(x-X1).^2)-Y1) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)- >(sqrt(r1^2-(x-X1).^2)-Y1))/(n-1); % circle1 <= y <= circle2 > else c <= x <= b; > y = (m.*x+b) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)-(m.*x+b))/(n-1); >% m*x+b <= y <= circle2 >end >for i = 1:n > for j = 1:m > [T(:,i,j)] = Function(x(i,j),y(i,j)); > end >end >figure >Z = squeeze(T(1,:,:)); >surf(x,y,real(Z)) >axis tight %want x & y axes tight so curves in x-y plane are not skewed >colormap hsv >colorbar > >Thanks for your help! > >Bookie -- Message posted via http://www.mathkb.com
From: Walter Roberson on 6 May 2008 18:51 In article <83c42f77f35ee(a)uwe>, Bookie via MathKB.com <u43093(a)uwe> wrote: >if a <= x < c; if (a <= x) & (x < c) > y = (sqrt(r1^2-(x-X1).^2)-Y1) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)- >(sqrt(r1^2-(x-X1).^2)-Y1))/(n-1); % circle1 <= y <= circle2 > else c <= x <= b; Did you mean, elseif (c <= x & x <= b) Or is c <= x <= b; just a comment, or are you attempting to execute ((c <= x) <= b) and then throw away the result ?? > y = (m.*x+b) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)-(m.*x+b))/(n-1); >% m*x+b <= y <= circle2 >end -- amazon.com's top 8 books about "walter" are Kotzwinkle/ Gundy/ Colman's "Walter the Farting Dog"
From: Bookie via MathKB.com on 6 May 2008 19:30 Hi, Yes, I want to say when x is equal to and greater than 'a' but less than 'c' then y is calculated in a certain way. So yes, if (a <= x) & (x < c); describes what I mean as well. I guess this is different from if a <= x < c;? Then I say when x is equal to and greater than 'c' but less than and equal to 'b' then y is calculated accordingly. "elseif (c <= x & x <= b)" is what I mean then. I don't want this is as a comment but actual code to help me generate the remaining of the results for y over the range of x values between c and b. As x values range from 'a' to 'b,' with 'c' being some midpoint, I want my y values to be calculated accordingly. So I want to generate and keep my corresponding y values that are generated over the range of x values between 'a' and 'b. I have a comment at the end of each of my y equations only describing that y is calculated between functions i.e. "% circle1 <= y <= circle2" I hope that answers what you've asked. Thanks Walter! Bookie Walter Roberson wrote: >>if a <= x < c; > >if (a <= x) & (x < c) > >> y = (sqrt(r1^2-(x-X1).^2)-Y1) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)- >>(sqrt(r1^2-(x-X1).^2)-Y1))/(n-1); % circle1 <= y <= circle2 >> else c <= x <= b; > >Did you mean, > >elseif (c <= x & x <= b) > >Or is c <= x <= b; just a comment, or are you attempting to >execute ((c <= x) <= b) and then throw away the result ?? > >> y = (m.*x+b) + (iy-1).*((-1*sqrt(r2^2-(x-X2).^2)+Y2)-(m.*x+b))/(n-1); >>% m*x+b <= y <= circle2 >>end -- Message posted via http://www.mathkb.com
From: Walter Roberson on 6 May 2008 19:54
In article <fvqndn$t73$1(a)canopus.cc.umanitoba.ca>, Walter Roberson <roberson(a)ibd.nrc-cnrc.gc.ca> wrote: >In article <83c42f77f35ee(a)uwe>, Bookie via MathKB.com <u43093(a)uwe> wrote: > >>if a <= x < c; >if (a <= x) & (x < c) Ah, I just realized that I wrote that assuming that x was a vector, but that if x -is- a vector, then you are going to hit a behaviour that you should know about. If a, c, and x are all single numbers, none of them vectors, then if a <= x && x < c is more efficient than using & instead of && . If x is a vector (more than one element), then the expression (a <= x & x < c) will produce a vector of logical results. The question then would what you would want to do if some of the results were true and some were false. The default in an 'if' statement is to only process the 'if' statement if *all* of the values are true. -- "Every intellectual product must be judged from the point of view of the age and the people in which it was produced." -- Walter Horatio Pater |