Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

Functions: Dealing with a triangle | Matlab Examples

Posted on November 26, 2018February 20, 2019 by Yılmaz Cemalettin

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

Matlab
MATLAB
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

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

get_abc.m

Matlab
MATLAB
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

Matlab
MATLAB
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

MATLAB
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

MATLAB
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

MATLAB
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

 

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

  1. Yılmaz Cemalettin says:
    December 24, 2018 at 11:22 pm

    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

    Reply
  2. wzmocnienie erekcji says:
    April 4, 2019 at 6:29 am

    wielkość penisa

    Reply

Leave a Reply Cancel reply

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

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme