Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB

Similar presentations


Presentation on theme: "CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB"— Presentation transcript:

1 CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Introduction

2 Overview MATLAB – Matrix Laboratory (designed to make matrix computations particularly easy) A powerful technical computing system for handling scientific and engineering calculations Essential requirements for successful MATLAB applications Must learn the exact rules for writing MATLAB statements and using MATLAB utilities Must know the mathematics associated with the problem you want to solve Must develop a logical plan of attack – the algorithm for solving a problem

3 Using MATLAB MATLAB must be installed on a computer
MATLAB is available on IUAnyWare Windows operating system is used in this class, but MATLAB is available for Unix or MAC Exit MATLAB click the X (close box) or Type quit or exit at the command window prompt and hit Enter

4 Working Folder Starting MATLAB will automatically create a folder named MATLAB in the user’s Documents folder. This is the default working folder. Anything saved from the Command Window will be saved here. Can change the default working folder.

5 Command Window The line with the >> prompt is called the command line. Type in a command at the command line and hit Enter to execute the command. Use Up-arrow and Down-arrow to select a command you have entered. Smart recall feature – type the first few characters of the command you want to recall then press the Up-arrow key. clear and command window: type clc or Right click on your mouse -> Clear Command Window Can save your commands in a script file

6 Variables A variable is the representation of a memory address unit on a computer where a certain value can be stored E.g. a = 2 means to store the value to the memory address labeled as a. Read as “a gets 2” What is the value of b after the following of commands are executed? a=2; b = 3; a=3+b; b = a + 10;

7 Variables The semicolon ; prevents the result to be displayed in the command window. You can view the values of variables in the workspace window. ans is used to store the most recent result if it is not stored in a user defined variable. . To destroy a variable, use the command clear clear a; %destroy variable a clear all %destroy all variables Once a variable is destroyed, it doesn’t exist any more thus cannot be used in commands.

8 Built-in Functions and Commands
MATLAB has a lot of built-in functions (see Appendix B of the textbook or MATLAB documentation) E.g. a = 4; sqrt(a); E.g. date(); A function returns a value. A command does something, e.g. clear a destroy the variable a. clc clears the command window.

9 More Tab completion – type in the first few letters of a command and press the Tab key. % is a flag that indicates characters to the right of this symbols are comments. You can use Shift+Enter to enter multiple lines and run them together You can copy codes from the command history window in the command line whos command will list the variables currently in the workspace If you copy commands from the slides or WORD document into the command window, you might need to retype single quotes or other special characters. Type in demo to get more help

10 Running a Script A script is a file that contains some commands (also called scripts or statements) to be executed. Click on New Script or type edit at the command line, and type in some commands in the Editor window, then click “Run” . You will be asked to save the script file. The result will show in the command window. All the script files has the .m extension, called M-files, e.g. a1.m Type in the file name a1 at the command window or select the file in the Current Folder window and right click “Run” will run the commands in the script file.

11 Writing a Program Problem: Find the new balance
You deposited $1000 into a bank. The interest rate is 3% per year. What will the balance be after one year?

12 Writing a Program Algorithm design
Use English or half English half code (pseudo-code) to describe in details how to solve a problem (a structure plan) English: create a variable called balance whose value is 1000. pseudo-code: create variable balance = 1000; The essence of any structured plan can be summarized as Input: Declare and assign input variables Operations: What to do with the input variables to solve the problem? Output: Display results

13 Writing a Program Algorithm design
Input Prepare data, create variables to store balance and interest rate, balance = 1000; rate = 0.03 Operations Calculate and store interest, interest = balance * rate Add interest to balance and store result, newBalance = balance + interest Output Display new balance Coding Process: translate the algorithm into MATLAB language The function for displaying a result is disp() disp(‘New Balance:’); disp(balance);


Download ppt "CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB"

Similar presentations


Ads by Google