CS005 Introduction to Programming

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Computer Programming w/ Eng. Applications
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Introduction to MATLAB 3 November Instructor: Andy Newman Office Hours: Stop by room 306 (main building) whenever
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to Programming with RAPTOR
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
REEM ALMOTIRI Information Technology Department Majmaah University.
Introduction to Programming
Development Environment
MATLAB – More Script Files
Introduction to Programming: Module #2 Python, Trinket, and Turtle Graphics Lois Delcambre.
COMP 170 – Introduction to Object Oriented Programming
CS005 Introduction to Programming
Introduction to Programming
Topics Introduction to Repetition Structures
MATLAB Basics Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Variables, Expressions, and IO
JavaScript: Functions
Basic operations in Matlab
Functions CIS 40 – Introduction to Programming in Python
Introduction to C Topics Compilation Using the gcc Compiler
CompSci 230 Software Construction
Intro to PHP & Variables
CS005 Introduction to Programming
User Defined Functions
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Lecture 1: Introduction
CS005 Introduction to Programming
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Assignment 4 For this assignment refer to the notes on MATLAB from an MIT course that you can find here (or at the original website, lectures 14, 15, 16).
CS005 Introduction to Programming
Introduction to Programming
CISC101 Reminders Assn 3 due tomorrow, 7pm.
T. Jumana Abu Shmais – AOU - Riyadh
Functions In Matlab.
Introduction to Programming
Introduction to MATLAB
Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
Scripts In Matlab.
MATLAB Introduction MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! It is also a programming language, commands.
Terminal-Based Programs
Matlab Basics Tutorial
Introduction to Programming
CISC101 Reminders Assignment 3 due today.
MatLab Program Used to Calculate Interactive
Presentation transcript:

CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

Displaying Text

(sidebar) EDU>> disp('Hello World') Hello World EDU>> From now on, I am mostly going to show the Matlab commands without showing the command window (as above, left). However we all understand that the commands are type in the command window.

Displaying Text EDU>> disp('Hello World') Hello World Correct, the phase ‘Hello World’ is in single quotes, it is a literal string EDU>> disp('Hello World') Hello World EDU>> EDU>> disp(Hello World) ??? disp(Hello World) | Error: Unexpected MATLAB expression. Incorrect, the phase Hello World is not in quotes, matlab thinks it might be a variable (but it does not have a legal variable name)

Displaying Variables EDU>> Age = 27; EDU>> We can display the value of a variable. This seem redundant, because we can just type the name of the variable itself. However we will see later that this is very useful. EDU>> Age = 27; EDU>> EDU>> disp(Age) 27

Displaying Variables EDU>> Age = 27; EDU>> Note the difference here. In the first case, we are saying display the value of the variable Age, because there are no quotes. In this case, we are saying, display the literal string ‘Age’ EDU>> Age = 27; EDU>> EDU>> disp(Age) 27 EDU>> disp('Age') Age

Displaying Variables EDU>> disp(' Age') Age EDU>> Note that the disp command respects white space EDU>> disp(' Age') Age EDU>> EDU>> disp('Age')

Displaying Variables EDU>> disp(Age + 17) 44 EDU>> We can ask disp to display the result of any valid expression EDU>> disp(Age + 17) 44 EDU>>

Using Built-In Functions We can use some of Matlabs built-in functions to do calculations Here sqrt is a function 9 is a parameter argument input argument 3 was the returned value result Here we say that sqrt was called invoked EDU>> sqrt(9) ans = 3

Using Built-In Functions We sometimes talk about a black box view of a function EDU>> sqrt(9) ans = 3 9 3

Using Built-In Functions We can use variables as input arguments EDU>> HisAge = 25 HisAge = 25 EDU>> sqrt(HisAge) ans = 5 EDU>>

Using Built-In Functions We can use expressions as input arguments EDU>> sqrt(HisAge - 5) ans = 4.4721 EDU>>

Using Built-In Functions We can use expressions as input arguments Since function calls are legal parts of an expression, we can have function calls inside function calls EDU>> HisAge = 81 HisAge = 81 EDU>> sqrt( sqrt(HisAge) ) ans = 3

Using Built-In Functions We can assign the result of function call to a variable EDU>> Fish = 56; EDU>> HisAge = sqrt(Fish) HisAge = 7.4833 EDU>>

>> bob = round(9.332) + sqrt(3 + 6) 12 >> bob = sqrt(9) Matlab has many built-in functions we can use. Later we will learn to write our own functions >> bob = abs(-9) bob = 9 >> bob = round(9.332) + sqrt(3 + 6) 12 >> bob = sqrt(9) bob = 3 >> bob = log(9) 2.172 >> bob = cos(0) 1 Matlab has hundreds of built-in functions

Using Built-In Functions Some functions take more than one parameter For example, the max function takes two But how could we find the maximum of three numbers? (next slide) EDU>> max(3,4) ans = 4 3 4 4

Using Built-In Functions How can we find the maximum of three numbers? We will later see better ways to do this EDU>> max(3,max(1,4)) ans = 4 1 4 4 3

Suppose we have an equilateral triangle, and we measure one side, finding it to be 45 millimeters long... s h >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> area = (sqrt(3) * side^2) / 4; >> height height = 38.9711 >> area area = 876.8507 a b Quiz! Finish the code below >> a = 3; >> b = 2; >> pi = 3.14; >> area = ... >> circ = ...

Up to this point we have typed in our “programs” line by line Up to this point we have typed in our “programs” line by line. Now we will learn to write scripts. Our program is typed into a special text editor that comes with Matlab. In this case the name of the file is triangle_stuff.m >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> area = (sqrt(3) * side^2) / 4; >> disp(height) 38.9711 >> disp(area) 876.8507 We can run (invoke) our program simply by typing the name of the file (without the ‘.m’ extension)

This caused a text editor to appear…. At the command line I typed edit and hit <enter> This caused a text editor to appear….

Into the text editor, I typed all the commands for my triangle program The empty lines are just for clarity..

I clicked save as , and that opened a dialogue box…. I named my script file as triangle_stuff.m Must end in .m

Now I can run (invoke, call) my script by typing its name….. Note the file name

We can generalize our program with the input function

side = input('How large is the side? '); When matlab encounters the input function, it display the association text, which is usually a prompt (How old are you etc) then it waits for a number to be typed, and <enter> to be hit. Once <enter> is hit, it assigns the value to the variable on the LHS, and continues to the next line.

As before, we won’t always bother showing the window. From now on we will assume that all code is being placed in a script file. As before, we won’t always bother showing the window. disp('Here is my triangle program'); side = input('How large is the side? '); height = (sqrt(3) * side) / 2; area = (sqrt(3) * side^2) / 4; disp(height); disp(area);

It is important to comment all our programs…. % Written by Bertram Wilberforce Wooster, June 11, 1917. % This program prompts a user for a number corresponding to % the side of an equilateral triangle, then calculates the % area and height of the triangle. disp('Here is my triangle program'); % Prompt the user. side = input('How large is the side? '); % Input the value. %------------------Begin Calculations ------------------------------------ % Math formulas came from “Math and % the single girl” Page 23. height = (sqrt(3) * side) / 2; % Perform calculation for height. area = (sqrt(3) * side^2) / 4; % Perform calculation for area. %------------------Begin Display of Results------------------------------- disp(height); % Display the height. disp(area); % Display the area. We may omit comments in lecture notes for pedagogical reasons, but essentially every line of code you write should be commented.

Variables Don’t name your variables the same as functions min, max, sqrt, cos, sin, tan, mean, median, etc Funny things happen when you do this MATLAB reserved words don’t work either i, j, eps, nargin, end, pi, date, etc i, j are reserved as complex numbers initially Will work as counters in my experience so they can be redefined as real numbers

MATLAB Help Ways to get help in MATLAB Type helpwin on command line help function name Provides basic text output Type helpwin on command line Look under the help menu on the desktop

MATLAB Help Can browse or search product help for a specific function or topic MATLAB help has introductory help material, basic overviews of how to use functions, plot, program in MATLAB, example code, etc lookfor keyword command will also find functions that have the keyword in them doc function_name brings up the full documentation for the function

Suppose we have an equilateral triangle, and we measure one side, finding it to be 45 millimeters long... s h >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> area = (sqrt(3) * side^2) / 4; >> height height = 38.9711 >> area area = 876.8507 a b Quiz! Finish the code below >> a = 3; >> b = 2; >> pi = 3.14; >> area = ... >> circ = ...