# 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**

1 2 |
function t = find_angle(a, b, c) t = my_arcsin(((((b ^ 2 )+ (c ^ 2) - (a ^ 2)) / 2) / b) / c) - 90; % calculate angle |

**find_area.m**

1 2 |
function Area = find_area(a, b, degree) Area = 0.5*a * b * my_sin(degree);%calculate area |

**get_abc.m**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function [a,b,c]=get_abc() condition = true; while condition a= input('Give the value of a:'); %take input for a while a<=0 || (a ~= ceil(a))%checking the condition positive integer or not disp('You need to give a positive value.'); a= input('Give the value of a:'); end b= input('Give the value of b:');%take input for b while b<=0 || (b ~= ceil(b))%checking the condition positive integer or not disp('You need to give a positive value.'); b= input('Give the value of b:'); end c= input('Give the value of c:');%take input for c while c<=0 || (c ~= ceil(c))%checking the condition positive integer or not disp('You need to give a positive value.'); c= input('Give the value of c:'); end if (a<(b+c))&&(b<(a+c))&&(c<(b+a)) % checking the condition for this is valid for triangle or not condition = false; else fprintf('The triangular is not valid!!!\n\n'); condition = true; end end %%I didn't seperate the while loop for new function because question don't %%want if I use new function I didn't need 3 times while in this .m file |

**main.m**

1 2 3 4 5 6 7 8 9 |
clc; clear; global s; [a, b, c] = get_abc; s = input('\nEnter sensitivity: '); fprintf('\nAngle thetaA is %g\n', find_angle(a, b, c)); %%If we only show the 2 decimal after comma we can use %.2f for this fprintf('Angle thetaB is %g\n', find_angle(b, a, c)); fprintf('Angle thetaC is %g\n\n', find_angle(c, a, b)); fprintf('The area of the triangle is %g\n', find_area(b, c, find_angle(a, b, c))); |

**my_arcsin.m**

1 2 3 4 5 6 7 8 9 10 11 12 |
function arcsin = my_arcsin(input) global s; mindegree = 90; maxdegree = 270; while (maxdegree - mindegree) >= (100 * s )%checking the condition for distance between min max greater than 100*sensivity arcsin = (mindegree / 2) + (maxdegree / 2); if my_sin(arcsin) > input mindegree = arcsin; else maxdegree = arcsin; end end |

**my_fact.m**

1 2 3 4 5 |
function factorial=my_fact(n) factorial=1; for i=1:1:n factorial=factorial*i; % calculate factorial end |

**my_sin.m**

1 2 3 4 5 6 7 8 9 10 11 12 |
function sin=my_sin(degree) global s; x=degree*(pi/180); i=0; sin = 0; sinold = sin; sin=sinold+(((-1)^i)*(x^(2*i+1)/my_fact(2*i+1))); while (abs(sin-sinold)>=s) %checking the condiion for sensivity i = i+1; sinold = sin; sin=sinold+(((-1)^i)*(x^(2*i+1)/my_fact(2*i+1))); %using taylor series for calculating end |

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!

Hello Johnathon, This is my personal blog and I use this website such as portfolio, I am sorry I can’t accept any co-author. If I open any blog or website, We can work together

Thank you

wielkość penisa

Thanks for the wonderful manual

Thank you for the terrific post

I like the article

Thanks, it’s quite informative

I enjoy the article