Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TYPE THE SCRIPT AND ANSWER THE QUESTIONS 2. Opening MATLAB, accessing the Comman

ID: 2268809 • Letter: T

Question

TYPE THE SCRIPT AND ANSWER THE QUESTIONS

2. Opening MATLAB, accessing the Command Window (Writing programs) (a) [click] Start button [typel "matlab"- Search text box [click] MATLAB R2016a - Results panel (b) Wait. (c) Notice there are four individual sub-windows opened in the main MATLAB window: Current Folder Command Window, Workspace, Command History. For now, we will concentrate on the Command Window were you interact with the interpreter, type commands the Workspace- where all variables that have been used are outlined the Command History- were all of the previous commands that have been interpreted are listed. (d) In the command window, type 3+5 at the>prompt What happened in each of the three windows? What is ans, and where did it come from? (e) In the command window, type x 7 at the >>prompt What happened in each of the three windows? What is x, and where did it come from? (f) In the command window, type yx +4 at the >prompt What happened in each of the three windows? Why does y, have the value it does, where did it come from? (g) (h) (i) In the command window, type cle at the>> prompt What happened in each of the three windows? In the command window, type clear at the >>prompt What happened in each of the three windows? Double click the command y = x + 4 in Command History what happened in each of the three windows? Why does it report Undefined function or variable 'x.? Double click the command x = 7 in Command History What happened in each of the three windows? (j) (k) Double click the command y #x + 4 in Cornmand History what happened in each of the three windows? Does the previous report of Undefined function or variable 'x'now make sense? Really think about what clc did and what clear did, try to summarize an important difference between them. ) In the command window, type x 5 at the >prompt What happened in each of the three windows? How is this different then the first time we typed x7? (m) In the command window, type x 5; at the >>prompt What happened in each of the three windows? How is this different then when we just typed x = 5 ?

Explanation / Answer

The command window of the matlab is where the commands can be entered. If the commands are not terminated with a semicolon ';', the result of the command is also displayed in command window. If semicolon is used at the end of command, the result will not be displayed.

Command history, as the name suggests, saves the list of commands executed using command window.

Workspace stores the variables.

The displays in the windows are shown in bold letters.

(d):

Command window:

No semicolon used. So result is displayed in command window:

>> 3+5

ans =

8

Command history:

3+5

Workspace:

As no variable name is specified, the result is stored with the default variable name 'ans'.

ans 8

(e):

Command window:

No semicolon used. So result is displayed in command window:

>> x = 7

x =

7

Command history:

The following line is added to 'command history'

x = 7

Workspace:

Variable name defined is 'x'. So the value '7' is assigned to 'x' and placed in 'Workspace' in addition to existing variables.

x 7

(f):

Command window:

No semicolon used. So result is displayed in command window:

>> y = x+4

y =

11

Command history:

The following line is added to 'command history'

y = x+4

Workspace:

Variable name defined is 'y'. The value of 'y' is value of 'x plus 4, i.e. 7+4. 'So the value '11' is assigned to 'y' and placed in 'Workspace' in addition to existing variables.

y 11

(g):

'clc' clears the command window.

Command window:

Command window would be empty

Command history:

The following line is added to 'command history'

clc

Workspace:

No change in workspace

(h):

'clear' command clears all the variables in workspace.

Command window:

>> clear

Command history:

The following line is added to 'command history'

clear

Workspace:

Workspace is empty

(i):

When a line in command history is double clicked, it is added to command window and Matlab tries to execute the command.

Command window:

>> y = x+4
Undefined function or variable 'x'.

When matlab tries to execute the command 'y = x+4', it tires to add the value 4 to the value in x and save it in y. But since we have used the command 'clear' in the previous step, x is deleted from workspace and hence Matlab cannot find it. Therefore, it gives the error 'Undefined function or variable 'x'.'

Command history:

The following line is added to 'command history'

y = x+4

Workspace:

Nothing happens (gets saved) here as the command is not executed succesfully and terminated due to error.

(i):

When a line in command history is double clicked, it is added to command window and Matlab tries to execute the command.

Command window:

>> x = 7

x =

7

Command history:

The following line is added to 'command history'

x = 7

Workspace:

Variable name defined is 'x'. So the value '7' is assigned to 'x' and placed in 'Workspace' in addition to existing variables.

x 7

(k):

When a line in command history is double clicked, it is added to command window and Matlab tries to execute the command.

Command window:

>> y = x+4

y =

11

Command history:

The following line is added to 'command history'

y = x+4

Workspace:

Variable name defined is 'y'. The value of 'y' is value of 'x plus 4, i.e. 7+4. 'So the value '11' is assigned to 'y' and placed in 'Workspace' in addition to existing variables.

y 11

This time the previous error is not shown. This is because the variable 'x' exists in the workspace now.

The main functions of 'clc' and 'clear' are explained before. 'clc' is used to erase the display in command window, whereas 'clear' is used to delete and empty the workspace.

(l):

Command window:

No semicolon used. So result is displayed in command window:

>> x = 5

x =

5

Command history:

The following line is added to 'command history'

x = 5

Workspace:

Variable name defined is 'x'. But this variable is already defined and has a value '7'. Now, the existing value is over written by the new value '5'. Therefore the value '5' is assigned to 'x'

x 7 in workspace changes to

x 5

Previousle when 'x = 7' is typed, a new variable 'x' is stored in the memory and assigned a value of '7'. Now, the vlaue of the existing variable is changed to a new value.

(m):

Command window:

Semicolon used. So result is not displayed in command window:

>> x = 5;

Command history:

The following line is added to 'command history'

x = 5;

Workspace:

Variable name defined is 'x'. But this variable is already defined and has a value '5'. The new value is also '5'. Matlab still over writes the existing value with new value

No 'visual' change in workspace

The major difference with the previous command 'x = 5' is that the result in command window is suppressed in this case.

(n):

When 'ignore this' is typed in command window, Matlab tries to make sense out of the first word 'ignore'. Since it is followed by another word with space in between, it assumes to be a function and tries to look for definition of a function with name 'ignore'. Since we haven't defined this before, it shows the error.

Command window:

>> ignore this
Undefined function 'ignore' for input
arguments of type 'char'.

Command history:

The following line is added to 'command history'

ignore this

Workspace:

Nothing changes in workspace

(o):

If the first line of a command is '%', it is a comment in matlab. Matlab doesnot execute it and ignores it.

Command window:

>> %ignore this

Command history:

The following line is added to 'command history'

%ignore this

Workspace:

Nothing changes in workspace

(p):

This action erases the data in command history. It is similar to 'clc' for command window and 'clear' for workspace. Nothing changes in other two windows.