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

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

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

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