Spherical Triangle - Fundamental Programming Concepts - Solved Homework, Exercises of Computer Programming

This course provides an introduction to programming and problem solving using a high-level programming language. We use matlab to practice our concepts in coding. Some important keywords in this homework are: Spherical Triangle, Area and Excess, Special Triangle, Testing Our Code, Distance, Triangular Area, Distance Converter, Setun Returns, Soccer Ball Toss

Typology: Exercises

2012/2013

Uploaded on 08/17/2013

zaid
zaid 🇮🇳

4.5

(2)

59 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 Spherical Triangle
1.a Area and Excess
In this part all you have to do is to transfer the given mathematical formulas
into assignments using arithmetic operators in MATLAB. When you run the
script file, test triangle.m, it will ask you to enter the values for three angles
and the radius, then it will compute the area and excess using your formulas,
and display the results.
function [Area, E] = spherical triangle(A,B,C,R)
% Returns the area of a spherical triangle with
% spherical angles A,B,C
E =A+B+Cpi;
Area = Rˆ2 *E;
1.b Special Triangle
When the corner Cis at North pole, and A&Bare on the equator; the
longtitudes of Aand B,LA &LB, can be used to compute the value of the
angle at C. The convention we followed was to consider the angle seen by the
arc starting from B, going in the west direction to reach A. Depending on the
values of LA and LB we have to branch our program.
LA = input('Enter the longtitude value for A in degrees: ');
LB = input('Enter the longtitude value for B in degrees: ');
if LA <LB
C = (LB LA)*pi/180;
else
C = (360 + LB LA)*pi/180;
end
A = pi/2; % longtitudes are perpendicular to the equator
B = pi/2;
R = input('Enter the value for the radius: ');
[Area, E] = spherical triangle(A,B,C,R);
disp('The area of the special triangle is '); disp(Area);
disp('The excess value is '); disp(E);
1
docsity.com
pf3
pf4

Partial preview of the text

Download Spherical Triangle - Fundamental Programming Concepts - Solved Homework and more Exercises Computer Programming in PDF only on Docsity!

1 Spherical Triangle

1.a Area and Excess

In this part all you have to do is to transfer the given mathematical formulas

into assignments using arithmetic operators in MATLAB. When you run the

script file, test triangle.m, it will ask you to enter the values for three angles

and the radius, then it will compute the area and excess using your formulas,

and display the results.

function [Area, E] = spherical triangle(A,B,C,R) % Returns the area of a spherical triangle with % spherical angles A,B,C

E = A + B + C − pi; Area = Rˆ2 (^) * E;

1.b Special Triangle

When the corner C is at North pole, and A & B are on the equator; the

longtitudes of A and B, LA & LB, can be used to compute the value of the

angle at C. The convention we followed was to consider the angle seen by the

arc starting from B, going in the west direction to reach A. Depending on the

values of LA and LB we have to branch our program.

LA = input('Enter the longtitude value for A in degrees: '); LB = input('Enter the longtitude value for B in degrees: ');

if LA < LB C = (LB − LA)pi/180; else C = (360 + LB − LA)pi/180; end

A = pi/2; % longtitudes are perpendicular to the equator B = pi/2;

R = input('Enter the value for the radius: ');

[Area, E] = spherical triangle(A,B,C,R);

disp('The area of the special triangle is '); disp(Area); disp('The excess value is '); disp(E);

1.c Testing Our Code

We can test our code using some special triangles we can recognize, like a hemi-

sphere. You could have a script asking for user input or a fixture of test cases.

The file special triangle.m already contains the hemisphere as a test case in a

separate code section. In order to make use of the function spherical triangle

you could write a script asking for values from the user:

A = input('Enter the angle A: '); B = input('Enter the angle B: '); C = input('Enter the angle C: '); R = input('Enter the radius R: ');

[Area, E] = spherical triangle(A,B,C,R);

fprintf('The area of the triangle is %f\n', Area); fprintf('The spherical excess is %f\n', E);

2 Distance

2.a Two Points

function dAB = distance(xA,xB,yA,yB,zA,zB) % Returns the distance between points A and B which are specified % using their cartesian coordinates in 3dimensions.

dAB = sqrt((xA−xB)ˆ2+(yA−yB)ˆ2+(zA−zB)ˆ2);

2.b Triangular Area

function [Area,dAB,dBC,dCA] = triangle area(xA,xB,xC,yA,yB,yC,zA,zB,zC) % Returns the area and side lengths of a triangle. The input consists % of the cartesian coordinates of vertices in 3−dimensions.

dAB = distance(xA,xB,yA,yB,zA,zB); dBC = distance(xB,xC,yB,yC,zB,zC); dCA = distance(xC,xA,yC,yA,zC,zA);

s = (dAB + dBC + dCA) / 2; % semi−perimeter

Area = sqrt(s(s−dAB)(s−dBC)*(s−dCA)); % dont forget (^) *

5 Soccer Ball Toss

5.a Coin Toss

We can simulate a coin toss by generating a random number using rand function

and then comparing it with 0.5. If heads is represented by the first half of the

unit interval, the following function satisfy the requirements:

function x = coin toss() x = rand > 0.5;

5.b Ball Toss

The probabilities of black and white are proportional to the areas covered by

pentagons and hexagons, respectively, on the spherical surface. The computation

of the exact ratio is a little complicated, but you can use an approximation by

considering a truncated icosahedron.

Let the side length of the hexagons and pentagons be a. Then the total area

covered by the white regions can be computed as follows:

AW = 20 ×

3 a^2

Similarly, the area covered by the pentagons is:

AB = 12 ×

a^2

We can compute the probabilities for black and white as:

PB =

AB

AB + AW

PW =

AW

AB + AW

We can modify the coin toss function to mimic a ball toss as:

function x = ball toss() x = rand > 0.2843;

A more accurate result can be computed using the spherical triangle area equa-

tion and spherical trigonometric identities, which gives the critical value around:

PB ≈ 0 .28167, PW ≈ 0 .71833.