Two distances are required to specify the location of a point relative to an origin in two-dimensional space (Fig. P3.6):
• The horizontal and vertical distances (x, y) in Cartesian coordinates.
• The radius and angle (r, θ) in polar coordinates.
It is relatively straightforward to compute Cartesian coordinates (x, y) on the basis of polar coordinates (r, θ). The reverse process is not so simple. The radius can be computed by the following formula:
If the coordinates lie within the first and fourth coordinates (i.e., x > 0), then a simple formula can be used to compute θ:
FIGURE P3.6
x | y | θ |
<0 | >0 | tan‒1(y/x) + π |
<0 | <0 | tan‒1(y/x) – π |
<0 | =0 | π |
=0 | >0 | π/2 |
=0 | <0 | –π/2 |
=0 | =0 | 0 |
Write a well-structured M-file using if...elseif
structures to calculate r and π as a function of x and y. Express the final results for π in degrees. Test your program by evaluating the following cases:
x | y | r | θ |
2 | 0 |
|
|
2 | 1 |
|
|
0 | 3 |
|
|
–3 | 1 |
|
|
–2 | 0 |
|
|
– 1 | –2 |
|
|
0 | 0 |
|
|
0 | –2 |
|
|
2 | 2 |
|
|
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.