MATLAB Udførelse af programmer Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2003 >>mit_modul minf.m … minf(a) … mit_modul.m function.

Slides:



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

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Introduction to Matlab
Introduction To MATLAB Programming
Introduction to M ATLAB 4 Useful stuff for real programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/ :30.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/27/2003.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Programming fprintf Cell arrays Structures Flow of control Vectorization Functions 1.
The textread Function It is designed to read ASCII files that are formatted into columns of data Each column can be of a different type It is useful for.
Introduction to MATLAB
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.
Programming in C #2 Input / Output.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
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 File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum.
Introduction to Matlab
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
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.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
File Operations in Matlab Load / Save *.dat, *.mat vs. -ascii fopen /fclose.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Input Output IO and datafiles Credits also to Prof Selim Aksoy (Bilkent University.
Reading and Writing Data Files
Lecture: MATLAB Chapter 1 Introduction
Introduction to Programming for Mechanical Engineers (ME 319)
Using Script Files and Managing Data
Programming in C Input / Output.
Programming in C Input / Output.
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
Advanced Data Import & Export Jeff Henrikson
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Introduction to MATLAB Programming
Introduction to MATLAB
funCTIONs and Data Import/Export
Note on Indexing of Array Elements
Introduction to MATLAB
Input/Output Functions
Programming in C Input / Output.
Introduction to MATLAB Programming
CS 111 Introduction to Computing in Engineering and Science
Input/Output Functions
Introduction to MATLAB Programming
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

MATLAB Udførelse af programmer Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2003 >>mit_modul minf.m … minf(a) … mit_modul.m function r = minf(x) … r= size(x) …

Programstrukturer Et program definerer rækkefølgen for at udføre ordrer. sekvens ordre ; ordre … valg if ( ) … end; switch … gentagelse (iteration, løkke) for …, while … kald af underprogram (funktion) f( … ) afbrydelse break, return

Funktioner function resultat = kvadratrod(a) % Beregn kvadratrod af a ved bisektion low = 0; high = a; while (high - low) > 2*eps, mid = (high+low)/2; if mid*mid > a, high = mid; else low = mid; end end; resultat = (high+low)/2; kvadratrod(4) kvadratrod(1000) ans = » ans*ans

Sammensat returværdi function [fundet,resultat] = findindeks(e,a) % finder index for element e i vektor a hvis det findes fundet = 0; for i=1:length(a), if a(i) == e, fundet = 1; break; end; if fundet, resultat = i; else resultat = 0; end; [b,c] = findindeks(7,a) b = 0 c = 0

MATLAB-maskinen Script » A = [9,7] ans = … » B = 25i ans = … » sinus(A,6) … Matlab.exe Workspace A B Lager (memory) Procesenhed Program

Funktionskald Script » A = [9,7] ans = … » B = 25i ans = … » sinus(A,6) … Matlab.exe Workspace A B function res = sinus(x,n) Workspace x init A n init 6 res init ?

Funktionskald -afslutning Script » A = [9,7] ans = … » B = 25i ans = … » sinus(A,6) … Matlab.exe Workspace A B function res = sinus(x,n) Workspace x init A n init 6 res init ? ans = res

Funktionskald - rekursion Script »f(8) Matlab.exe Workspace ans = ? function r = f(x) r = f(x-1) Workspace x init 8 r init ? function r = f(x) r = f(x-1) Workspace x init 7 r init ? …

Og så … PRØV DET !

Funktionaler (Function-functions) FPLOT(FUN,LIMS) plots the function specified by the string FUN between the x-axis limits specified by LIMS = [XMIN XMAX]. FUN must be the name of an M-file function with variable x such as 'sin(x)', 'diric(x,10)'. plot('sinus(x,17)', [0, 2*pi] ) Sammenlign: x = 0: 0.2 : 2*pi; plot(x, sinus(x,17))

Integral » quad('sin', 0, 2*pi) ans = 0 » quad('sinus(x,2)', 0, 2*pi) ??? Can not find function 'sinus(x,2)'. function r = sin2(x) r = sinus(x,2); » quad('sin2', 0, 2*pi) ans =

Minimum » fmin('sin', 0, 2*pi) ans = » ans/pi ans =

Løsning af Differentialligninger function xdiff = difflign(t,x) % Differentialligningerne % x(1)'(t) = x(2) % x(2)'(t) = -x(1) % xdiff = [ - x(2) ; x(1) ]; » [t,x] = ode23('difflign', [0,2*pi], [0,1]'); » plot(t,x)

Simpel Ind- og Udlæsning » help iofun File import/export functions. load - Load workspace from MAT-file. save - Save workspace to MAT-file. dlmread - Read ASCII delimited file. dlmwrite - Write ASCII delimited file. wk1read - Read spreadsheet (WK1) file. wk1write - Write spreadsheet (WK1) file. Command window I/O clc - Clear command window. home - Send cursor home. disp - Display array. input - Prompt for user input. pause - Wait for user response.

Formatteret Ind- og Udlæsning File opening and closing. fopen - Open file. fclose - Close file. Formatted file I/O. fscanf - Read formatted data from file. fprintf - Write formatted data to file. fgetl - Read line from file, discard newline character. fgets - Read line from file, keep newline character. input - Prompt for user input.

Formater The format specifier : % [- | + | 0] [Field Width][.Precision] Format Character "-" aligns output left (usually, it's right-aligned). "+" outputs a plus sign for positive numbers (usually, it is supressed). “0” outputs leading zeroes. The field width specifies the overall field length. The precision specifies the length of the fractional part for floating point numbers. If omitted, the default is 6. The format character determines the base type for the formatted values: "d": integer value in decimal format. "f": floating point value in fixed format (xxx.yyyyyy). "e": floating point value in scientific format (0.yyyyyye+zzz). "E": floating point value in scientific format (0.yyyyyyE+zzz). "s": String.

Eksempel fprintf(red_file,'Jordens radius: %9.0f m\r\n',R); fprintf(red_file,'Refraktionskoefficient: %9.2f\r\n',k); fprintf(red_file,'\r\n'); fprintf(red_file,' Fra Til V Sd ih sh S d_H\r\n'); fprintf(red_file,' gon m m m m m\r\n'); Jordens radius: m Refraktionskoefficient: 0.13 Fra Til V Sd ih sh S d_H gon m m m m m

Eksempel … fprintf(red_file,'%5.0f %5.0f %9.4f %9.3f %9.3f %9.3f %9.3f%9.3f\r\n',linie);