Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
4th Edition
ISBN: 9780128045251
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 6, Problem 15E
To determine

To write:

A program to convert the Cartesian coordinates to spherical coordinates, and print the results.

Expert Solution & Answer
Check Mark

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 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.

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,

(x,y,z)=(1,2,3)

The spherical coordinates are (r,θ,ϕ).

The formulas to convert the Cartesian coordinates to spherical coordinates are as follows.

r=x2+y2+z2θ=cos1(zr)ϕ=tan1(yx)

Substitute 1 for x, 2 for y and 3 for z in the all formulas.

r=(1)2+(2)2+(3)2r=14r=3.7417

The inclination angle is,

θ=cos1(33.7417)θ=36.699°θ=0.6405rad

The azimuth angle is,

ϕ=tan1(21)ϕ=63.4349°ϕ=1.1071rad

The spherical coordinates are (3.7417,0.6405,1.1071).

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,

Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving, Chapter 6, Problem 15E

Therefore, the result is stated above.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Knowledge Booster
Background pattern image
Statistics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, statistics and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage
Text book image
Mathematics For Machine Technology
Advanced Math
ISBN:9781337798310
Author:Peterson, John.
Publisher:Cengage Learning,
Text book image
Elementary Geometry For College Students, 7e
Geometry
ISBN:9781337614085
Author:Alexander, Daniel C.; Koeberlein, Geralyn M.
Publisher:Cengage,
Text book image
Trigonometry (MindTap Course List)
Trigonometry
ISBN:9781337278461
Author:Ron Larson
Publisher:Cengage Learning
Area Between The Curve Problem No 1 - Applications Of Definite Integration - Diploma Maths II; Author: Ekeeda;https://www.youtube.com/watch?v=q3ZU0GnGaxA;License: Standard YouTube License, CC-BY