next up previous
Next: Solution of ODEs Up: MATLAB summary Previous: Relational and logical operators

If-then-else statements.


The following gives a mesh plot if the user enters the string 'm':
x=input(' Do you want a contour (c) or a mesh plot (m)');
if( x == 'm')  
   mesh(xx,yy,zz) 
else 
   contour(xx,yy,zz,20) 
end

Nested if-statements.

The following gives a `double' step function, i.e. a function that jumps at x=0 and again at x=1:

function [y] = twostep(x)
if x <= 0
   y=0;
else
   if x <= 1
      y = 1;
   else
      y = 2;
   end
end



Charlie Macaskill
Fri Mar 5 16:05:28 EST 1999

Copyright © University of Sydney 2003