# 1. Scripts

## 1.2. Overview

As an alternative to entering a sequence of commands on the MATLAB command line to do a computation, the sequence of commands may be entered into a text file with the extension .m. The commands in the script will be executed in the same way as if you had entered them on the command line.

To execute the commands, one can either

1. Select the green Run button or
2. Enter the name of the script on the MATLAB command line (without the .m extension).

In MATLAB, script files must have the extension .m and are typically created using the MATLAB GUI (Graphical User Interface).

When learning MATLAB, it is strongly recommended that you enter your commands into a script and then execute the script using one of the two methods above instead of entering commands on the command line.

In addition, it is strongly recommended that the first line of your program contains the clear command (see Assignment#The_clear_Command). This is to ensure that any variables created on the command line are removed before your script is executed.

# 2. Problems

## 2.1. Creating and Executing a Script

Create a script named scripttest.m with the contents

clear
a = 1;
b = a*2


Execute this script using the two methods described above.

## 2.2. A Basic Script

Write a script named quadratic.m. At the top of the script define variables named a, b, and c. When you execute the script, the values of x that satisfy the equation ax2 + bx + c = 0 should be displayed.

## 2.3. The importance of the clear command

Suppose that you have created and saved two scripts:

cleardemo1.m:

a = 1;
b = c+1


cleardemo2.m:

clear
a = 1;
b = c+1


and then you enter the following on the MATLAB command line

c = 1;
cleardemo1


What do you expect to happen (will there be an error or what will be displayed?).

Suppose that next you enter the following on the MATLAB command line

c = 2;
cleardemo2


What do you expect to happen (will there be an error or what will be displayed?).