ENGG1801 Lab 2 Re-Cap

Course material

In Lab 2 we finally got to play around with MATLAB, a calculator on steroids and a particularly useful engineering tool. We started it up, and wrote some programs both by typing straight into the ">>" prompt, and by creating scripts called m-files and then running that script by typing the name of that file.

We also stored some numbers as variable names, and used them in the calculations to same typing the numbers over and over again. We didn’t really get to indexing an array, but that’s something I want you all to have a play with before our next lab.

Also have a go at the Excel section, since it’s pretty easy and won’t take up much of your time at all. It’s all to do with applying what you’ve learnt in lab 1 to make the table, and then plotting them in an X-Y scatter plot, adding trendlines.

I’ll go through arrays/vectors/matrices operators at the start of the next lab.

Issues raised during the lab

More hints and tips about the lab after the break!

  • m-files. They act almost as though you type out the file line by line into the command window.
    • This means, every line you type into the m-file must be a valid statement that MATLAB understands.
    • If you’re copying from the command window into the m-file, remember that you didn’t type in the ">>" at the start of the line, and hence it shouldn’t stay there.
    • You also didn’t type in the lines that Matlab output for you in the command window. So don’t include that into your m-file.
    • The easiest way to copy the commands you’ve typed in the command window into an m-file is by selecting the lines you want from the "Command History" window, right click on it and then select copy.
  • Almost as though you type them out line by line. One key difference is that they are separate to the workspace.
    • This means, your m-files can’t see the variables you’ve defined in the workspace, and the variables you define within the m-file can’t be seen by anything outside of it.
    • Why is this so? This behaviour allows the outcome of your scripts to be predictable, and not be affected by external factors. If things go wrong in the script, it means you don’t need to look anywhere else but the script that went wrong.
    • Also, be careful not to override existing function names! For example, MATLAB can get confused if you name an m-file "sin.m" Since you don’t really know if you’re running the m-file or calling the built-in function.
  • Taking input.
    • Don’t know how input() works? Type help input
    • To clarify, input(‘foo’) reads in the user input and (naturally) tries to interpret that as a number, while input(‘foo’, ‘s’) forces matlab to read the user input as a string. Then, even if the user types in a number, it’ll be stored as a string of characters.
  • Showing output
    • Noticed that the numbers disappeared when you use disp(['foo', var, 'bar])? That’s because the number you have assigned to variable var is not a string.
    • To get resolve this problem (and to customise the format of the number), use the function num2str(var) to make that number into a string.
    • Type help num2str for more information
  • Calculating roots
    • You might have noticed that Matlab doesn’t deal with the ± symbol. If you want to set both roots to one variable, consider putting them into an array. Try this:
      • a = 1; b = 2; c = 1
      • my_roots = [(-b + sqrt(b^2 - 4*a*c))/2*a, (-b - sqrt(b^2 - 4*a*c))/2*a]
    • The part before the comma is the first element (+ve determinant), and the part after the comma is the second element in the array (-ve determinant).
    • To check if this is correct or not, try using the built-in MATLAB function roots().
      • roots([1 2 1])
  • Indexing an array
    • MATLAB is 1-indexed!
    • So if my_array = [50:-10:10]
    • We can point to the first element using my_array(1)
    • Or the last element using my_array(end)

Changing your password

To change your School of IT (SIT) account password, you will need to get to the SIT server. This can be done by starting a Secure SHell (SSH) session to the undergraduate server. After logging onto the Windows machine, start the program "PuTTy", whose shortcut may be found in the Windows Quick Launch bar, bottom left hand corner of the screen to the right of the start menu.

After launching PuTTy, double click on the saved session named "ugrad". This will connect you to the undergraduate server. When prompted for your username and password, use the same combination that you used to log onto the Windows machine. Note that your password does not appear on screen (not even as asterisks) when you type.

When you log on successfully, you will be prompted with the word "bash". It kind of indicates that it’s time to start bashing away at the keyboard for whatever you want to do next, but it actually stands for "Bourne-Again SHell". If you haven’t used unix before, then you’ll come across this more in other Units of Study later.

The command to change your password is "passwd". You’ll then need to type in your old password and then press enter. Type your new password, press enter and then type it in and press enter again to verify that you’ve typed correctly.

If it didn’t tell you that your command failed, then it worked. Go ahead and type "exit" to get out of there. For more information of using unix, see the tutorials offered by SUITS.

Forwarding your Uni email

It’s difficult to keep track of your x emails inboxes, so why not have the emails sent to your uni one forwarded to the one you check regularly? SUITS has a great tutorial on how to do this!

Other matters

If you need any help, email me on engg1801help@it.usyd.edu.au.

See you next week!

ET

1 Comment

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment

WordPress Themes