Matlab course – exercise # 1

advertisement
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
Matlab course – exercise # 1
Question 1: Creating a working environment and displaying the current
directory.
Please make a new directory for each HW assignment, where you will keep all files
and scripts.
A. Read carefully the submission guidelines document.
B. Create a directory in Windows called C:\matlab_course\HW1.
C. In order for Matlab to recognize the programs in this directory please
include it in the Matlab path (Reminder: go to FileSet Path  Add
Folder).
D. Create a script named “ex1_<your ID>” as detailed in the guidelines and
save it in the directory C:\matlab_course\HW1.
1.
Your script should display on the screen the current directory name,
using the appropriate functions (notice that you must use semicolon at
the end of each code line).
E. Run the script from the command line and check that it works properly.
F. Change the current directory to C:\matlab_course\HW1 (using the command
cd in the command line).
G. Run your script again.
From now on when you use MATLAB check what is your current directory - it
is important for saving and loading variables and creating the desired working
environment as you will see in the following questions.
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
Question 2: distance calculation and variables handling
All the following instructions should be implemented in your script except of subquestion E, K, M and O, which you should do them from the command line.
A. Create a variable velocity that has the value 10 (m/sec);
B. Create a variable time that has the value 5 (sec);
C. Calculate the value of the variable distance (distance = velocity *time)
D. Display your result in the following format “The distance is: <your result>”
(for farther explanation regarding displaying your results read the
submission guidelines).
E. Use the function whos in the command line (do not write it in your
script). What is the variable type of “velocity”? (You should answer
theoretical questions in your script as detailed in the submission
instructions).
F. Display all variables in the workspace using the function who.
G. Save the variable time (using the function save), under the name
“save_time”.
H. Delete the variable time from the workspace using the function clear.
I. Display now all the variables in the workspace using the function who. How
many variables are in the working space?
J. Load from the memory the variable time. How many variables exist in the
working space now?
K. Change the current directory to C:\matlab_course (using cd ..\ in the
command line).
L. Insert % to your script in the beginnings of the lines where you deleted the
variable “time” from the working space and the line where you saved the
variable “time”. Save the script and re-run it. Why did you get an error?
Does the variable “time” exist in your workspace?
M. Change the current directory back to C:\matlab_course\HW1 and rerun the
program, did you get an error? Why?
N. Remove the % that you added in sub-question L.
O. Use clear in the command line. How many variables exist in the working
space?
P. Copy paste the following line into your command line:
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
time=20; velocity=80;
How many variables exist in the working space?
Q. Rerun your program. What is the distance now? Why it didn’t change to
1600? What should be done to get the result 20*80 =1600 ?
R. Insert “clear” command to your code between the creation of the velocity
and time variables, and the distance calculation and rerun your program.
What is the error massage you got? Why did you get the error?
S. Delete the “clear” command from your script.
In general do not use functions like “clear”, “clc”, “close” in the middle
of your submitted scripts!! You might clear your outputs and those
won’t be checked…
T. In general it is recommended to insert “clear” and “clc” commands at the
beginning of your program. why? Please write a scenario where using the
function “clear” in the beginning of a script will prevent a run time error.
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
Question 3: dealing with an error –debugging
A. Copy paste to your script the following code:
% this code sums two numbers and displays the result
num1=15;
num2=12;
num2=759;
disp(['The sum of the two numbers ',...
num2str(num), ' and ', num2str(num2), ' is: '...
num2str(num1+num2))]);
B. The code has a syntax error and a warning. MATLAB marks the syntax
errors in red, and the warnings in orange. Fix the syntax error, make sure
there are no additional red marks and run your program. Did it run without
an error?
C. Hover with the cursor over “num2”, what is the MATLAB warnning? Can
you conclude what is the difference between MATLAB syntax errors and
MATLAB warnings? Fix the code so it won’t include the warning and the
result will remain the same. Rerun your program.
D. You should get an error; this is a run time error. In the error massage you get
an explanation why the program crushed and the line number in the code,
pressing it will move you to the relevant line in the editor. What is the
problem in the code? Fix the error and make sure the program runs without
crushing.
Download