Matlab. Remaining Time: 1 hour, 13 minutes, 56 seconds. Question Completion Stat
ID: 3920362 • Letter: M
Question
Matlab.
Remaining Time: 1 hour, 13 minutes, 56 seconds. Question Completion Status: QUESTION 4 Create a user defined function that will convert a coordinate (x.y) in the Cartesian Coordinate System to a Polar coordinate (, thela) where theta is in radians, referenced positive counter clockwise from the x-axis, Use the function delfintionline function Ir th) a cart2polar(x,y) in your solution. Keep in mind that there are four quadrants in the Cartesian system and the traditional sine and cosine functions only provide the frst quadrant value. Assume only scalar values will be sent to the function. y- axis x-axis 0-0 Save All Ans Click Save and Submit to save and submit. Click Save All Answers to save all ansuersExplanation / Answer
SOLUTION:
According to the given data the following code follows;
Function:
function [ r , th] =cart2polar( x,y)
r=sqrt(x^2+y^2);
th=atan(y/x)*180/pi;
if (x<0 && y>0);
th= 180+th;
elseif (x<0 && y<0)
th=180+th;
elseif (x>0 && y<0)
th=360+th;
else
th=th;
end
end
main programm:
clc;
clear all;
close all;
[r,th]=cart2polar(1,1);
fprintf('cart2polar(1,1) is [r,th]=[%f, %f ] ',r,th);
[r,th]=cart2polar(-1,1);
fprintf('cart2polar(-1,1) is [r,th]=[%f, %f ] ',r,th);
[r,th]=cart2polar(-1,-1);
fprintf('cart2polar(-1,-1) is [r,th]=[%f, %f ] ',r,th);
[r,th]=cart2polar(1,-1);
fprintf('cart2polar(1,-1) is [r,th]=[%f, %f ] ',r,th);
OUTPUT :
cart2polar(1,1) is [r,th]=[1.414214, 45.000000 ]
cart2polar(-1,1) is [r,th]=[1.414214, 135.000000 ]
cart2polar(-1,-1) is [r,th]=[1.414214, 225.000000 ]
cart2polar(1,-1) is [r,th]=[1.414214, 315.000000 ]