ENGG 1801 Engineering Computing MATLAB Lecture 2: Background for Week 4 Tutorial Simple Programming in MATLAB.

Slides:



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

259 Lecture 10 Spring 2013 Advanced Excel Topics – More User Defined Functions; Conditional Statements.
Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables)
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Functions.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
ME457 Mechatronic System Modeling MICHIGAN STATE UNIVERSITY Matlab® refresher Your objective: to dominate! My objective: to help you dominate!
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Vlachopoulos Georgios Lecturer of Computer Science and Informatics Technological Institute of Patras, Department of Optometry, Branch of Egion Lecturer.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MCE 372 Engineering Analysis MATLAB Review. M ATLAB – What Is It ? Where Is It? Name is from matrix laboratory Powerful tool for – Computation and visualization.
CSE123 Lecture 3 Files and File ManagementScripts.
More about Quadratic Equations November 16, 2009.
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Quadratic Functions (4) What is the discriminant What is the discriminant Using the discriminant Using the discriminant.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Quadratic Equations. PROGRAM QuadraticEquations_1 IMPLICIT NONE REAL A, B, C, Discriminant, Root_1, Root_2 ! Get the coefficients PRINT *, "Enter the.
Laboratory 2. LABORATORY OBJECTIVES This lab work is intended to give the students more insights on the Matlab environment and how to work more efficiently.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Factoring Quadratic Trinomials
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
M ATLAB – What Is It ? Name is from matrix laboratory Powerful tool for – Computation and visualization of engineering and science mathematics – Communication.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
M ATLAB – What Is It ? Name is from matrix laboratory Powerful tool for – Computation and visualization of engineering and science mathematics – Communication.
4.6 Quadratic formula.
Matlab Programming for Engineers
L2. Basics Variables and Expressions Assignment Statements
Matlab Training Session 4: Control, Flow and Functions
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter 4 MATLAB Programming
ENGG 1801 Engineering Computing
4.6 Quadratic formula.
Lecture 1: Introduction
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
Lecture 4 Structure plan
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Using Script Files and Managing Data
AP CS Be able to fix a program written in Java
Electrical and Computer Engineering Department SUNY – New Paltz
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

ENGG 1801 Engineering Computing MATLAB Lecture 2: Background for Week 4 Tutorial Simple Programming in MATLAB

Outline of lecture MATLAB as a calculator revisited Concept of M-files Decision making in MATLAB Use of IF and ELSEIF commands Example: Real roots of a quadratic

MATLAB as a calculator MATLAB can be used as a ‘clever’ calculator – This has very limited value in engineering Real value of MATLAB is in programming – Want to store a set of instructions – Want to run these instructions sequentially – Want the ability to input data and output results – Want to be able to plot results – Want to be able to ‘make decisions’

Example revisited Can do using MATLAB as a calculator – >> x = 1:10; – >> term = 1./sqrt(x); – >> y = sum(term); Far easier to write as an M-file

How to write an M-file File → New → M-file Takes you into the file editor Enter lines of code (nothing happens) Save file (we will call ours L2Demo.m) Exit file Run file Edit (ie modify) file if necessary

L2Demo Version 1 n = input(‘Enter the upper limit: ‘); x = 1:n;% Matlab is case sensitive term = sqrt(x); y = sum(term) What happens if n < 1 ?

L2Demo Version 2 n = input(‘Enter the upper limit: ‘); if n < 1 disp (‘Your answer is meaningless!’) end x = 1:n; term = sqrt(x); y = sum(term) Jump to here if TRUE Jump to here if FALSE

Decision making in MATLAB For ‘simple’ decisions? IF … END (as in last example) More complex decisions? IF … ELSEIF … ELSE... END Example: Real roots of a quadratic equation

Roots of ax 2 +bx+c=0 Roots set by discriminant Δ < 0 (no real roots) Δ = 0 (one real root) Δ > 0 (two real roots) MATLAB needs to make decisions (based on Δ)

One possible M-file Read in values of a, b, c Calculate Δ IF Δ < 0 Print message ‘ No real roots’→ Go END ELSEIF Δ = 0 Print message ‘One real root’→ Go END ELSE Print message ‘Two real roots’ END

My M-file %========================================================== % Demonstration of an m-file % Calculate the real roots of a quadratic equation %========================================================== clear all; % clear all variables clc; % clear screen coeffts = input('Enter values for a,b,c (as a vector): '); % Read in equation coefficients a = coeffts(1); b = coeffts(2); c = coeffts(3); delta = b^2 - 4*a*c; % Calculate discriminant % Calculate number (and value) of real roots if delta < 0 fprintf('\nEquation has no real roots:\n\n') disp(['discriminant = ', num2str(delta)]) elseif delta == 0 fprintf('\nEquation has one real root:\n') xone = -b/(2*a) else fprintf('\nEquation has two real roots:\n') x(1) = (-b + sqrt(delta))/(2*a); x(2) = (-b – sqrt(delta))/(2*a); fprintf('\n First root = %10.2e\n\t Second root = %10.2f', x(1),x(2)) end Header Initialisation Calculate Δ Make decisions based on value of Δ

Conclusions MATLAB is more than a calculator – its a powerful programming environment Have reviewed: – Concept of an M-file – Decision making in MATLAB – IF … END and IF … ELSEIF … ELSE … END – Example of real roots for quadratic equation – Same structure as Week 4 tutorial problem