Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Converting polar and Cartesian vector representations using functions and struct

ID: 3604727 • Letter: C

Question

Converting polar and Cartesian vector representations using functions and structures. A vector is a mathematical quantity that has both magnitude and direction. A 2-dimensional vector can be represented as a displacement along xx and yy axes in rectangular (Cartesian) coordinates or, by a distance rrand an angle in polar coordinates,

The relationships between the Cartesian (x,y)(x,y) and polar (r,)(r,) coordinates are given by the following equations,

xyr=rcos()=rsin()=x2+y2x=rcos()y=rsin()r=x2+y2

Write a MATLAB function getPolar(inputCartesianStruct) that takes a structure inputCartesianStructas input, that has two components x and y. Then on output, it returns another structure that has to components r and phi. Write another function getCart(inputPolarStruct) that does the inverse of getPolar(inputCartesianStruct). Make sure that both functions are robust to wrong input structures by checking whether the input structures have the required fields: x and y, or r and phi. You can do so by using MATLAB’s builtin function isfield().

3. Converting polar and Cartesian vector representations using functions and structures. A vector is a mathematical quantity that has both magnitude and direction. A 2-dimensional vector can be represented as a displacement along x and y axes in rectangular (Cartesian) coordinates or, by a distance r and an angle in polar coordinates, r sin r cosp The conversion rule between the Cartesian and Polar representations of a vector. The relationships between the Cartesian (x,y) and polar (r,) coordinates are given by the following equations, x = r cos(d) y-r sin() Write a MATLAB function getPolar(inputcartesianstruct) that takes a structure inputcartesianstruct as input, that has two components x and y. Then on output, it returns another structure that has to components r and phi. Write another function getcart (inputPolarstruct) that does the inverse of getPolar(inputcartesianstruct). Make sure that both functions are robust to wrong input structures by checking whether the input structures have the required fields: x and y, or r and phi. You can do so by using MATLAB's builtin function isfield).

Explanation / Answer

%get polar
% asking the user to give the x and y values for the structure
x = input('Enter an x value: ');
y = input('Enter an y value: ');
% making sure the entries have the required field then finding x and y
disp(getPolar(struct('x',x,'y',y)));
function s = getPolar(inputCartesianStruct)
if ~isfield(inputCartesianStruct,'x')||~isfield(inputCartesianStruct,'y')
    return
end
x = inputCartesianStruct.x;
y = inputCartesianStruct.y;
r = sqrt(x^2+y^2);
phi = atan(y/x);
s = struct('r',r,'phi',phi);
end

%get cartesian
% asking the user to give the r and phi values for the structure
r = input('Enter a value for r:');
phi = input('Enter a value for phi:');
% making sure the entries have the required field then finding r and phi
disp(getCartesian(struct('r',r,'phi',phi)));
function f = getCartesian(inputPolarStruct)
if ~isfield(inputPolarStruct,'r')||~isfield(inputPolarStruct,'phi')
    return
end
r = inputPolarStruct.r;
phi = inputPolarStruct.phi;
x = r*cos(phi);
y = r*sin(phi);
f = struct('x',x,'y',y);
end