please help with this function

조회 수: 5 (최근 30일)
francis
francis 2013년 7월 23일
function [th rad]=cartopolar25(x,y
t=atan(y/x); rad=sqrt(x^2+y^2); if if x<0 if y<0 th=180+t return else th=180-t return end else if y<0 th=-t return else th=t return end end
end end
Hi I need help with this code, if you guys see any mistakes please let me know. I have to make a function to determine the polar coordinates from the Cartesian plane, thanks for the help yes I was able to get the degrees now all I need is the radians any tips with this current format thanks

채택된 답변

Narges M
Narges M 2013년 7월 23일
your function is this:
function [th rad]=cartopolar25(x,y)
th = atan(y/x);
rad = sqrt(x^2+y^2);
if x<0
if y<0
th=th+180;
else
th=th-180;
end
else
if y<0
th=-th;
end
end
and you need to call it like this:
[t,r] = cartopolar25(-11,20);
the output will be:
t =
-181.0680
r =
22.8254
  댓글 수: 1
francis
francis 2013년 7월 24일
yes that was it I got it now

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jonathan Sullivan
Jonathan Sullivan 2013년 7월 23일
help atan2
doc atan2

Gorji
Gorji 2013년 7월 23일
What is Y and X? Are they matrix? I think the structure of 'if' is incorrect you must use if & elseif & else
  댓글 수: 2
francis
francis 2013년 7월 23일
Y and X are supposed to be the coordinates, if I used that function with the points (-11,20) it should be able to give me radians=2.8 and deg=-118.8, I can do all of that on paper but here I am just trying to figure out I dont know how to defined Y and X, I try by using a vector and still gives me the same error "Undefined function or variable "y".
francis
francis 2013년 7월 23일
I was able to get the degrees with a few changes yess, all I need some tips of putting the radiants on to this format, thanks

댓글을 달려면 로그인하십시오.


Andrew Reibold
Andrew Reibold 2013년 7월 23일
atan doesn't take the 'y over x' value you seem to be thinking of. It takes an angle in radians....
Y = atan(X) returns the inverse tangent (arctangent) for each element of X. For real elements of X, atan(X) is in the range [–π/2, π/2].
The atan function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians.
Example Graph the inverse tangent function over the domain –20 ≤ x ≤ 20. x = -20:0.01:20; plot(x,atan(x)), grid on

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by