Convert cartesian coordinates to polar coordinates

8 ビュー (過去 30 日間)
N/A
N/A 2020 年 11 月 24 日
コメント済み: N/A 2020 年 11 月 24 日
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 件のコメント
KSSV
KSSV 2020 年 11 月 24 日
Okay...what is the question?
N/A
N/A 2020 年 11 月 24 日
Please scroll down to the colored text (at the bottom). I included a picture of the question.

サインインしてコメントする。

採用された回答

Stephan
Stephan 2020 年 11 月 24 日
編集済み: Stephan 2020 年 11 月 24 日
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 件のコメント
Stephan
Stephan 2020 年 11 月 24 日
編集済み: Stephan 2020 年 11 月 24 日
Save the filein your working directory and close it. Then open another script or use the command line with the call of the function like you already did correctly:
[r,theta]=cart2polar(2,2)
The idea behind a function is, that once it is written in can always be called from command line, another function or a script just by using the correct input arguments and in your case 2 output arguments that it returns when it was called correctly.
N/A
N/A 2020 年 11 月 24 日
I got it to work. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by