Prev: system of partial differential and differential equations
Next: Is there a MATLAB function similiar to TableCurve or FindGraph?
From: Steven_Lord on 9 Aug 2010 12:54 "Shamma Raghib" <critico.nino(a)gmail.com> wrote in message news:i3p9vc$kj7$1(a)fred.mathworks.com... > "Andy " <myfakeemailaddress(a)gmail.com> wrote in message > <i3p8gg$g6r$1(a)fred.mathworks.com>... >> Well, since you haven't posted your distance function, the only error I'm >> getting is that the distance function is undefined. Could you post your >> error message, rather than forcing us to guess at it? > > > ERROR: > ??? Attempted to access RSX(2); index out of bounds because numel(RSX)=1. > Error in ==> polytry2 at 29 > d=distance(MSX,MSY,RSX(j),RSY(j)) > > Here is my distance function: > Like I said its plain and simple :) > %%%%%%%% > function d=distance(a, b, x, y) > d=sqrt((a-x).^2+(b-y).^2); > > end Looking at a piece of your original code: d_tot=0; for j=1:6 [RSX,RSY]=pol2cart(relay_theta(j),relay_r(j)); %transform RS position to the cartesian coordinate system hold on; plot(RSX,RSY,'b*'); end for j=1:6 d=distance(MSX,MSY,RSX(j),RSY(j)); end Since you overwrite RSX and RSY during each iteration of the code I quoted, when you reach your distance call RSX and RSY don't have 2nd elements -- they are 1-by-1 scalars. POL2CART is vectorized; it can accept a vector of theta and r values, so use that instead of the first loop I quoted. Then RSX and RSY will be vectors and your distance call will work. But actually, your distance function will work with vectors x and y (since you're using the elementwise .^ operator instead of the matrix ^ operator so you don't need _that_ loop either. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: Shamma Raghib on 9 Aug 2010 13:34 Hey thanks Andy, Jan, Steve, Yea you were right about the vector thing. I changed the second for loop code to: for j=1:6 d=distance(user_theta,user_r,relay_theta(j),relay_r(j)); end but now a new problem occurs I am assuming it is something similar....in the code: %%%%%line after the second distance for loop d_tot=d_tot+d(j)^-gamma; error: ??? Attempted to access d(6); index out of bounds because numel(d)=1. Error in ==> polytry2 at 32 d_tot=d_tot+d(j)^-gamma; Does it mean that since d_tot is scalar, the loop is not progressing? :S This is where the debugger stops. I just want a summation of all the 6 vector distances that I calculates in the loop. Is this the wrong approach? Thanks!
From: Shamma Raghib on 9 Aug 2010 13:44 I forgot to mention something. I think my last code (above messege of mine) was wrong since I wanted to calculate the distance in terms of polar coordinates. (user_r, user_theta ) are the coordinates for the random MS points (relay_r, relay_theta) are the coordinates for the fixed 6 points Now after the loop runs, I wanted to find the distance of one MS as it keeps on adding a point at each iteration. So at first I converted it to normal cartesian coordinate and then tried calculating the distance. but the first error occured. Then i tried changing the vector value as Steve suggested, but i realized that both the points are in polar coordinates so the distance function won't be logical. So I have to do it in terms of polar distance calculation...
From: Shamma Raghib on 9 Aug 2010 13:54
Hey guys! I solved it :) Thanks a lot. Now I understand the vector error issue. Thank Jan for the debug command. Thanks Andy for your help. Thanks Steve for pointing it out to me! Cheers! |