EP375 Computational Physics

Similar documents
Basic MATLAB Tutorial

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

Introduction to Engineering gii

Matlab Programming Introduction 1 2

Introduction to MATLAB

An Introduction to MATLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Finding, Starting and Using Matlab

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Digital Image Analysis and Processing CPE

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Welcome to EGR 106 Foundations of Engineering II

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Chapter 1 Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to MATLAB

Chapter 2. MATLAB Basis

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Programming in Mathematics. Mili I. Shah

MATLAB. A Tutorial By. Masood Ejaz

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Numerical Analysis First Term Dr. Selcuk CANKURT

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

McTutorial: A MATLAB Tutorial

Dr Richard Greenaway

Computer Programming in MATLAB

MATLAB QUICK START TUTORIAL

Chapter 3. built in functions help feature elementary math functions data analysis functions random number functions computational limits

ENGR 1181c Midterm Exam 2: Study Guide and Practice Problems

1 Introduction to MATLAB

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

Programming in MATLAB

Consider this m file that creates a file that you can load data into called rain.txt

9 Using Equation Networks

A QUICK INTRODUCTION TO MATLAB

ENGR 1181 Autumn 2015 Final Exam Study Guide and Practice Problems

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

TECH TIP VISION Calibration and Data Acquisition Software

1 Introduction to MATLAB

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Introduction to Matlab

ENGR 1181 Midterm Exam 2: Study Guide and Practice Problems

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Data Types and Basic Calculation

Lecturer: Keyvan Dehmamy

MATLAB TUTORIAL WORKSHEET

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

A Short Introduction to Matlab

Summary of basic C++-commands

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

Quick MATLAB Syntax Guide

Goals for This Lecture:

Dr Richard Greenaway

Programming 1. Script files. help cd Example:

MATLAB BASICS. Macro II. February 25, T.A.: Lucila Berniell. Macro II (PhD - uc3m) MatLab Basics 02/25/ / 15

AMS 27L LAB #1 Winter 2009

Introduction to MATLAB Practical 1

Lab 1 - Worksheet Spring 2013

1 Week 1: Basics of scientific programming I

Practice Reading for Loops

Matlab as a calculator

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

VARIABLES Storing numbers:

To start using Matlab, you only need be concerned with the command window for now.

BEGINNING MATLAB. R.K. Beatson Mathematics Department University of Canterbury. 2 Matlab as a simple matrix calculator 2

Chapter 7: Programming in MATLAB

1 Introduction to Matlab

What is Matlab? The command line Variables Operators Functions

Introduction to Python, Cplex and Gurobi

Python Programming: An Introduction to Computer Science

Introduction to MATLAB

CT 229 Java Syntax Continued

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Laboratory 1 Octave Tutorial

EGR 111 Introduction to MATLAB

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

PART 1 PROGRAMMING WITH MATHLAB

Introduction to MATLAB

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

1.1 ABOUT MATLAB and MATLAB GUI (Graphical User Interface)

C++ Overview. Chapter 1. Chapter 2

Introduction to PartSim and Matlab

LAB 1 General MATLAB Information 1

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Welcome. Please Sign-In

Functions and Inverses ID1050 Quantitative & Qualitative Reasoning

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Stokes Modelling Workshop

A Quick Tutorial on MATLAB. Zeeshan Ali

General MATLAB Information 1

Intrinsic Functions Outline

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

User manual. Version 9.2.0

Transcription:

EP375 Computational Physics Topic 1 MATLAB TUTORIAL BASICS Department of Engineering Physics University of Gaziantep Feb 2014 Sayfa 1

Basic Commands help command get help for a command clear all clears all the memory (workspace) clear x removes variable x from the memory whos lists all the variables (and details) on the workspace Sayfa 2

Semicolon (;) at the will suppress the output Command history: upper & lower arrows, also command name guess If you don t use a variable name, your calculation is labelled and assigned by ans variable. Sayfa 3

Data Types MATLAB data types are classes. Most commonly used types are double char logical All of them are considered by MATLAB as arrays Numerical objects belong to the class double (i.e. double precision array). A scalar is treated as a 1 1 array. Sayfa 4

Sayfa 5

Variables Variable names, which must start with an English Letter. MATLAB is case sensitive: Food and food are different variables There are several built-in constants and special variables: ans Default name for results eps Smallest number for which 1 + eps > 1 inf Infinity NaN Not a number i or j -1 pi π Sayfa 6

>> eps ans = 2.2204e-016 >> pi ans = 3.1416 >> x = 3 + 4i % complex number x = 3.0000 + 4.0000i Sayfa 7

4. Arrays and Matrices Arrays can be constructed in several ways. >> A = [5-3 4 2] A = 5-3 4 2 >> A = [5, -3, 4, 2] A = 5-3 4 2 >> B = [1 2 3; 4 5 6; 7 8 9] B = 1 2 3 4 5 6 7 8 9 >> B = [ 1 2 3 4 5 6 7 8 9 ] B = 1 2 3 4 5 6 7 8 9 Sayfa 8

>> v = [1 2 3] % row vector v = 1 2 3 >> v = [1; 2; 3] % column vector v = 1 2 3 >> v = [1 2 3] % transpose of a row vector v = 1 2 3 Sayfa 9

Strings (Character Arrays) String is a sequence of characters. >> s1 = 'University '; >> s2 = 'of Gaziantep'; >> s3 = strcat(s1,s2); >> s3 s3 = University of Gaziantep >> s1(1:5) ans = Unive >> s2(8:12) ans = antep Sayfa 10

Some Intrinsic Functions Function Description _ abs(x) x sin(x) sine of x (x is in radian) cos(x) tan(x) cosine of x tangent of x sind(x) sine of x (x is in degrees) cosd(x) tand(x) asin(x) acos(x) atan(x) log(x) log10(x) exp(x) cosine of x tangent of x angle in radian from sin 1 (x) angle in radian from cos 1 (x) angle in radian from tan 1 (x) ln(x) log 10 (x) e x mod(x,y) x modulo y (mod(12,5) = 2) Sayfa 11

Input / Output User input >> a = input('birinci sayi: '); birinci sayi: 2 >> b = input('ikinci sayi: '); ikinci sayi: 3 >> c = a + b c = 5 >> p = input('input an array: '); input an array: [1 2 3] >> p' ans = 1 2 3 Sayfa 12

User output Normally MATLAB displays numerical results with about five digits, but this can be changed with the format command: format long format short 16-digit display 5-digit display >> pi % default format ans = 3.1416 >> format long >> pi ans = 3.14159265358979 >> format short >> pi ans = 3.1416 Sayfa 13

A simple way of printing values is to use disp function: disp(value) >> x = 10; >> disp(x); 10 >> y = [1 3 5 7]; >> disp(y) 1 3 5 7 >> disp(y') 1 3 5 7 >> disp('university of Gaziantep') University of Gaziantep Sayfa 14

To print formatted output, one can use fprintf function: fprintf(format, list) format contains formatting specifications list is the list of items to be printed Typical format specifiers are: %wd Integer notation %w.df Floating point notation %w.de Exponential notation \n Newline character (line break) where w is the width of the field and d is the number of digits after the decimal point. Sayfa 15

>> x = 123.456; >> i = 1453; >> fprintf('i = %d ve x = %f\n',i,x) i = 1453 ve x = 123.456000 >> fprintf('i = %7d ve x = %10.2f\n',i,x) i = 1453 ve x = 123.46 Sayfa 16

Arithmetic Operators + Addition 2 +4 = 6 Subtraction 2 4 = 2 * Multiplication 2 * 4 = 8 / Right division 2 / 4 = 0.5 \ Left division (we ll see later) 2 \ 4 = 2 ^ Exponention (x y ) 2 ^ 4 = 16.* Element-wise multiplication./ Element-wise division.^ Element-wise exponention Sayfa 17

>> 3 + 5 % scalar addition ans = 8 >> 3 ^ 5 ans = 243 >> 3 / 5 ans = 0.6000 >> 3 \ 5 ans = 1.6667 Sayfa 18

>> a = [1 2 3]; >> b = [-2 3 4]; >> a + b % vector addition ans = -1 5 7 >> a * b % dot product ans = 16 >> a.*b % element-wise product ans = -2 6 12 Sayfa 19

>> A = [1 2 3; 4 5 6]; >> B = [7 8 9; 0 1 2]; >> A + B % matrix addition ans = 8 10 12 4 6 8 >> A * B % matrix product ans = 50 8 122 17 >> A.*B % element-wise product ans = 7 16 27 0 5 12 Sayfa 20

Comparison Operators < Less than <= Less than or equal to > Greater than >= Greater than or equa to == Equal to ~= Not equal to Logical Operators & AND OR ~ NOT Sayfa 21

Conditional Statements if condition block if condition_1 block_1 elseif condition_2 block_2... else block_n Executes the block of statements if the condition is true. Executes the block_i of if the condition_i is true. Otherwise block_n is executed. Note that the use of else statement is optional. Sayfa 22

>> edit tekmi.m a = input('input a: '); if mod(a,2)==1 fprintf('%d is odd number\n',a) else fprintf('%d is even number\n',a) >> tekmi input a: 6 6 is even number >> Sayfa 23

>> edit quadratic.m % MATLAB script to calculate the % roots of f(x) = a x^2 + b x + c a = input('input a '); b = input('input b '); c = input('input c '); Delta = b*b - 4*a*c; x1 = ( -b - sqrt(delta) )/(2*a); x2 = ( -b + sqrt(delta) )/(2*a); if Delta < 0 disp('two imaginary roots:') x1, x2 elseif Delta > 0 disp('two real roots:') x1, x2 else disp('one real root:') x1 >> quadratic Input a 1 Input b -2 Input c -8 Two real roots: x1 = -2 x2 = 4 >> quadratic Input a 1 Input b -4 Input c 4 One real root: x1 = 2 >> quadratic Input a 1 Input b 2 Input c 3 Two imaginary roots: x1 = -1.0000-1.4142i x2 = -1.0000 + 1.4142i Sayfa 24

Loops while v condition block Executes the block of statements if the condition is true. After execution of the block, condition is evaluated again. If it is still true, the block is executed again. The loop is repeated until the condition becomes false. >> edit loop1.m k = 1; while k<=6 fprintf('%d %d\n',k,k*k); k=k+1; >> loop1 1 1 2 4 3 9 4 16 5 25 6 36 Sayfa 25

for target = sequence block >> edit loop2.m for n = 1:5 y(n) = cos(n*pi/10); disp(y ) >> loop2 0.9511 0.8090 0.5878 0.3090 0.0000 This is equivalent to: >> n = 1:5; >> y = cos(n*pi/10); >> y y = 0.9511 0.8090 0.5878 0.3090 0.0000 Sayfa 26

>> x = 0:0.4:2; >> for i = 1:length(x) fprintf('%4.1f %11.6e\n',x(i),log(x(i))) Warning: Log of zero. 0.0 -Inf 0.4-9.162907e-001 0.8-2.231436e-001 1.2 1.823216e-001 1.6 4.700036e-001 2.0 6.931472e-001 Sayfa 27

Example: Write a program to find and list all integer (x,y) pairs satisfying the condition x + y <=3. >> edit pairs.m for x = -3:3 for y = -3:3 if abs(x)+abs(y) <= 3 fprintf('(%d, %d)\n',x,y) >> pairs (-3, 0) (-2, -1) (-2, 0) (-2, 1) (-1, -2)... Sayfa 28

break and continue Statements Any loop can be exited by a break statement. continue statement allows you to jump to the next iteration. kes.m for x = -5:5 if x == 0 break fprintf('%f %f\n',x,1/x) devam.m for x = -5:5 if x == 0 continue fprintf('%f %f\n',x,1/x) >> kes -5.000000-0.200000-4.000000-0.250000-3.000000-0.333333-2.000000-0.500000-1.000000-1.000000 >> devam -5.000000-0.200000-4.000000-0.250000-3.000000-0.333333-2.000000-0.500000-1.000000-1.000000 1.000000 1.000000 2.000000 0.500000 3.000000 0.333333... Sayfa 29

References [1]. http://www.mathworks.com/products/matlab [2]. Numerical Methods in Engineering with MATLAB, J. Kiusalaas, Cambridge University Press (2005) [3]. Numerical Methods for Engineers, 6th Ed. S.C. Chapra, Mc Graw Hill (2010) Sayfa 30