MATLAB Answers

Matlab Cart2pol plotting help

18 views (last 30 days)
Good Afternoon All,
I have been taking a perfect circle and applying displacement to the circle and then converting to polar coordinates using car2pol matlab function. I want to plot both the original circle and the new displaced circle on the same plot but when I attempt to plot the displaced circle I receive a polynomial rather than a closed circle.I know that car2pol produces the coordinates in radians and so I just do a simple conversion to degrees (180./pi) but still receieve a polynomial type function. Any suggestions? Thanks for the much needed help.
Here is the code for te new displaced circle:
%Calculating the radial component
for i=1:length(Xcoor)
new_r = [Xcoor(i)-Xcent, Ycoor(i)-Ycent];
[angle(i),rad(i)] = cart2pol(new_r(1),new_r(2));
delta_r(i)=cos(angle(i))*Xdisp(i) + sin(angle(i))*Ydisp(i);
end
%Generating the profile and sorting by order of increasing angle
prof = ([angle;r + delta_r])';
prof = sortrows(prof);
%Output results
angles = (prof(:,1))';
radius = (prof(:,2))';
%plotting
rconvert=180./pi;
plot(angles*rconvert,radius)
Mel
Example of data:
Xcoor=[ -33.5335 -32.043 -30.3087 -28.3437 -26.163 -23.7832 -18.5001 -15.6369 -12.6548 -9.57637 -6.42505 -3.22483 -0.00006 3.2247 6.42492 9.57625 12.6547 15.6368 18.5 21.2223 23.7831 26.1629 28.3436 30.3086 32.0429 33.5334 35.7392 36.4379 36.8592 37 36.8592 36.4379 35.7392 34.7686 33.5334 32.0429 30.3086 28.3436 26.1629 23.7831 21.2223 18.5 15.6369 12.6547 9.57628 6.42495 -0.00003 -3.2248 -6.42502 -9.57634 -12.6548 -15.6369 -18.5 -21.2224 -23.7832 -26.163 -28.3437 -30.3087 -32.043 -33.5334 -34.7687 -35.7393 -36.438 -36.8593 -37.0001 -21.2224 34.7686 3.22473 -36.8593 -36.438 -35.7393 -34.7687];
Ycoor=[240.137 243 245.722 248.283 250.663 252.844 256.543 258.033 259.269 260.239 260.938 261.359 261.5 261.359 260.938 260.239 259.269 258.033 256.543 254.809 252.844 250.663 248.283 245.722 243 240.137 234.076 230.925 227.725 224.5 221.275 218.075 214.924 211.845 208.863 206 203.278 200.717 198.337 196.156 194.191 192.457 190.967 189.731 188.761 188.062 187.5 187.641 188.062 188.761 189.731 190.967 192.457 194.191 196.156 198.337 200.717 203.278 206 208.863 211.845 214.924 218.075 221.275 224.5 254.809 237.155 187.641 227.725 230.925 234.076 237.155];
Xdisp=[0.0685 0.0662 0.0636 0.0606 0.0571 0.0532 0.0446 0.0404 0.0363 0.0327 0.0295 0.0268 0.0245 0.0223 0.0197 0.0166 0.0132 0.00937 0.00541 0.00147 -0.00221 -0.00532 -0.00755 -0.00855 -0.00838 -0.0074 -0.00534 -0.00471 -0.00437 -0.00438 -0.0047 -0.00531 -0.00619 -0.00728 -0.00845 -0.0093 -0.0092 -0.00781 -0.00523 -0.00181 0.00215 0.00629 0.0104 0.0143 0.0178 0.0208 0.0254 0.0275 0.03 0.0331 0.0366 0.0406 0.0448 0.0491 0.0533 0.0572 0.0606 0.0634 0.0658 0.0678 0.0696 0.0712 0.0725 0.0734 0.0739 0.0489 -0.00627 0.0233 0.0739 0.0733 0.0722 0.0705];
Ydisp=[ -0.0118 -0.0173 -0.0227 -0.0279 -0.0328 -0.0373 -0.0451 -0.0483 -0.0511 -0.0535 -0.0552 -0.0562 -0.0566 -0.0563 -0.0553 -0.0535 -0.0511 -0.0481 -0.0448 -0.0411 -0.0371 -0.0328 -0.028 -0.0228 -0.0172 -0.0112 0.000453 0.00581 0.011 0.016 0.0211 0.0263 0.0317 0.0375 0.0434 0.0493 0.0548 0.0597 0.0642 0.0682 0.0719 0.0751 0.078 0.0806 0.0827 0.0842 0.0854 0.0851 0.0843 0.0829 0.0809 0.0784 0.0754 0.0721 0.0683 0.0641 0.0593 0.0543 0.0489 0.0434 0.0379 0.0323 0.0267 0.0211 0.0156 -0.0414 -0.00521 0.0851 0.0101 0.0046 -0.000885 -0.00636];
Xcent=-4.7019e-004; Ycent=224.5;
r=37;
rconvert=180./pi;
  2 Comments
Melissa
Melissa on 12 Sep 2012
Sorry, that would have been helpful to have done from the beginning. I am getting ranges in degrees from -180 to 180 so I think shifting to 0 to 360 would seem like the correct way to go.

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 12 Sep 2012
Yup, this makes sense. angles is getting bigger so this along the x-axis will continue to move forward because it is being treated like an x-value and not a polar-angle.
Also, your loop can be easily vectorized:
new_r2 = [(Xcoor-Xcent)',(Ycoor-Ycent)'];
[angle2, rad2] = cart2pol(new_r2(:,1),new_r2(:,2));
delta_r2 = cos(angle2).*Xdisp' + sin(angle2).*Ydisp';
prof2 = sortrows([angle2,(r+delta_r2)],[1 2]);
Something like:
polar(prof2(:,1),prof2(:,2))
Is probably more what you're expecting..
  1 Comment
Melissa
Melissa on 12 Sep 2012
Thanks again Sean de Wolski. I didn't even know there was a plotting function polar, thats exactly what I was looking for.
You are the best!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!