Functions: Dealing with a triangle | Matlab Examples

Matlab Tutorials | Examples

Practice 8:

Functions: Dealing with a triangle

The side lengths of a triangle always satisfy the following conditions:

a < b+c b < a+c c < a+b

Function get_abc

Write a function named get_abc that gets three positive integers inputs (a, b, c) from the user. If either value is negative or zero, the function should immediately ask for that input again. After all three inputs are given, using the above equations the function should check whether the given values could constitute a triangle. If not, an error message should be displayed, and all three inputs should be given again. The function gets nothing from the caller, and returns the three sides of the triangle.

Function my_fact

Write a function named my_fact that gets a positive integer from the calling program, and evaluates and returns the factorial of that number using loops.

Function my_sin

Write another function named my_sin that finds the sine of an angle using Taylor series expansion. The function should receive one angle in degrees, and return the sine of that angle. The function should use my_fact function to evaluate factorials. It should stop evaluating new terms of the series when the absolute value of the newly evaluated term is less than value s (sensitivity). Variable s should be a global variable set by the main program. The Taylor series expansion of sin(x) is:

Function my_arcsin

Write a function that will get a single value between -1 and 1 from the calling program, and find the arcsine of the given number using an interval halving algorithm. This function should use my_sin function, and search for the angle between limits of 90° and 270°. It should stop halving the interval and return the angle when either an exact match is found, or the left and right limits of the interval are less than 100s away (s is the global sensitivity variable defined in the main program). The function should return the result (in degrees) to the calling program.

Function find_angle

Write another function that finds an angle of the triangle by using the cosine rule.

Note how the three formulas are similar! Thus it is possible to write a single generic function and call it three times, each time changing the places of a, b, and c in the argument list.

Using the formula derived on the right, the function can easily find cos(θX), which will yield a number between -1 and 1. In order to find the angle θX, you will use the my_arcsin function with a little trick.

We know that
cos(x) = sin(x+90°)

Taking arcsine of both sides, and then simplifying, arcsin(cos(x)) = arcsin(sin(x+90°)) arcsin(cos(x)) = x+90°
x = arcsin(cos(x)) – 90°

So if you take the arcsine of this value and subtract 90, you can easily find the angle θX. Return the result to the calling program.

Function find_area

Write a function that calculates the area of a triangle. The function should take three arguments as inputs; two sides and the angle between them; it should return the evaluated area as a result. Use my_sin function.

Main program

In the main program, start by calling get_abc. After that, ask the user to provide the sensitivity value s, and put it into a global variable. Then call find_angle three times to find θA, θB, θC, and in the main program print those angles on the screen with two decimal places. Finally, call find_area, and print the value returned from this function with two decimal places.

 

Solutions:

find_angle.m

find_area.m

get_abc.m

main.m

my_arcsin.m

my_fact.m

my_sin.m

 

2 thoughts on “Functions: Dealing with a triangle | Matlab Examples

  1. Please let me know if you’re looking for a author for your weblog. You have some really great posts and I believe I would be a good asset. If you ever want to take some of the load off, I’d really like to write some material for your blog in exchange for a link back to mine. Please shoot me an email if interested. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *