Note on Indexing of Array Elements

Slides:



Advertisements
Similar presentations
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
Introduction to C Programming
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
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.
Introduction to Python
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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)
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
CSE123 Lecture 3 Files and File ManagementScripts.
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 Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Reading and Writing Data Files
Basic concepts of C++ Presented by Prof. Satyajit De
EEE 161 Applied Electromagnetics
Introduction to C++ Programming
INC 161 , CPE 100 Computer Programming
Chapter 2 - Introduction to C Programming
Quiz 11/15/16 – C functions, arrays and strings
Other Kinds of Arrays Chapter 11
Basic operations in Matlab
Introduction to C++.
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 2 - Introduction to C Programming
Engineering Innovation Center
MATLAB: Structures and File I/O
User Defined Functions
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Arrays, For loop While loop Do while loop
Matlab review Matlab is a numerical analysis system
Introduction to C++ Programming
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Use of Mathematics using Technology (Maltlab)
C Preprocessor(CPP).
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to C++ Programming
funCTIONs and Data Import/Export
INTRODUCTION TO MATLAB
Introduction to MATLAB
Introduction to MATLAB
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.
Stata Basic Course Lab 2.
C++ Programming Lecture 3 C++ Basics – Part I
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Matlab Basics.
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Note on Indexing of Array Elements Indexing in MATLAB % No declarations % are necessary for i = [1:20] myarray( i ) = 0.0; end Indexing in C/C++ float myarray[20]; int i; for (i = 0; i < 20; i++) { myarray[ i ] = 0.0; }

Relational Operators in Matlab Relational operators are used to compare two numbers or two arrays The inbuilt relational operators in Matlab are: Greater than… > Smaller than… < Equal to… == …What was the purpose of a single equals sign? Greater or equal… >= Smaller or equal… <=

Logical Operators in Matlab When doing logical operations in Matlab, 0 always stands for false and everything else is true. Conventionally 1 is used for true statements. Logical operators in Matlab are: And… & Or… | Not… ~

Matlab Path A “path” is a string that specifies how to find a resource located in a file system (ex: C:\Matlab_workshop\module_1 ) The Matlab Path is a list of many paths to be searched in succession whenever Matlab needs to find something Starts with a basic set of paths Can be extended by the user (using File/Set Path…) If Matlab tells you that it cannot locate a file, perhaps you have not specified a path to find it…

Inputting Data from Open Files Data that has been read in as a string using fgets can be parsed using the function sscanf. The syntax to input a line and then extract and discard three columns from the string and save the fourth column of floating point data in an element in the array myarray would be: line = fgets(infile_id); myarray(k)=sscanf(line,'%*s%*s%*s%f');

Loading Data Let’s clear the workspace so we can tell if anything was loaded: >> clear all >> whos Load file ex1.mat: >> help load >> load ex1

Reading Data from Files The load command does not always succeed in reading an input file. It only works when all lines in the file have the same ASCII format. Files with header material do not qualify, nor do binary files. ASCII files that cannot be input with the load function can be opened and input with MATLAB functions that are similar to C language functions we have been using. The MATLAB functions include fopen, fgets, fscanf, and sscanf.

Opening Data files The command to open a file for input is: infile_id = fopen('filename','r'); The command to open a file for output is: outfile_id = fopen('filename','w'); r-for read w- for write

Saving Data >> help save Let’s save both t and y1 in file ex1.mat: >> save ex1 t y >> dir

Try this: x = 0:.1:1; y = [x; exp(x)]; fid = fopen('exp.txt','w'); fprintf(fid,'%6.2f %12.8f\n',y); fclose(fid)

Printing Data to Files When a file has been opened for output, the command to output one line to the file as a string is: fprintf(outfile_id,'%s',line); Lines of other data can be output by inserting the appropriate format string and variable names. For example: fprintf(outfile_id,'%f%f\n',a,b);

Printing to the Screen Data can also be printed to the screen with the fprintf function. To do so, we omit the file idenification (outfile_id). For instance, to print a line of text to the screen we could use: fprintf('%s',line) and to print other data we might use: fprintf('%f %f\n', a , b)

Manual Definition of t Create with brackets [ ] Separate elements inside with spaces or commas >> t = [0, 0.1, 0.2, …] Access individual elements with integer index enclosed in parentheses ( ) >> t(1), t(2), t(end) Indices start at 1.

Checking Workspace Variables are defined in workspace Can list all variables in workspace using the command whos Can clear any or all variables: >> clear t >> clear all

The programming environment Executable m-files are located in the current working directory or somewhere on the search path. The function/variable duality causes Matlab to search for an identifier according to the scheme 1. variable in current workspace 2. built-in variable 3. built-in m-file 4. m-file in current directory 5. m-file on search path

Simple Examples %Example1.m X=5; Y=3; Z=3X^2+5; Z % character denotes the start of a comment lines that end with a “;” are simply run with out displaying result lines that do not end with a “;” are run and results displayed on command window

.m Files Once your program has been saved as a .m file it can be run by either pressing F5 in the editor or by typing the file name in the command window Other .m files maybe be run from within different .m files

RLC Series .m File %Example2.m - RLC w=linspace(1,40000,1000); v=10./(10+(1./(j.*w.*3.*10^-6))+(j.*w.*0.01)); plot(w,abs(v)); j is intrinsically unless its redefined

RLC series result

RLC series result

Exercise The circuit shown in Figure 1.0 is describe by the following set of node voltage equations when the input is a steady-state sinusoidal waveform. Determine the voltage at each of the nodes.