Convert cartesian coordinates to polar coordinates

221 views (last 30 days)
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:
  2 Comments
N/A
N/A on 24 Nov 2020
Please scroll down to the colored text (at the bottom). I included a picture of the question.

Sign in to comment.

Accepted Answer

Stephan
Stephan on 24 Nov 2020
Edited: Stephan on 24 Nov 2020
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
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!