

You are not limited to only checking if one thing is equal to another and returning a single result, you can also use mathematical operators and perform additional calculations depending on your criteria. In this example, the formula in cell D2 says: IF(C2 = 1, then return Yes, otherwise return No)As you see, the IF function can be used to evaluate both text and values. In the above example, cell D2 says: IF(C2 = Yes, then return a 1, otherwise return a 2) The value that you want returned if the result of logical_test is FALSE. The value that you want returned if the result of logical_test is TRUE. Required to end the if-block with the endif statement.Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. You are not required to have an elseif or else block, but you are The basic form of the if-block is demonstrated in the program above. Also, tryĬhanging the other variables and experiment. Try changing the value of the variableĭecision to see what actions the script will take. You can execute the instructions in the file by simply typing ifDemoĪt the matlab prompt. The most basic structure for anĭecision = 3 leftx = 0 rightx = 1 lefty = 1 righty = 1 N = 10 h = ( rightx - leftx ) / ( N - 1 ) x = ' A = zeros ( N ) for i = 2 : N - 1, A ( i, i - 1 : i + 1 ) = end A = A / h^ 2 A ( 1, 1 ) = 1 A ( N, N ) = 1 b = sin ( x ) b ( 1 ) = lefty b ( N ) = righty if ( decision 3 ) % Find and plot the eigen values of inv(A) = eig ( inv ( A )) e = diag ( e ) plot ( real ( e ), imag ( e ), 'rx' ) title ( 'Eigen Values of the inverse of the matrix' ) else % Solve the system y = A \ b linear = ( lefty - righty + sin ( leftx ) - sin ( rightx )) / ( leftx - rightx ) constant = lefty + sin ( leftx ) - linear * leftx true = - sin ( x ) + linear * x + constant subplot ( 1, 2, 1 ) plot ( x, y, 'go', x, true, 'y' ) title ( 'True Solution and Approximation' ) xlabel ( 'x' ) ylabel ( 'y' ) subplot ( 1, 2, 2 ) plot ( x, abs ( y - true ), 'cx' ) title ( 'Error' ) xlabel ( 'x' ) ylabel ( '|Error|' ) end There are times when you want certain parts of your program to beĮxecuted only in limited circumstances. Greater than 3 the eigen values of the inverse of the matrix are foundĪnd plotted, otherwise, the system is inverted to find anĪpproximation to y’=sin(x) according to the specified boundary If this variable is less than 3, theįile will find and plot the eigen values of the matrix, if it is The file is called by Matlab, and it constructs a second derivativeįinite difference matrix with boundary conditions. Here we will define an executable file that contains an if statement. Rate of change is discontinuous, you may want to change the rate

ForĮxample, if you are approximating a differential equation, and the
#MATLAB IF STATEMENT CODE#
There are times when you want your code to make a decision. ( Plotting), executable files ( Executable Files), or ( Vector Functions), loops ( Loops), plotting

( Introduction to Vectors in Matlab), matrices ( Introduction to Matrices in Matlab), vector operations More information on those topics see one of our tutorials on vectors
#MATLAB IF STATEMENT HOW TO#
In this tutorial we will assume that you know how to create vectorsĪnd matrices, know how to index into them, and know about loops.
