In 3D space, the Cartesian coordinates (x,y,z) can be converted to spherical coordinates (radius r, inclination
, azimuth
) by the following equations:

A program is being written to read in Cartesian coordinates, convert to
spherical, and print the results. So far, a script pracscript has been
written that calls a function getcartesian to read in the Cartesian
coordinates, and a function printspherical that prints the spherical
coordinates. Assume that the getcartesian function exists and reads
the Cartesian coordinates from a file. The function printspherical calls
a subfunction convert2spher that converts from Cartesian to spherical
coordinates. You are to write the printspherical function. Here is an
example:
>> pracscript
The radius is 5.46
The inclination angle is 1.16
The azimuth angle is 1.07
pracscript.m
[x,y,z] = getcartesian();
printspherical(x,y,z)
getcartesian.m
function [x,y,z] = getcartesian()
% Assume this gets x,y,z from a file
% Function stub:
x = 11;
y = 3;
z = 7;
end
printspherical.m
function printspherical(x,y,z)
[r, inc, azi] = convert2spher(x,y,z);
fprintf('The radius is %.2f\n', r)
fprintf('The inclination angle is %.2f\n', inc)
fprintf('The azimuth angle is %.2f\n', azi)
end
function [r, i, a] = convert2spher(x,y,z)
r = sqrt(x^2 + y^2 + z^2);
i = acos(z/r);
a = atan(y/z);
end
need an explanation for this answer? contact us directly to get an explanation for this answer