Introduction Why do we need numerical methods? What are numerical methods? Programming languages Matlab: our choice for this course Introduction to MatLab.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Programming with MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Numerical Methods Hans Maassen Room HG03.710, tel Text: Andreas Guertler.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
ME457 Mechatronic System Modeling MICHIGAN STATE UNIVERSITY Matlab® refresher Your objective: to dominate! My objective: to help you dominate!
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.
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.
Digital Image Processing Lecture3: Introduction to MATLAB.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
AN ENGINEER’S GUIDE TO MATLAB
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
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.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
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 Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Introduction to Computational Linguistics Programming I.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Lecture 7 Sept 22 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
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.
Type Conversions Implicit Conversion Explicit Conversion.
Script M-Files Group of Matlab commands placed in a text file with a text editor. Matlab can open and execute the commands exactly as if they were entered.
Scientific Computing Introduction to Matlab Programming.
User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide.
GENERATION OF RANDOM NUMBERS
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.
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.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
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.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
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.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
An Introduction to Programming in Matlab Emily Blumenthal
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.
BAHASA PEMROGRAMAN MATLAB as an Engineering Tool & Programming Language a lecture note for Civil Engineering students of PETRA Christian University Doddy.
Introduction to Programming
A bit of C programming Lecture 3 Uli Raich.
Introduction to Matlab
CS1010 Programming Methodology
CS314 – Section 5 Recitation 1
Lecture 13 Input/Output Files.
funCTIONs and Data Import/Export
Note on Indexing of Array Elements
Your objective: to dominate! My objective: to help you dominate!
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
ME 123 Computer Applications I Lecture 4: Vectors and Matrices 3/14/03
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

Introduction Why do we need numerical methods? What are numerical methods? Programming languages Matlab: our choice for this course Introduction to MatLab

Why do we need numerical methods… … if we can solve our problems analytically. What can we solve analytically? –Harmonic oscillator (parabolic potential) –1/r potential two-body problem (planet + sun, hydrogen atom) –particle-in-a-box (box potential) –and a few more

Input/Output printing text and variables to the screen reading from/ writing to a file

formatted text to output data (to screen or to a file) we need a way to control the format of numbers fprintf (format-string,var1,var2,var3…) does just that the format string defines how variables are printed. Example 1: a=1; b=2.65;c=12345;d='test' fprintf ('%d %f %e %s \n',a,b,c,d) e+04 test Example 2: fprintf (‘a:%2d b:%1.2f c:%1.4e \n',a,b,c) a: 1 b:2.65 c:1.2345e+04

saving & loading data variables can be saved to binary and text files save and load the complete workspace (binary) save filename, load filename save & load variables to/from file ' test.mat ' save 'test' a b c; load 'test' reading a formatted data file [A,B,C,...] = textread('filename','format') example file: [A,B,C]=textread(‘test.dat', '%f %f %f') A = general data input/output f=fopen('filename') open file fprintf (f,format, v1,…) formatted output to file fscanf (f,format, v1,…) formatted reading from file fclose(f) close file

m-files Matlab commands can be put in a textfile with ending.m.m files can be edited with the Matlab Java editor or any other text editor (e.g. xedit ) if the file example.m is in the current directory (pwd, cd) or in the load path, it can be called by typing example Modifying the load path: path, addpath Modular programming: function.m-files

function definitions A function is a name for a particular calculation or subroutine. It can be called be that name, can take arguments and return a value It must be put in a.m-file with same name as function Define function: function returnvariable=name(arguments) … end in separate file square.m: function ret=square(n) % calculate n^2 ret=n*n; end square(3) ans =9 help (square)

Example: print number in binary system file binary.m: function b=binary(num) % return num in binary format as string b=''; x=num; while (x>0) | (strcmp(b,'')) if rem(x,2)==0 b=['0' b]; else b=['1' b]; end x=fix(x/2); end file example.m n=input('Please enter a number:'); disp(binary(n));

Exercise now: convert binary to decimal Now, write a function which takes a binary number in string bin and converts it to a number hints: –you can use a for or a while loop –access i th character of a string s with s(i): s='test'; s(3) 's' For cracks: Do the binary-to-number conversion in one line, without loop! hint: Use all the vector tricks, Use s-48

Solution function r=bintonum(s) % takes binary number s as string,returns integer n=0; i=length(s)-1; for (c=s) n=n+str2num(c)*2^i; i=i-1; end r=n end One-liner: (much faster!!!) r=sum((s-48).*2.^[length(s)-1:-1:0])