PRACTICE 1: Find the natural logarithm for all the numbers between

advertisement
PRACTICE 1: Find the natural logarithm for all the numbers between 0 and 100 using arrays.
Show your answer to a mentor.
Type InputArray = linspace(0, 100, 101)
ResultArray = log(InputArray)
and press enter.
---------------------------------------------------------------------------------------------------------------------------PRACTICE 2: Write a MatLab program that asks the user for their favorite number and then
insults them if you don't like their choice for some reason. Save this file as 'Practice2.m' in your
MatLab folder. Test it on your neighbor! Then show an REU mentor.
close all;
clear all;
user_input = input('What''s your favorite integer?\n');
if user_input == 21,
disp('Me too!');
elseif mod(user_input,2) == 1,
disp('You are an odd duck!');
elseif mod(user_input,2) == 0,
disp('Nice...nice.');
end;
---------------------------------------------------------------------------------------------------------------------------PRACTICE 3: Write a MatLab program that calculates the first 50 integers of the Fibonacci
sequence. Save this program as 'Practice3.m' in your MatLab folder. Write down the 50th integer
in the Fibonacci sequence here to check with your neighbors and mentor. Note: 1, 1, 2, 3, 5, …
50th Fib = 12586269025_____________________
close all; clear all; clc;
Fibs = zeros(1, 100);
Fibs(1,1) = 1;
Fibs(1,2) = 1;
for iFibLoop = 3:100,
Fibs(1, iFibLoop) = Fibs(1, iFibLoop-1) + Fibs(1, iFibLoop-2);
end;
disp(['The 100th Fibonacci number is: ',num2str(Fibs(end))]);
----------------------------------------------------------------------------------------------------------------------------
PRACTICE 4: Write a MatLab program that asks the user to guess your favorite number between
1 and 20, and does not stop until they get it. Test it on your neighbor to see anyone is psychic.
Don't forget to berate them for their ineptitude when they guess incorrectly! Save this file as
'Practice4.m' in your MatLab folder.
close all; clear all; clc;
Mine = 21;
Theirs = 0;
NumberOfLoops = 0;
while 1,
NumberOfLoops = NumberOfLoops + 1;
Theirs = input('Guess my favorite integer: \n');
if Theirs == Mine,
disp('Cheater!');
break;
else,
disp('You''re not very lucky!');
end;
if NumberOfLoops > 20,
disp('Out of chances!');
break;
end;
end;
Download