Skip to main content

Area of rectangle, square, circle and triangle using Matlab

Problem

Ask the user to give input to select the rectangle or square or circle or triangle and then ask the user necessary input. Finally calculate the area of the selected geometrical figure using Matlab. The areas of the polygons(circle can also be called as a polygon with infinite sides) is as follows:
1)Area of  Rectangle:
Area of Rectangle - LxB
Area of Rectangle Formula
Where L is the length and B is the breadth of the rectangle
Area of square - a2 - axa
Area of square Formula
Where a is the length of the side of a square
Area of circle Formula - pi*r*r - pi*r^2
Area of a circle
Where r is the radius of the circle
4) Area of triangle:
Where b is the length of the base of triangle and h is the height of the triangle.
If you want to more about the geometric polygons then you can see:
3) Circle

Solution

The solution is very simple if you know what the above figures mean. But if you don't remember the figure then you can see the images below to get a brush up of the polygons.
Rectangle
Rectangle
Square
Square
circle
Circle
Triangle
Triangle
That's it if you know about these figures then it is very easy to solve the problem in Matlab. It is similar to the Display current date and time in Matlab poblem which we have solved earlier. Don't worry if you have not solved that problem, I am assuming that you are starting from scratch after reading a bit of Matlab programming. Okay lets get started.
According to me there are two major ways to write this program. They both are indirectly same. I will tell you how to solve the problem in both the ways but I will follow the first way because it will be helpful for everyone even if they are beginners in Matlab.
Method 1
We will take the input of the user and then we will use the if else statement to solve the problem accordingly. I am not explaining much here about this method because, we are anyways going to solve this problem and you can see more in the explanation section below the program section.
Method 2
First we will write scripts for the areas of the rectangles, circles, squares and triangles and then we we will finally write a script which will ask the user for the input and use the if else statements which contains direct calling of the scripts. This is similar to the functional approach. I have made a tree diagram of the two methods.
Tree diagram representing the methods on how to solve the areas of circle, triangle, rectangle and squares
Tree diagram representing the solution for solving the above problem
We can see from the tree diagram that using method 1 we can solve in two ways: We can create functions for each of the areas and then solve it or we can directly solve it under simple way so that even the beginners will understand what is happening in the code. We are going to use simple approach to solve the problem.
Don't worry if you don't understand any of the above concepts or diagrams, skip them and you will understand the rest even if you don't understand them. But trying to understand them is good as you will be using the above concepts in future.
I suggest that you first refer to the program section and then if you don't understand the program then refer to the explanation section to get a detailed explanation about the program. Some of the concepts used in this post are as follows:
1) Matlab Variables
2) input function in matlab
3) fprintf function in matalab
4) if statement in matlab
5) elseif statement in matalb
6) else statement in matlab

Program


 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
fprintf('Select your choice and enter the option number!\n')
fprintf('1) Area of Rectangle\n')
fprintf('2) Area of square\n')
fprintf('3) Area of circle\n')
fprintf('4) Area of triangle\n')
user_input = input('You Choice? : ');

if user_input == 1
    length = input('Enter the length of the rectangle: ');
    breadth = input('Enter the breadth of the rectangle: ');
    a = length*breadth;
elseif user_input == 2
    side = input('Enter the length of the side of the square: ');
    a = side*side; %This can also be written as side^2
elseif user_input == 3
    radius = input('Enter the length of the radius of circle: ');
    a = pi*(radius^2);%This can also be written as pi*radius*radius
elseif user_input == 4
    height = input('Enter the height of the triangle: ');
    base = input('Enter the base length of the triangle: ');
    a = 0.5*height*base;
else
    fprintf('The input you have entered is wrong\n');
    fprintf('Please enter only numbers present in the choice');
    fprintf('Try Again!!!');
end

fprintf('Area = %.2f\n',a);
I have saved the matlab program as area.m on my local computer.

Explanation

In lines 1-5 I have just added the fprintf statements to display the available choices to the user.
In line 6 I used the input function to take the user input and have stored the value in user_input variable.
From line 8 I have started the if statement with a conditional to check if the user has given the input as 1, if the user has given the input 1 then the user is asked to input length and breadth of the rectangle to calculate the area. After the user has given the input then the area has been found out using the relation given in the question.(LxB).
Then on line 12 I have continued the if statement with the elseif statement and then checked if the user has entered the input of 2 and if the input is 2 then the user is asked to enter the length of the side of the square. Finally the area is calculated using the given relation. 
Similarly the elseif statements work for the user input corresponding to circle and triangle with 3 and 4 respectively. 
The last else statement is used to check if the user has given input other than any of the values given in the options. If the user gives the input of other choices then else gets executed and the fprintf functions below it gets executed.
At the end of the program we have a fprintf function to display the area we have calculated in a neat format so that the user can simply use the value for other computations if he/she requires.
Remember not to name the variable same as the script name because it will generate a matlab error because Matlab assumes that you are using the script name as the variable. That is the reason why I have named my area variable as a instead of area.

Input/Output

area of rectangle, circle, triangle, square using matlab, area.m file
Trial run in matlab for area.m script

Try it yourself

1) Try to write programs for method 2 and comment in the comment box below
2) Try to write program using function approach in the method 1 and comment in the comment box below

Final note

I have tried to explain all the contents in the post in a easy to understand format. 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 we app.
Keywords: area, rectangle, circle, triangle, square, methods, matlab

Popular posts from this blog

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed tag Us

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation