Skip to main content

Find eccentricity and area of ellipse using Matlab

Problem

Find the area and eccentricity of the ellipse using simple if else and also using functions in Matlab. The relations for eccentricity and area of ellipse are given below:
Area of ellipse - A = πab
Area of ellipse equation
Formula for eccentricity of ellipse - e = sqrt(1-b*b/a*a)
Eccentricity of ellipse formula
To know more about ellipse you can see from here:

Solution

As we have already seen many basic programs lets make this a little bit difficult about 0.1 star difficulty from other problems. Remember we are now not adding difficulty in the Matlab program but we are adding difficulty in terms of math. We will see more difficult problems in Matlab soon. Stay tuned for difficult problems.

In this problems we will ask the user to give the value of a and b. Then we will check if the user has not entered any value of b less than zero. Because length cannot be negative. And similarly we will check if the user has not entered the value of a less than or equal to 0. Because as the given equation has a in denominator youtif the user enters 0 then we get the value of ellipse as infinity or undefined. So Also we know that the length cannot be less than zero. Both combined we will get that a ≥ 0. 
Then we will calculate the area and eccentricity of the ellipse. 
Remember that we will calculate the area of the ellipse even if the user has entered the value of a to zero, But we will say that the eccentricity is not defined. 
I suggest you to refer to the program first and then refer to the explanation section to get detailed explanation of the program. Some of the concepts used to solve this problem are as follows:
1) Request user input using input function
2) if elseif and else statements in Matlab
3) sqrt function
4) Functions in Matlab
5) Matlab Comments

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
a = input('Enter the length of semi major axis (a): ');
b = input('Enter the length of semi minor axis (b): ');

if a == 0
    fprintf('a cannot be equal to zero')
    area = pi*a*b;
    fprintf('area: %.4f\n',area);
elseif a<0 || b<0
    fprintf('a or b is less than zero! This is not the case with ellipse!\n');
elseif b>a
    fprintf('a cannot be less than b. a is major axis, b is minor axis!\n')
else
    area = pi*a*b;
    e = sqrt(1-(b^2/a^2));
    fprintf('Area: %.4f, e = %.4f\n',area,e);
end
I have saved this file as ellipse.m on my local computer.

Explanation

Line 1&2: We are asking the user to enter the value of a and b for calculations.
Line 4-7: As we have discussed in the solution section a cannot be zero so we are initiating the if statement and checking and checking whether the value of a is equal to zero. As we know that the area can be calculated if the value of a is zero but the eccentricity cannot be calculated we are calculating the area of the ellipse and displaying it to the user using the fprintf statement.
Line 8-9: Here we are using the elseif to check if the user has entered any value of a and b less than zero, because length cannot be less than zero. We are using the fprintf to display the user that they have entered wrong values.
Line 10-11: As it is ellipse the value of major axis is always greater than or equal to the minor axis. This condition cannot be violated. So we are checking this condition here using the elseif and displaying the user that they have made a mistake by typing value b>a.
Line 12-15: If all the above conditions are false then the program gets executed and the value of the area and eccentricity are calculated and the user is displayed with a neat output of area and eccentricity(e).

Program - functional approach

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function [area,e] = ellipse_fun(a,b)
%This function will calculate the area
%and the eccentricity of an ellipse if all the
%conditions for a real ellipse are met.
%Function usage:
%ellipse_fun(a,b)
%
%Where a - Semi major axis
%b - Semi minor axis

if a == 0
    area = pi*a*b;
    e = false;
elseif a<0 || b<0
    area = false;
    e = false;
elseif b>a
    area = pi*a*b;
    e = false;
else
    area = pi*a*b;
    e = sqrt(1-(b^2/a^2));
end
end
I have saved this file as ellipse_fun.m on my local computer.

Explanation - functional approach

This program is also same as the previous program but here we are using function so that the user can use the function in any other program. In the above program we are stating that the return variables will be area and e and the input parameters for the function ellipse_fun will be a and b. And in the similar way we have changed the if else statements so that there will only be output of the return variables and not any other stuff. That's it you have written a function which will calculate the area and eccentricity of an ellipse if the user gives correct input!

Input/Output

Matlab session on calculating the area and eccentricity of ellipse using Matlab
Matlab session - Calculate the area and eccentricity of ellipse

Try it yourself

1) Check what happens if a = b? Comment your answer in the comment box below.
2) Try to write a program in such a way that if the user has entered of major axis less than minor axis, intimate the user that you have swapped the values and swap the values to calculate the area and eccentricity of ellipse. (Swapping - Exchange of the values of a and b).

Final Note

I have tried to explain everything in this post in such a way that it is easy to understand. If you have not understood anything or have a 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 highlighted using hilite.me we app
Keywords: ellipse, eccentricity

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.

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...