|
From: Roger Stafford on 6 May 2008 20:13 "Bookie via MathKB.com" <u43093(a)uwe> wrote in message <83c42f77f35ee(a)uwe>... > 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 --------- In addition to Walter's two criticism's I have one more, and it is a serious one. You have used the 'if' function improperly here. When you put an array of logical values after the 'if', rather than a single scalar quantity, as you have done above, it interprets them as if you had placed 'all' around them. What else could it do? It is limited to going the one way or the other and not some quantum-computer combination of both. In other words, if you have: x = [1 2 3 4 5]; if x <= 3 y = x; else y = -x; end then you will get y = [-1 -2 -3 -4 -5], not the [1 2 3 -4 -5]] you are apparently expecting, because the expression x<=3 is not true in all cases. The execution of the 'if' here is to either execute y = x; or else y = -x; in their respective entireties, and not some of one and some of the other. To make this go the way I think you want, it would have to be written in this fashion: x = [1 2 3 4 5]; y = (x<=3).*x + (x>3)*7; This is the equivalent of saying y = [1 1 1 0 0].*x + [0 0 0 1 1]*7; Roger Stafford
From: Roger Stafford on 6 May 2008 20:27 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <fvqs6g$ll9$1(a)fred.mathworks.com>... > ....... > To make this go the way I think you want, it would have to be written in this > fashion: > > x = [1 2 3 4 5]; > y = (x<=3).*x + (x>3)*7; > > This is the equivalent of saying > > y = [1 1 1 0 0].*x + [0 0 0 1 1]*7; > > Roger Stafford --------- Oops, I meant to write: x = [1 2 3 4 5]; y = (x<=3).*x + (x>3)*(-x); This is the equivalent of saying y = [1 1 1 0 0].*x + [0 0 0 1 1]*(-x); in the last part. Roger Stafford
From: Steven Lord on 6 May 2008 23:00 "Bookie via MathKB.com" <u43093(a)uwe> wrote in message news:83c4b714bfcb0(a)uwe... > 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;? Yes, (a <= x) & (x < c) is different from (a <= x < c). In the latter case, the expression is evaluated left-to-right, so (a <= x < c) is equivalent to: (a <= x) < c I'm going to assume, for sake of argument, that all of a, x, and c are scalars. [This makes it easier to discuss; it's similar if one or more are nonscalar.] Now (a <= x) will return either logical 0 or logical 1. If c is less than 0, the result of (a <= x) < c will always be 0. If c is greater than 1, the result will always be 1. If c is between 0 and 1, the result of (a <= x) < c will be the same as (a > x). [If (a<=x) is true or logical 1, then 1 < c is false or logical 0, and vice versa.] -- Steve Lord slord(a)mathworks.com
From: Bookie via MathKB.com on 7 May 2008 15:15 Thanks Roger, Walter & Steven. I see what you mean. So I've now fixed how I'm using my relational operators but I'm still not getting x and y inputs for my function. I've let y = ya + yb, the sum of the y values for each range of x. ya exists for x between a and c and yb exists for x between c and b. When one y component does not exist I assume it will return zero for that component, i.e. for x = b, y = yb. But I've compiled this and I still don't get my output. This is what I have: Roger Stafford wrote: >"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in >message <fvqt0n$h7v$1(a)fred.mathworks.com>... >> Oops, I meant to write: >> >[quoted text clipped - 8 lines] >> >> Roger Stafford >-------- > Oops number two! I left out the dots. It should read: > > x = [1 2 3 4 5]; > y = (x<=3).*x + (x>3).*(-x); > >This is the equivalent of saying > > y = [1 1 1 0 0].*x + [0 0 0 1 1].*(-x); > > My apologies. > >Roger Stafford -- Message posted via MathKB.com http://www.mathkb.com/Uwe/Forums.aspx/matlab/200805/1
From: Bookie via MathKB.com on 7 May 2008 15:18 Sorry here's what I have now that's still not compiling: % Left out comments and variable declarations, see earlier posting of code x = a + (ix-1)*(b-a)/(m-1); % a <= x <= b midXpt1 = c; % circle1 <= ya <= circle2 ya = (sqrt(r1^2-(((x>=a)&(x<c))-X1)^2)-Y1) + (iy-1).*(-1*sqrt(r2^2-((((x>=a)& (x<c))-X2)^2)+Y2)-(sqrt(r1^2-(((x>=a)&(x<c))-X1)^2)-Y1))/(n-1); % m*x+b <= yb <= circle2 yb = (m*((x>=c)&(x<=b))+b) + (iy-1).*((-1*sqrt(r2^2-(((x>=c)&(x<=b))-X2)^2) +Y2)-(m*((x>=c)&(x<=b))+b))/(n-1); y = ya + yb; 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 Bookie wrote: >Thanks Roger, Walter & Steven. I see what you mean. So I've now fixed how >I'm using my relational operators but I'm still not getting x and y inputs >for my function. I've let y = ya + yb, the sum of the y values for each range >of x. ya exists for x between a and c and yb exists for x between c and b. >When one y component does not exist I assume it will return zero for that >component, i.e. for x = b, y = yb. But I've compiled this and I still don't >get my output. > >This is what I have: > >>"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in >>message <fvqt0n$h7v$1(a)fred.mathworks.com>... >[quoted text clipped - 16 lines] >> >>Roger Stafford -- Message posted via MathKB.com http://www.mathkb.com/Uwe/Forums.aspx/matlab/200805/1
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: ?User Friendliness of free mathware? Next: unix('command') |