Tags

, , ,


Many students ask me how do I do this or that in MATLAB.  This is a new addition to the “How do I do that in MATLAB“  series.
 
In this blog, I show you how to use the if-else-end statement to write a simple program of finding if a number is non-negative or not.
 
Here are the links for the program.
The mfile is here
The published version of the mfile is here
 

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.
%% TOPIC
% How do I check if a number is non-negative or negative
%% SUMMARY
% Language : Matlab 2010a;
% Authors : Autar Kaw and Sri Harsha Garapati;
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/if_else_end_statements_blog.m
% Last Revised : January 23, 2012;
% Abstract: This program shows you how to check if a number is negative or
% non-negative using if-else-end statement in MATLAB
clc
clear all
%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to check if a number is negative or’)
disp(‘   non-negative using if-else-end statement in MATLAB’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar Kaw and Sri Harsha Garapati of https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://numericalmethods.eng.usf.edu/blog/if_else_end_statements_blog.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   February 13, 2012’)
disp(‘ ‘)

%% INPUTS
 
% Enter the number to be tested
a=4;
%% DISPLAYING INPUTS
disp(‘  ‘)
disp(‘INPUTS’)

% Printing the input number
fprintf(‘The input number to be tested is %g\n’,a)
%% CODE AND DISPLAYING OUTPUT
disp(‘  ‘)
disp(‘OUTPUTS’)

% using the if-else-end statement to check
if (a>=0)
    disp(‘The number is nonnegative’)
else
    disp(‘The number is negative’)
end

_________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, the textbook on Introduction to Programming Concepts Using MATLAB, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Advertisements