12.07.2015 Views

MatLab Tutorial

MatLab Tutorial

MatLab Tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

. 'if-then' statements gives you the power of logic! - Replace the previous m-file content with the following:close all;clear all;user_input = input('Are you happy? (''1'' for Y, ''2'' for N.) \n');% If you want to single quote to appear as text, you% have to type two of them!if user_input == 1,disp('Oh brother!');elseif user_input == 2,disp('Boo hoo.');else,disp('Wrong answer!');end;% We would need to do something fancier to prevent errors% when something other than a number is typed.- Press F5.- Replace the previous m-file content with the following:% Tossing a coin.close all;clear all;clc; % Clears the current screen.x = 2*rand(1,1); % Random number between 0 and 2.if x >= 1,disp('Heads!')else,disp('Tails!');end;- Press F5.PRACTICE 2: Write a <strong>MatLab</strong> program that asks the user for their favorite number and theninsults them if you don't like their choice for some reason. Save this file as 'Practice2.m' in your<strong>MatLab</strong> folder. Test it on your neighbor! Then show an REU mentor.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!