Skip to main content
Chemistry LibreTexts

2.3.1: The Basics

  • Page ID
    490007
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Originally Created by Lindsay Pederson, Fall 2022 | Modified by Kathryn Haas each semester since.

    Caution

    This exercise may be an uncomfortable experience if you haven't completed the basic MATLAB tutorials that were part of your pre-lab assignment. If you are feeling lost during this activity, please go back to the basic MATLAB tutorials to get comfortable with the basic commands and practice working with matrices before returing to this exercise.

    MatLab General Layout

    Open the MatLab program to begin. If the layout of your MatLab window looks different than the arrangement illustrated below, you can switch to the default layout using the "Layout" button in the Home Tab toolbar (see below). Choose "Default Layout" from the "Layout" drop down menu. If the layout still seems different than what is shown it might be because you have a Script or LiveScript file open in your MatLab program. 

    Compare your MatLab window to what is shown below. Read the figure below and familiarize yourself with the panels of the MatLab window. Then follow the instructions.

    Screenshot 2024-05-06 at 10.37.07 AM.png
    Figure \(\PageIndex{1}\): The default layout of the MatLab window (as of version R2024a). The tabs above the top toolar give access to different toolbar options. The file path is shown under the top toolar and indicates the current working folder. The Current folder's contents is shown in the pannel on the left. The center window is the Command Window, which functions like a calcuator. Type commands into the command window, save the work in the command window using the "diary filename" command to start saving and "diary off" when finished. All variables enterend in the Command Window will be populated in the Workspace, shown in the panel on the right. The Workspace can be saved using the "save filename" command or by using the "save workspace" button in the Home Tab toolbar. The workspace can be cleared using the "clear" command. (CC-NC-BY-SA; Kathryn Haas)

    Practice with the Command Window 

    Find the command window in MATLAB. The command window is like a calculator. You can type simple equations here to perform calculations. The command window can be cleared by the command:

    clc

    All work is lost after the space is cleared.

    The commands entered in the command window can be recorded in a text file (a diary). To record a diary, use the command "diary filename" at the beginning of your work:

    diary filename

    And then then at the end of the work:

    diary off

    The diary file is a good way to keep track of commands, but the work cannot be implemented again from the diary file, so it has limited utility. Script and Live Script files are a much more useful way to record your work. First, let's practice using the command window:

    Using the command window, begin a diary file with the name "practice_diary". Then do the following:

    1. Clear the workspace by typing the clear command.
    2. Create a variable b and set it equal to 7
    3. Create a variable a and set it equal to 4
    4. Add a and b and assign the result as x
    5. Subtract a and b and assign the result as y
    6. Multiply a and b and assign the result as z
    7. Divide a by b and assign the result as r
    8. Find b to the power of a and assign the result as s
    9. Use the up arrow key to go back to the the command from step 4. Edit the command to assign it as v istead of x and end the command a semicolon; What does the semicolon do?
    10. End the diary
    11. Clear the command window with clc

    The workspace

    Notice that your workspace now contains all variables you defined. The workspace should look like this:

    Screenshot 2024-05-06 at 11.23.03 AM.png

    You can save the variables in the workspace as a binary ".mat file" with the command:

    save filename

    Use the command window to save your workspace using the file name "practice_workspace.mat". Notice the new file shows up in the Current Folder Window.

    Now, clear the workspace with the clear command.

    clear

    Just for practice, use the load command to load the file you just saved and inspect the workspace:

    load filename

    Clear the workspace again before moving to the next section.

    Discussion Questions\(\PageIndex{1}\)
    1. Where was the practice_diary file saved and what extension does it have? How would you submit a file like this to Gradescope?
    2. When mathematical operations are performed in the command window, how are they defined in the workspace? What happens to the previous value in the workspace when you enter a new operation? (eq, try a + b , then a^b )
    3. What does a semicolon at the end of a command do?
    4. What does a percentage sign at the beginning of a line do?

    Practice with Script Files

    The Command Window is convenient for quick operations that you don't want to save. But it has some big limitations. For example, even if you do save the diary, you can't re-run any of the commands automatically. A better way to save your work is by entering it in a Script File.

    One way to start a new script is to go to the Home Tab and select New, then Script. Instead, let's use the command line to start a new script called "reference_script.m". Type the following into the command window:

    edit reference_script.m
    

    This will create a new script file. The new file should appear in the Current Folder window and it will open as a new tab in the Editor Window.

    Find the Editor Window. In the Editor window, type the following:

    %% Editor Exercises %%
    % single percent sign indicates a text line 
    % double %% percent (only) indicates a section break. 
    % you can run each section of the editor by pressing the Run Section button.
    % you can run the entire editor by pressing the Run button

    clipboard_e35814b4b35e194b20e93488a7e02b08f.png

    %%
    %Type this into the new script in the Editor
    b = 7
    a = 4
    % then hit run.

    Notice what happens in the command window and in the workspace. 

    % again, type these examples of simple operations into the editor
    x = a+b
    y = a-b
    z = a*b
    r = a/b
    s = b^a
    % hit run and look at the command window
    % then clear the command window and clear the workspace
    % then add semicolons to the end of the lines and hit run again
    

    Now that we see how the editor works, let's save the contents of the editor window as "reference_script.m". Go to the Editor tab and choose save.

    clipboard_e4249bd89e0bf28bdf2c4eb8a39ec4e66.png 

    At the end of the script that you just saved, add the following. Then run each section to see what happens. You can view any variables by typing their name in the command window.

    %%
    %% Building a reference sheet %%
    a = 7;
    b = 4;
    
    %%
    %Simple matrix - you can use spaces or commas to separate entries%
    l = [1 2 3];
    %%
    %multiple rows - separated by semicolons%
    m = [1;2;3];
    %%
    %matrices can have expressions in them%
    n = [a+b, a-b, a*b];
    %%
    %transpose matrices with an apostrophe%
    N = n';
    %%
    %multiply matrices
    L = l*m;
    M = m.*N;
    %works the same with division
    %%
    %Create a matrix with regularly spaced intervals - defaults to one if not
    %otherwise specified%
    e = [1:16];
    E = [1:0.5:16];
    %%
    %To reference specific elements of a matrix, call the matrix and then add
    %coordinates (row, column) or just one number if it's a vector%
    e7 = e(7);
    m2 = m(2);
    %%
    %delete specific elements of a matrix
    h = [1 2 3;4 5 6;7 8 9];
    h(2) = [];
    %check value of h in workspace
    %%
    %How to plot stuff
    %you can just say "plot" but to intentionally keep track of figures it's a
    %good idea to invoke figures by number
    x = [1 2 3 4 5];
    y = x.^2;
    z = y./2;
    figure(1)
    hold on
    plot(x,y)
    plot(x,z,"o")
    hold off
    A = [1, 5, 0, 20];
    axis(A)
    xlabel('x')
    ylabel('x^2')
    title('Example')
    %you can get help for any function you want to use (ex: plot()) by searching
    %in the "Search Documentation" box at the top right of the screen

    Save your files. Then clear the workspace and command windows before proceeding to the next section. 

    Discussion Questions\(\PageIndex{2}\)
    1. What is the purpose of the period in the following statement? What happens if the period is removed?
      M = m.*N;
    2. How can you delete the value 0.4 in the following matrix? 
      mat1 = [0.1 0.2 0.3 0.4 0.5 0.9 1.0];
    3. What does the "o" specification do in the code below:
      plot(x,z,"o")

     


    2.3.1: The Basics is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?