MATLAB: Convert cartesian coordinates to polar coordinates

cartesian coordinatespolar coordinates

Please let me know how to fix my code.
Here is my code so far:
I created a file and named it Cart2polar.m
[r,theta]=cart2polar(x,y)
r=sqrt(x^2+y^2);
theta=atan(y/x);
I created a new live script, then ran it.
[r,theta]=cart2polar(2,2)
Here is the homework prompt:

Best Answer

Edit your file - you did not declare it as a function, so Matlab uses it as a script. Also think about to vectorize your code, to allow it to accept vector inputs:
function [r,theta]=cart2polar(x,y)
r=sqrt(x.^2+y.^2);
theta=atan(y./x);
end
Related Question