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

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

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