Nested if-else statements, MATLAB in Mathematics

Assignment Help:

Nested IF-ELSE statements:

The if-else statement is used to select between the two statements. In order to select from more than two statements, the if-else statements can also be nested, one inside of the other. For illustration, consider executing the following continuous mathematical function y = f(x):

y = 1 for x < -1

y = x2 for -1 ≤ x ≤ 2

y = 4 for x > 2

The value of y depends on the value of x that could be in one of three possible ranges. Selecting which range could be proficient with three separate if statements, which is as shown:

if x < -1

   y = 1;

end

if x > = -1 && x < = 2

   y = x^2;

end

if x > 2

   y = 4;

end

 

As the three possibilities are mutually exclusive, then the value of y can be determined by using three individual if statements. Though, this is not very efficient code: all the three Boolean expressions should be computed, regardless of the range in which the x falls. For illustration, if x is less than -1, the initial expression is true and 1 would be assigned to y. Though, the two expressions in the next two if statements are still computed. Rather than of writing it in such a way, the expressions can be nested so that the statement ends whenever an expression is found to be true:

if x < -1

   y = 1;

else

   % If we are here, x must be > = -1

   % Use an if-else statement to choose

   % between the two remaining ranges

   if x > = -1 && x < = 2

  y = x^2;

   else

  % No need to check

  % If we are here, x must be > 2

  y = 4;

   end

end


Related Discussions:- Nested if-else statements

Three-dimensional plots, Three-Dimensional Plots: The MATLAB has many ...

Three-Dimensional Plots: The MATLAB has many functions which will display three-dimensional plots. Most of these functions have similar name as corresponding two-dimensional p

Illustration of variance, Illustration of Variance For illustration, fo...

Illustration of Variance For illustration, for the vector [4, 6, 1, 5], there are n = 4 values therefore n - 1 is 3. The mean of this data set is also 4. The variance will be

User-defined functions - matlab, User-defined functions: Therefore, al...

User-defined functions: Therefore, although many functions compute and return values, some do not. A few functions rather merely accomplish a task. Regardless of what type of

Refer the subset of a matrix, Refer the subset of a matrix: It is also...

Refer the subset of a matrix: It is also possible to refer to the subset of a matrix. For illustration, this refers to the first & second rows, second & third columns: >> m

Script a MATLAB program, Script a MATLAB program which meets the following ...

Script a MATLAB program which meets the following speci cations: The program expects an input of a two-variable real-valued continuous function f : R^2--> R The program is to cal

Optimal foraging theory, This project requires you to use the ideas of Chap...

This project requires you to use the ideas of Chapter 25 regarding maximization of a function. Here we assume that evolution has acted to generate highly efficient foragers. By hig

Built in recursive function in matlab, Built in recursive function in MATLA...

Built in recursive function in MATLAB: We have seen that the built-in function in MATLAB to compute factorials, termed as the factorial and we know how to implement the iterat

Variance, The variance is generally defined in terms of the arithmetic me...

The variance is generally defined in terms of the arithmetic mean as: At times, though, the denominator is defined as n instead of n - 1. The default definition used by t

Logical vectors, Logical Vectors: The relational operators can also be...

Logical Vectors: The relational operators can also be used with the vectors and matrices. For illustration, let's say that there is a vector, and we want to compare each eleme

Illustration of a recursive function, Illustration of a recursive function:...

Illustration of a recursive function: illustration is of a recursive function which does not return anything, but simply prints. The below function prtwords receives a sentenc

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd