Skip to main content

Third side of a triangle if two sides and the angle is given using Matlab

Problem

Write a program in Matlab to ask the user with the length of the two sides and angle between two sides. Then use the following relation to calculate the third side of the triangle:
Equation to calculate the third side of the triangle if two sides and angle is given

Solution

We can solve this right hand side of the equation to get the value of the third side. But we will have to write the above equation to matlab equivalent. It is very easy and we will solve using functions.
I suggest you refer to program section and then refer to the explanation section to get a detailed explanation of the same.
Some of the concepts you need to know to solve the problem are as follows:
1) Matlab functions
2) Operator Precedence
3) input function 
4) Matlab Variables
5) fprintf in Matlab

Program 1

This is functional approach

1
2
3
4
5
function size = third_side_function(a,b,theta)
%third_side(a,b,theta) will give the length of third side of triangle

size = a^2 + b^2 - a*b*cos(theta);
end
I have saved this file as third_side_function.m on my local computer.

Explanation 1

Line 1: Function initiation using the Matlab functions.
Line 2: This is matlab comment and also function documentation.
Line 4: Matlab equivalent of the given equation

Program 2


1
2
3
4
5
6
7
8
9
%This script will calculate the third side length in a triangle

a = input('Enter the length of first side: ');
b = input('Enter the length of second side: ');
theta = input('Enter the angle between the above in radians: ');

c = a^2+b^2-a*b*cos(theta);

fprintf('Length of third side is: %.4f',c);
I have saved this file as third_side.m on my local computer.

Explanation 2

First line is matlab comments.
3-5 lines are taking the user input for calculations.
Line7: I have written matlab equivalent of the equation given in the question.
Line9: I am printing the length of the third side so that the user get to know the same in a clean and neat format!

Input/output

Length of third side of a triangle calculation using Matlab when two sides and angle is given
Third side of a triangle calculation using Matlab

Final note

I have tried to explain every part in the post in such a way that it is easy for everyone to understand. If you have not understood anything or have any doubt then please do contact me or comment in the comment box below. You can contact me from here: contact me.
The above program was high lighted using hilite.me 
Keywords: third side, 

Popular posts from this blog

Project Euler Problem 62 solution with python

Cubic permutations ¶ The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube. Find the smallest cube for which exactly five permutations of its digits are cube.

Project Euler Problem 67 Solution with Python

Maximum path sum II By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

Problem 43 Project Euler Solution with python

Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d 1 be the 1 st digit, d 2 be the 2 nd digit, and so on. In this way, we note the following: d 2 d 3 d 4 =406 is divisible by 2 d 3 d 4 d 5 =063 is divisible by 3 d 4 d 5 d 6 =635 is divisible by 5 d 5 d 6 d 7 =357 is divisible by 7 d 6 d 7 d 8 =572 is divisible by 11 d 7 d 8 d 9 =728 is divisible by 13 d 8 d 9 d 10 =289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. One might write a simple solution using direct if else statements for this problem. Using if else statements, the execution time may be a few seconds. But this is not a very good approach. I too had written a program with if else statement which will take each and every permutation of the 0-9 Pandigital and check for the conditions given in the qu...