Concept explainers
To write:
A program to convert the Cartesian coordinates to spherical coordinates, and print the results.
Answer to Problem 15E
Solution:
The script file is,
% MATLAB code to print the result in spherical coordinates.
%script file.
[x, y, z] = getcartesian();
%get the value of cartesian coordinates by calling the
%getcartesian.
printspherical(x, y, z)
%print the value in spherical coordinates by calling the function
%printspherical.
% end of script
%The script file should be placed in the same folder.
The function file is,
% MATLAB code to get the result in cartesian coordinates.
%Function file.
function [x, y, z] = getcartesian()
%get the cartesian coordinates by using the function getcartesian.
x = 1;
%define the variable x.
y = 2;
%define the variable y.
z = 3;
%define the variable z.
end
% end of function
%The function file should be placed in the same folder.
The function file is,
% MATLAB code to print the result in spherical coordinates.
%Function file.
function printspherical(x, y, z)
%the spherical coordinates will be printed by using the function
%printspherical.
[rad, incl, azi] = convert2spher(x, y, z);
%convert the spherical coordinates to cartesian coordionates.
fprintf('the radius is %.2f\n', rad);
%print the radius.
fprintf('the inclination angle is %.2f\n', incl);
%print the inclination angle.
fprintf('the azimuth angle is %.4f\n', azi);
%print the azimuth.
end
% end of function
function [ra, in, a] = convert2spher(x, y, z)
%call a subfunction convert2spher to transform the cartesian coordinates
%into spherical coordinates.
ra = sqrt(x^2+y^2+z^2);
%define the radius.
in = acos(z/ra);
%define the inclination angle.
a = atan(y/z);
%define the azimuth angle.
end
%The function file should be placed in the same folder.
Explanation of Solution
Consider the Cartesian coordinates are,
The spherical coordinates are
The formulas to convert the Cartesian coordinates to spherical coordinates are as follows.
Substitute 1 for
The inclination angle is,
The azimuth angle is,
The spherical coordinates are
MATLAB Code:
% MATLAB code to print the result in spherical coordinates.
%script file.
[x, y, z] = getcartesian();
%get the value of cartesian coordinates by calling the function
%getcartesian.
printspherical(x, y, z)
%print the value in spherical coordinates by calling the function
%printspherical.
% end of script
%The script file should be placed in the same folder.
% MATLAB code to get the result in cartesian coordinates.
%Function file.
function [x, y, z] = getcartesian()
%get the cartesian coordinates by using the function getcartesian.
x = 1;
%define the variable x.
y = 2;
%define the variable y.
z = 3;
%define the variable z.
end
% end of function
%The function file should be placed in the same folder.
% MATLAB code to print the result in spherical coordinates.
%Function file.
function printspherical(x, y, z)
%the spherical coordinates will be printed by using the function
%printspherical.
[rad, incl, azi] = convert2spher(x, y, z);
%convert the spherical coordinates to cartesian coordionates.
fprintf('the radius is %.2f\n', rad);
%print the radius.
fprintf('the inclination angle is %.2f\n', incl);
%print the inclination angle.
fprintf('the azimuth angle is %.4f\n', azi);
%print the azimuth.
end
% end of function
function [ra, in, a] = convert2spher(x, y, z)
%call a subfunction convert2spher to transform the cartesian coordinates
%into spherical coordinates.
ra = sqrt(x^2+y^2+z^2);
%define the radius.
in = acos(z/ra);
%define the inclination angle.
a = atan(y/z);
%define the azimuth angle.
end
%The function file should be placed in the same folder.
Save the MATLAB script with name, paracscript.m and function files with names printspherical.m and getcartesian.m in the current folder. Execute the program by typing the script name at the command window to generate result.
Result:
The result is,
Therefore, the result is stated above.
Want to see more full solutions like this?
Chapter 6 Solutions
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:CengageMathematics For Machine TechnologyAdvanced MathISBN:9781337798310Author:Peterson, John.Publisher:Cengage Learning,Elementary Geometry For College Students, 7eGeometryISBN:9781337614085Author:Alexander, Daniel C.; Koeberlein, Geralyn M.Publisher:Cengage,
- Trigonometry (MindTap Course List)TrigonometryISBN:9781337278461Author:Ron LarsonPublisher:Cengage Learning