Download presentation
Presentation is loading. Please wait.
Published byMilton Wilson Modified over 7 years ago
1
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
John Federici NJIT Physics Department
2
TRIVIA QUESTION! Which “Big Bang Theory” character is an Optical Physicist? (a) Raj Koothrappali (b) Bernadette Rostenkowski (c) Howard Wolowitz (d) Sheldon Cooper (e) Amy Farrah Fowler (f) Leonard Hofstadter (g) Penny a b c d e f g
3
Course WEBPAGE Course Webpage — The course webpage is accessible from my web page Follow the link for Phys 114 Course Notes, Syllabus, Grades etc. — Will be posted on the course web page. Course Textbook — Available (should be) in NJIT book store. Course Software — MATLAB. It is available FREE to you on the NJIT website. Please download it in the next few days to make sure that you can get it operational on your computer. Why MATLAB? Its free to NJIT students. It is one of the ‘standard’ software packages for analyzing data. Widely used. … I use it in my research. If you want to use ANOTHER software package, eg. Mathcad, Mathematica, etc. etc., please see me. No need to write your own C++, Java, etc. code. Why “reinvent the wheel?” Bring you Laptop (if you can) to class — We will be doing some class participation problems.
4
Why take this class? Required of all Applied Physics Majors for graduation You will meet other Applied Physics majors. The skills which you will learn How to properly prepare journal-quality plots and images How to use Matlab The limits of detection, measurement error, etc. are useful for ALL of your science courses, independent study (research as an undergraduate), and your career AFTER NJIT. Watching “The Big Bang Theory” is required…… (just kidding). If you take my OPSE 301 course, I EXPECT you to know how to use MATLAB. Other faculty will expect the same.
5
Downloading MATLAB
6
Downloading Software Go to http://ist.njit.edu/software/download.php
If you are OFF-Campus, you will first need to download and install VPN (Virtual Private Network) software BEFORE you download other software:
7
Follow Install Instructions
8
Next, install Matlab
9
Run Matlab Search for Help Click here for Getting Started resources
10
Enter commands (line by line) in the command window
Line by line processing of commands Variables you create
11
Some simple tips: Use UP and DOWN arrows to scroll through previous commands Use “;” to suppress printing of output Use “;” and put more than one command on a line Double-click on WORKSPACE Variable to open up a spreadsheet with data.
12
TRIVIA QUESTION! What does “Matlab” stand for? (a) Mathematics Lab
(b) Mathew’s Labrador – Name of founder’s dog (c) Sheldon Cooper’s Laboratory name (d) Matrix Laboratory (e) Mat Lab – The ‘doorway’ entrance to computational tools.
13
More Simple Tips: SAVE your Matlab code as a script
You may want to use your script later. You may want to keep a ‘working’ copy before you edit it. You can import the script into other software programs (eg. Labview) Use “%” to annotate your script. WHY do you THINK you should annotate your script? Save a script Open a NEW script Type in your Matlab code
14
Open and then Run *.m files
Use OPEN icon to open a matlab file/ script (*.m) Use RUN icon to run the code “Run-time” errors will appear in the command window Run a script Open a script
15
Matlab Language Fundamentals
Search in help for “Language Fundamentals”
16
Matlab: Numeric Types On the website, you can find HOW TO videos. For example, here is one on Matlab fundamentals Numeric classes in MATLAB® include signed and unsigned integers, and single-precision and double-precision floating-point numbers. By default, MATLAB stores all numeric values as double-precision floating point. (You cannot change the default type and precision.) You can choose to store any number, or array of numbers, as integers or as single-precision to save space compared to double precision. By default, MATLAB® displays numeric output as 5-digit scaled, fixed-point values. Values are SAVED as double precision, but only 5 digits are displayed. You can change display using “format” command >> format long >> 1/11 ans = >> format short 0.0909 >>
17
Matlab: Data types For now, we will focus on some of the simplier data types in Matlab: Numbers and Arrays (which includes vectors), and Text
18
Manipulation of Arrays
If A is an array, then you can perform same operations on ALL elements in array a = >> a+10 ans = >> cos(a) >>
19
Array versus Matrix Operations
Array operations work on corresponding elements of arrays with equal dimensions. Each element in the first operand gets matched up with the element in the same location in the second operand. If the inputs are different sizes, then MATLAB cannot match the elements one-to-one. These are ELEMENT by ELEMENT manipulations Eg. A+B means “Parallel Processing” of multiple elements A.*B means Matrix operations DO NOT work on corresponding elements of arrays. The size of outputs and inputs depend on the mathematical operation. A*B means Standard Matrix Multiplication
20
Text data Define a STRING by putting alphanumeric characters in ‘quotes’ >> year=2017; >> DateString='The year is '; >> TempString=[DateString,int2str(year),'. HAPPY NEW YEAR!']; >> disp(TempString) The year is HAPPY NEW YEAR! >> As you would expect, there is standard syntax to concatenate strings and to convert numeric data (eg. int2str, num2str, sprintf) to string data. Display a STRING with ‘disp’ NOTE: Some functions interpret a ‘FORMAT SPECIFIER Differently…
21
Text data - continued NOTE: Some functions interpret a ‘FORMAT SPECIFIER Differently… >> i=5; >> str1=int2str(i) str1 = 5 >> str2=num2str(i,'%3d') str2 = >> str3=sprintf('%3d',i) str3 = >> NOTE: for sprintf function, spaces PAD on the left to ensure that the string is 3 characters long while for num2str, the same format string DOES NOT PRODUCE SAME RESULTS
22
Class Exercise: Write a Simple Matlab program
Use Matlab HELP or DOCUMENATION SEARCH as needed Create a script which will calculate the number of days in a year using the following method. Create a ROW matrix of 12 elements. Call the matrix “DaysInMonths”. For each element, use the number of days in each month…. Eg. January has 31 days, February has 28 days, etc. Sum all of the elements and assign that value to the variable NumDaysInYear. Have the NumDaysInYear displayed in Matlab Once you have your code working so that you get the CORRECT answer, create a new script and SAVE your commands and at lease ONE annotation to the script. Save the file as a matlab script. HINT: You can cut and paste from COMMAND window to EDITOR window. Use up/ down arrows to scroll through previous commands.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.