Scientific Computing Introduction to Matlab Programming.

Slides:



Advertisements
Similar presentations
259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,
Advertisements

Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to FX Equation 4. The Basic Idea FX Equation is DIFFERENT. Most equation editors use a point and click interface that has you searching for.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Matlab intro The Environment
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
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.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
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.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
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.
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 MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2010.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Introduction to MATLAB 7 MATLAB Programming for Engineer Hassan Migdadi Spring 2013.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
Today we will learn MATLAB Click Start  All programm  Class Software  Matlab This command window will be seen with a prompt sign >> Any command can.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Matlab for Engineers Matlab Environment Chapter 2.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Pre Calculus – Synthetic Division Unit 3. First, you have to write the coefficients of the polynomial to be divided at the top (remember to use 0’s.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Introduction to Programming
Computer Application in Engineering Design
Introduction to Programming
L – Modeling and Simulating Social Systems with MATLAB
MATLAB DENC 2533 ECADD LAB 9.
StatLab Matlab Workshop
MATH 493 Introduction to MATLAB
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Introduction to Programming
Matlab Basics Tutorial
Presentation transcript:

Scientific Computing Introduction to Matlab Programming

Today We will introduce the programming environment for our course: Matlab We will cover: – Basic Operations, Functions, Plotting – Vectors, Looping – Creating a new Function in a M-file

Why Matlab? Programming Language is relatively easy. You have access to it in the CS computer lab. Free, similar software is available (GFreeMat, GNU Octave). Most people in scientific and engineering disciplines use Matlab.

Matlab Basics Arithmetic operations: Built-in Functions like sqrt, log, exp, sin etc. Built-in constants like pi MathMatlab – 2 1 x 21 * 2 1 ⌯ 2 1 / ^2

Golden Ratio

Golden Ratio - Numerical Matlab: (Note the use of parentheses!) phi = (1 + sqrt(5))/2 This produces phi = Let's see more digits. format long phi phi =

Golden Ratio – Solution to Polynomial Second calculation: Equating ratios in Golden rectangle gives: So, phi is a (positve)solution of

Golden Ratio – Solution to Polynomial You can use MATLAB to get the roots of a polynomial. MATLAB represents a polynomial by the vector of its coefficients, in descending order. p = [ ] r=roots(p) r=

Golden Ratio – Zeroes of function The number phi is also a zero of the function f(x) = 1/x – (x-1) (Verify) The inline function is a quick way to create functions from character strings. f = inline('1/x - (x-1)')

Golden Ratio – Zeroes of function A graph of f(x) over the interval 0 ≤ x ≤ 4 is obtained with ezplot(f,0,4)

Golden Ratio – Zeroes of function To get the root of f(x) around 1 you can use the following command: phi=fzero(f,1) You can plot this point on the top of the ezplot graph: ezplot(f,0,4) hold on plot(phi,0,'o')

Vectors and Looping Matlab was designed to be an environment for doing Matrix and Vector calculations. Almost all of Matlab's basic commands revolve around the use of vectors. A vector is defined by placing a sequence of numbers within square braces: v = [3 1] produces: v = 3 1

Vectors and Looping Note that Matlab printed out a copy of the vector after you hit the enter key. If you do not want to print out the result put a semi- colon at the end of the line: v = [3 1]; produces no output

Vectors and Looping Matlab can define a vector as a set of numbers with a common increment: v = [1:8] produces v =

Vectors and Looping If you wish to use an increment other than 1 that you define the start number, the value of the increment, and the last number. For example, to define a vector that starts with 2 and ends in 4 with steps of.25 : v = [2:.25:4] produces

Vectors and Looping You can view individual entries in a vector. For example to view the first entry in the vector from the last slide, type: v(1) produces ans = 2

Vectors and Looping We can add or subtract vectors: v = [0:2:8] u = [0:-1:-4] u+v produces ans =

Vectors and Looping We can multiply or divide vectors term by term: u.*v produces ans =

Vectors and Looping We can generate a column vector of zeroes by: zeros(5,1) produces u = 0

Fibonacci Numbers Consider the following program: function f = fibonacci(n) % FIBONACCI Fibonacci sequence1.2. Fibonacci Numbers 9 % f = FIBONACCI(n) generates the first n Fibonacci numbers. f = zeros(n,1); f(1) = 1; f(2) = 2; for k = 3:n f(k) = f(k-1) + f(k-2); end

Matlab Function files The first line defines this program as a special Matlab function M-file. The remainder of the first line says this particular function produces one output result, f, and takes one input argument, n. function f = fibonacci(n)

Matlab Function files These lines are comments: % FIBONACCI Fibonacci sequence1.2. Fibonacci Numbers 9 % f = FIBONACCI(n) generates the first n Fibonacci numbers. The next two lines initialize the first two values of f f(1) = 1; f(2) = 2; The next three define a loop that will iterate through the vector 3:n for k = 3:n f(k) = f(k-1) + f(k-2); end

Matlab Function files To create an M-file, we go to the File menu and select New -> Function M-file. An editor window will pop up. We can type our code into this window and then click the “Run” button in the toolbar. We will be asked to save the file. We must save it with the same name as the function, here as “fibonacci.m”

Matlab Function files Then, we can use this function in the main Matlab (script) window: fibonacci(5) ans =

Matlab Function files We can measure how much time it takes for this function to complete using the Matlab commands tic and toc: tic, fibonacci(24), toc ans = (terms omitted to save space) Elapsed time is seconds.

Matlab Function files Class Exercise: Triangular numbers are numbers of the form n*(n+1)/2, with n a positive integer. – Write a Matlab M-file function to find the triangular numbers up to n=20. – Try to do this in as few of lines of code as possible. Friday: In Computer Lab, First Lab Project