2. Simple Mathematics

Comments, clear, ; and Ctrl-C

Comments using % sign

All text after a percent sign (%) is taken as a comment statement and is simply ignored by MATLAB. It helps you and readers understand what certain variables represent or what outcomes your commands are trying to achieve.

Clear previous variables

If you have been working with MATLAB in a long session you may wish to re-use a variable name for a different purpose. This is especially important if, say, you had an array variable x and now you want to use x for a scalar variable or a shorter array x. Then you can delete the previous variable from the MATLAB workspace using the command clear x.

You can also delete all variables no longer required using clear all. This is useful if you are starting a new problem .

Suppress output

A semicolon (;) after a calculation command tells MATLAB to suppress the result. When you have a large amount of data, or certain variables are not needed to be shown in the final output, they need not be displayed. They will still be held in memory ready for future use. For example, suppose you want to know where the straight line passing through the points (x1, y1) = (3, 4) and (x2, y2) = (8, 7) intersects the y-axis. Type in the following commands:

clear all

x1=3, y1=4

x2=8, y2=7

%let m be the slope of the line y=m*x+b

m=(y2-y1)/(x2-x1);

% y-y1=m*(x-x1) intersects y-axis when x=0

% this gives b=y1-m*x1

y_intercept=y1-m*x1

 

Exercise: add a command to find where the straight line intersects the x-axis.

Exercise: You know that two points on the curve are x1 = π/6 , y1 = 1/2 and x2 = π/3, y2 = 3/2. In MATLAB , pi represents the number π while sqrt(3) represents the number √3.

Use the up-arrow to change the commands above to find the intercepts on both axes of the straight line joining these two points.

Ctrl-C

Sometimes you will inadvertently set MATLAB into a never-ending loop or cause masses of numbers to scroll down the screen. You can interrupt MATLAB at any time by pressing Ctrl-C.