Puff! The magic dragon, live by the tree… 第 3 章 檔案、函數、資料結構 files, functions, and data structures.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Introduction to Matlab
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with MATLAB Lecture 20.
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter ,
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
Matlab Basics IEF 217a: Lecture 2 Fall 2002 Sigmon and Davis, chapters 1-7.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Builtin and user defined functions
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 MATLAB Programming Chapter 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
1 Yangon Technological University Department of Mechanical Engineering Computer Application In Mechanical Engineering I ME Dr. Yin Yin Tun Deputy.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 3 Functions and Files PowerPoint to accompany Copyright © The McGraw-Hill.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
CSE123 Lecture 3 Files and File ManagementScripts.
Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 3: Array Operations Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers.
User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to.
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 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
MATLAB Lecture 1 염익준. Introduction MATLAB (MATrix LABoratory) a special purpose computer program optimized to perform engineering and scientific calculations.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
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.
ECE 1304 Introduction to Electrical and Computer Engineering
Spreadsheets.
Arithmetic Operations
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
ECE 1304 Introduction to Electrical and Computer Engineering
EGR 115 Introduction to Computing for Engineers
Computer Simulation Lab
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Functions (subprograms)
Matlab review Matlab is a numerical analysis system
Fourth Year – Software Engineering
Introduction to MATLAB
funCTIONs and Data Import/Export
Islamic University of Gaza
Computer Simulation Lab
Chapter 2 MATLAB Environment
Presentation transcript:

Puff! The magic dragon, live by the tree… 第 3 章 檔案、函數、資料結構 files, functions, and data structures

Puff! The magic dragon, live by the tree…

Why use ‘files’? 多行指令 or 日後重複使用

Puff! The magic dragon, live by the tree… 3 types of files *.m, ~ in ASCII format *.mat, ~ in binary format (variables in work space) data file ~ in ASCII format

Puff! The magic dragon, live by the tree… Recording ur session diary ~ save in the file ‘diary’ type diary ~ u can see the record after u have typed ‘diary’ diary filename ~ assign the filename that will be recorded get(O, ’ diary ’ ) ~ ‘on’ or ‘off’ ~ see the situation that whether diary is on or off!

Puff! The magic dragon, live by the tree… Saving and retriving ur workspace variables~ save & load save ~ in matlab.mat save filename load filename save filename var1 var2 save filename –ASCII save filename –double

Puff! The magic dragon, live by the tree… Importing data from externally generated files load filename (remember to remove the header first) e.g. load force.dat ~ than we get a matrix named ‘force’ Don’t use ‘*.mat’ as the data file!

Puff! The magic dragon, live by the tree… Importing spreadsheet files M=wk1read(‘filename’) A=xlsread(‘filename’) [A,B] =xlsread(‘filename’) 1.Try to generate a 3 by 10 matrix with all elements are ’10’ in excel, and save it as ‘ggg.xls’ 2.Try to read it from matlab

Puff! The magic dragon, live by the tree… The import wizard importing data exporting data

Puff! The magic dragon, live by the tree… Importing ASCII data P.129 attentions! Data type: numeric? Text? Or in mixed type!

Puff! The magic dragon, live by the tree… Import ASCII data files with text headers P

Puff! The magic dragon, live by the tree… Importing mixed text and numeric ASCII data P

Puff! The magic dragon, live by the tree… Importing binary data files ?A=[1 2 3; 4 5 6; 7 8 9] A = ?save my_data.out A -ascii ?type A ??? Error using ==> type a.m: File not found. type my_data.out

Puff! The magic dragon, live by the tree… Controlling input & output ?speed=30 speed = 30 ?disp('The predicted speed is:') The predicted speed is: ?disp(speed) 30

Puff! The magic dragon, live by the tree… Example (p )

Puff! The magic dragon, live by the tree… ?a=3/7 a = ?format long ?a a = ?format short e ?a a = e-001 ?format long e ?a a = e-001

Puff! The magic dragon, live by the tree…

?format rat ?a a = 3/7 ?format bank ?a a = 0.43

Puff! The magic dragon, live by the tree… User input input (‘please enter the value of x’) If x, y exist first! ?k=menu('Choose a data marker','o','*','x'); ?type=['o','*','x']; ?k k = 2 ?plot(x,y,x,y,type(k))

Puff! The magic dragon, live by the tree… User input xi=input ('please enter the initial value of x ') xf=input ('please enter the initial value of x ') x=[xi:0.01:xf]; y=sin(x); k=menu('Choose a data marker','o','*','x'); k type=['o','*','x']; plot(x,y,x,y,type(k))

Puff! The magic dragon, live by the tree… Elementary mathematical functions ?lookfor imaginary I Imaginary unit. J Imaginary unit. COMPLEX Construct complex result from real and imaginary parts. IMAG Complex imaginary part.

Puff! The magic dragon, live by the tree… Exponential & logarithmic functions ?x=9; ?y=sqrt(x); ?x,y x = 9 y = 3

Puff! The magic dragon, live by the tree… Complex number functions x=a+b*I ~ (M,th) M=sqrt(a^2+b^2) ~ abs(x) th=arctan(b/a) ~ angle(x) a=M*cos(th) ~ real(x) b=M*sin(th) ~ imag(x)

Puff! The magic dragon, live by the tree… Complex number functions a=input('please input the real part ') b=input('please input the real part ') x=a+b*i M=sqrt(a^2+b^2) M1=abs(x) th=atan(b/a) th1=angle(x) th2=atan2(b,a) aa=M*cos(th2) aa1=real(x) bb=M*sin(th2) bb1=imag(x)

Puff! The magic dragon, live by the tree… Complex number functions >> test5 please input the real part -3 a = -3 please input the real part 4 b = 4 x = i M = 5 M1 = 5 th = th1 = th2 = aa = aa1 = -3 bb = bb1 = 4

Puff! The magic dragon, live by the tree…

x = i ?abs(x) ans = 5 ?angle(x) ans = 第四象限 ?y=-3+4*i y = i ?angle(y) ans = 第二象限

Puff! The magic dragon, live by the tree… x is a vector abs(x) ~ return all the absolute value of each elements ~ a vector again! We can use sqrt(x’*x) to obtain the ‘length’ of the vector x when x is a column vector We can use sqrt(x*x’) to obtain the ‘length’ of the vector x when x is a row vector

Puff! The magic dragon, live by the tree… ?x=-3+4i x = i ?y=6-8i y = i ?mag_x=abs(x) mag_x = 5 ?angle_x=angle(x) angle_x = ?angle_y=angle(y) angle_y = ?sum_angle=angle_x+angle_y sum_angle = ?sum1=angle(x+y) sum1 = ?angle_product=angle(x*y) angle_product =

Puff! The magic dragon, live by the tree… Numerical functions ?x=[5,7,15] x = ?y=sqrt(x) y =

Puff! The magic dragon, live by the tree… ?x1=2.1 x1 = ?x2=2.9 x2 = ?round(x1) ans = 2 ?round(x2) ans = 3 ?fix(x1) ans = 2 ?fix(x2) ans = 2 ?ceil(x1) ans = 3 ?ceil(x2) ans = 3 ?floor(x1) ans = 2 ?floor(x2) ans = 2 What happen when x1=-2.1, x2=-2.9?

Puff! The magic dragon, live by the tree…

Trigonometric functions ?x=[1;2;3] x = ?sin(x.^2+5) ans = ?sin(sqrt(x)+1) ans =

Puff! The magic dragon, live by the tree… atan(y/x), atan2(y,x) x = 3 ?y=-4 y = -4 ?atan(y/x) ans = ?atan2(y,x) ans =

Puff! The magic dragon, live by the tree…

Hyperbolic functions

Puff! The magic dragon, live by the tree…

Self testing P.143 Use ur calculator now Use ur computer after this class

Puff! The magic dragon, live by the tree… User-defined functions function file ~ local variables function definition line 函數定義列 ~1st line function[output variables]=function_name(input variables); save as function_name.m

Puff! The magic dragon, live by the tree… function definition line 函數定義列 1.function[area_square]=square(side) 1 input/1 output 2.function area_square=square(side) 1 input/1 output 3.function[volume_box]=box(height, width,length) 3 input/1 output 4.function[area_circle,circumf]=circle(radius) 1 input/2 output 5.function sqplot(side) ~ just plotting

Puff! The magic dragon, live by the tree… a=32.2; %call_drop.m initial_speed=10; time=5; [feet_Dropped,speed]=drop(a, initial_speed,time) %[feet_Dropped,speed]=drop(32.2,10, 5) %[feet_Dropped,speed]=drop(32.2,10, [0:1:5]) function[dist, vel]=drop(g,v0,t); %calculate the velocity and falling distance for the free falling body %input var: acceleration, initial vel. v0, falling time t vel=g*t+v0; dist=0.5*g*t.^2+v0*t;

Puff! The magic dragon, live by the tree… Local variables U can use it freely in the functions U don’t have to care any multi-use of the variable

Puff! The magic dragon, live by the tree… Global variables Common used variables! See p for detail description

Puff! The magic dragon, live by the tree… function P=ideal_1(T,Vhat,R); P=R*T./Vhat; function P=ideal_2(T,Vhat); R= ; P=R*T./Vhat; function P=ideal_3(T,Vhat); global R; P=R*T./Vhat; global R R= ; ideal_3([300,330],20)

Puff! The magic dragon, live by the tree… function P=vdwaals_1(T,Vhat,R,a,b); P=R*T./(Vhat-b)-a./Vhat.^2; function P=vdwaals_2(T,Vhat); R= A=6.49; b=0.0562; P=R*T./(Vhat-b)-a./Vhat.^2; vdwaals_3(300,20) P??????????? P=vdwaals_3(300,20) global a,b

Puff! The magic dragon, live by the tree… applications Finding zeros of a function ~ roots ~ for single variable only ~fzero(‘function’,x0)~ find out the zero for ‘function’ near x0 ~fzero(‘function’,x0(1),xo(2))~ f(x0(1))*f(x0(2))<0

Puff! The magic dragon, live by the tree… function y=f1(x) y=x+2*exp(-x)-3;

Puff! The magic dragon, live by the tree…

minimizing a function of one variable fminbnd(‘function’,x1,x2) ???????? Pp

Puff! The magic dragon, live by the tree…