Beginning Programming for Engineers Introduction to Programming and Computer Science.

Slides:



Advertisements
Similar presentations
compilers and interpreters
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Flow Charts, Loop Structures
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
CSE123 Introduction to Computing Lecture 1 Introduction Engineering Problem Solving.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
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.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Computer Science 101 Introduction to Programming.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Scientific Computing Introduction to Matlab Programming.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer Developing algorithms.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Beginning Programming for Engineers Introduction to Programming and Computer Science.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Release Numbers MATLAB is updated regularly
Introduction to Matlab
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Lecture 1: Introduction
Matlab review Matlab is a numerical analysis system
Use of Mathematics using Technology (Maltlab)
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
Chapter 1 Introduction(1.1)
Introduction to MATLAB
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Presentation transcript:

Beginning Programming for Engineers Introduction to Programming and Computer Science

Course Goals Learn basic concepts of computer science Learn how to use Matlab effectively Learn how to program in Matlab

What is Programming? There are 5 steps to successful programming: 1.Make sure you understand the problem. The problem must be well-defined. 2.Find or develop an algorithm to solve the problem. 3.Translate the algorithm into a language the computer can execute. (This translated algorithm is the program.) 4.Run your program, to get the results. 5.Verify the output, to make sure your program works correctly.

Algorithms An algorithm is a sequence of instructions that solves a problem, such that: Each instruction is unambiguous, and is something the computer can do. After an instruction is finished, there is no ambiguity about which instruction is executed next. Execution finishes in a finite number of steps.

Describing Algorithms 1.Get a 2.Get b 3.Calculate 4.Display c Algorithms can be represented using either pseudocode or flowcharts. The computer program is also an expression of the algorithm. Frequently there is more than one algorithm that can solve a problem.

Diagram of a computer Input is data that flows into the computer, through an input device. Output is data that flows out of the computer, through an output device. The CPU transforms data, processing a simple machine language. The memory holds both data and the program.

High Level Languages We can use the computer to help us program it! Source code is written by the programmer in a high level language. A compiler translates source code into object code (machine language). An interpreter processes source code without translating to object code.

Development cycle

Programming, Computer Science, and Software Engineering Programming: Applying the development cycle to solve problems. Computer science: Study of how to evaluate and develop efficient algorithms for various computer architectures to solve various sorts of problems, as well as developing new computer architectures to enable more efficient algorithms. Software engineering: Primarily concerned with the methodology of crafting reliable and maintainable programs, documentation, etc. Domain expert: Expert in some field of application, but able to converse intelligently with the computing professionals.

Program Speed Every computer instruction takes time to execute. An algorithm that can be completed with fewer instruction executions will be faster than an algorithm that requires more instruction executions. Shorter programs are not necessarily faster than longer programs, because programs often "iterate" over groups of instructions.

Moore's Law Every two years, the number of transistors in an integrated circuit can double. o Until 2005: New computers doubled in speed every 18 months. o Since 2005: New computers have doubled the CPU cores every 18 months, but the CPU cores have not gotten faster. (This is due to thermal issues: faster CPUs run too hot and self-destruct.) Quad-core AMD processor die

Origins of Matlab Matlab was created around 1980 to allow students to work with matrix software without learning Fortran, etc. Mathworks, Inc. has further developed Matlab. Now widely used for engineering and science. Cleve Moler

Using Matlab Matlab has features like: o Command line and history o Workspace window o Built-in editor o Built-in debugger o Help! Matlab needs access to a license server.

Alternatives to Matlab GNU Octave o Programs can be written that work in both Matlab and Octave. o Lacks the development environment and toolboxes of Matlab. o Completely free – no license server! Other similar systems exist, such as Freemat and Scilab.

Matlab as a calculator

MATLAB and Trig Functions Try these computations: sin(90) sin(pi/2) cos(pi/4) sin(pi/4) tan(pi/4) sind(90) cosd(90) tand(90) What units do these functions expect?

Variables and Memory Objects Type in these expressions: ang = pi/4 c = cos(ang) s = sin(ang) c^2 + s^2 Notice how variables are assigned values and used. Watch the "memory objects" in the Workspace window.

Clearing memory and the command window You can access variable values through the workspace window, or by typing in the name: c ang Issue this command. Observe the workspace window: clear You can clear the command history window: clc

Suppressing Output Try these expressions: ang = pi/4; c = cos(ang); s = sin(ang); c^2 + s^2; ans What seems to be the result of ending lines with semicolons?

Simple Operations on Vectors Try these expressions: a = [ ] a+a 5*a a*a a.* a a' a * a' a-3 a/2 2\a Last expression is "left division".

Simple Operations on Vectors (2) Try these expressions: a b = [ 2 ; 3 ; 4] a*b b = [ 5 6 7]

Using subscripts Try these expressions: a = [ ] a(1) a(4) a(12/4) a(9) a(end) The expression in parenthesis is a subscript or index. We can assign into subscripted elements of vectors. The vector will grow as needed. Try these: z(4) = 42 z(2) = 9 z(5) = 88 z(end) = 94 z(end+1) = 87

Entering Matrices Try these expressions: a = [ ] b = [10 20 ; 30 40; 50 60] a*b

Subscripts and Matrices Try these commands: a = [ 1 2 ; 3 4] a(1,2) = 9 a(3,2) = 8 Normally, use (row,column) Single subscript counts down column, then proceeds to next column...

Simple Ranges Try these expressions: 1:10 0:10:50 50:-5:15

Simple Functions on Vectors Try these expressions: theta = 0:10:180; c = cosd(theta); s = sind(theta); theta(4) c(4)^2 + s(4)^2

The plot command Using the results in c, s from the last slide, try this command: plot(c,s);

Writing Matlab scripts Sequences of commands can be saved to a script or “m-file”. Comments start with % or % symbols. Long lines can be continued using …

A simple script, simple_plot.m % This plots cos(theta), sin(theta) for theta % starting at 0 to 180 degrees, in increments of 10 % degrees. % % Programmed by R. Lindsay Todd % Clear memory, etc. clear clc % Co mputations theta = [ ]; c = cosd(theta); s = sind(theta); % Plot plot(c,s);

Running a script To run a script, just type its name. Alternatively, use the "run" button on the editor window.

Scripted input and output % pythag.m: Get a, b and calculate c. % % Programmed by R. Lindsay Todd clear clc % Get the values a = input('Enter a:'); b = input('Enter b:'); % Do the calculation c = sqrt(a^2 + b^2); disp('The hypotenuse has been calculated. It is:'); disp(c); Use input to get values. Use disp to print messages.

Simple functions function pythagf(a, b) % PYTHAGF calculates the hypotenuse of a triangle. % % The pythagf function uses the pythagorean theorem. % Programmed by R. Lindsay Todd % Calculate c = sqrt(a^2 + b^2); % Show result disp('Calculated hyptenuse is'); disp(c); end

Arguments and Parameters % Defining pythagf (in pythagf.m) function pythagf(a, b) c = sqrt(a^2 + b^2); disp('Calculated hyptenuse is'); disp(c); end % Calling pythagf f = 30; pythagf(f, 40); The variables a and b are parameters of pythagf. The values 30 and 40 are arguments to the function pythagf. Notice we do not use inside pythagf to get the values of the parameters.

Writing a function The function must go into an m-file with the same name as the function you are defining. E.g., the function foo_bar must be defined in the file foo_bar.m The Matlab "path" determines where Matlab will look for scripts and functions. A common pitfall is to create a script or file whose name conflicts with the name of a Matlab function that your program needs. Ways to avoid this: o Use names that are rather long. o Use names that can not possibly conflict with Matlab names, e.g. "homework1". o Begin names with your initials, e.g., rlt_plot instead of plot The help command uses comments for its text.

More on plotting The figure command can group related graphics and set some attributes for all the graphics. The hold on command prevents the figure from being cleared and redrawn for each plot. The axis command can set the range of graphics shown, the size of the window, tick-marks, etc. Use help to get more information! figure('Color', 'w'); % White background hold on; % Don't clear... axis([ ]); % 0<=x<=10, -5<=y<=5 axis manual; % Use my size!