Anda di halaman 1dari 14

( Lecture II : Programming in MATLAB )

Sholihun, M.Sc.

MATLAB
Command Window: - running program - finding help M-File: a place to create a program - symbolic programming - numerical programming GUI: an interface to make an user-friendly program - usually used to make a simulation
2

Command Window (CW)


Some commands in CW: cd : to know where we are doing ls or ;dir : to know what files in our current directory delete, open, copy, etc.

Note: we can do all commands above just by using mouse like we usually do, but it is unique if we can use those commands

Other commands: clc : to clear something written in CW clear or clear all : to clear memory recorded syms x y z : to define variables Some functions provided by MATLAB: sinusoidal: sin(x), cos(x), tan(x), sec(x), csc(x),cot(x). Their invers: asin(x), acos(x), etc. Other functions: exp(x), log(x) = ln or natural logaritmic, log10(x) = logaritmic, etc.
3

How to Create Program in M-File ?


M-File is a place where we create a program either symbolical program or numerical one Symbolical program means a program which contains theoretical algorithm avoiding an approximation or a numerical method for certain purpose This type of programming is usually used to solve simple problems It needs long time to run, when our problem is complex, even a computer can do nothing (trouble) because of its hard calculation It is difficult to be used to solve a complex system Numerical program is a program which contains numerical algorithm with a certain method and we usually use it to solve our complex problem in physics We can solve all problems we have either simple or complex by designing or choosing curtain numerical method this is OUR GOAL!
4

How to Create Program in M-File ?


Which method we will use? It depends on our problem we face: > root finding: Newton-Raphson, Bisection, Secant, Broyden, etc. > integrating: Trapezoid, Simpson, etc. > differential: Euler, Runge-Kutta, Finite Difference, CrankNicolson, Finite Element, etc. Which software or programming language we should use? It depends on our needs as long as we learn a basic programming By understanding a basic programming, we will be able to make a program we want to find solutions of our problem

M.Sc. Physics

Programming Logic
Loop for : Example 1: the constant pi can be approximated by:

Logic if : its format is given below:

Example 2: create a program to calculate the function follow:

Solution:

M.Sc. Physics

Example 3: calculate the logarithmic function using this approximation:

To make a program to solve this problem, follow these steps: Firstly, we separate the equation to be positive and negative series: Positive series: Negative series:

M.Sc. Physics

10

Secondly, write codes for each series:

11

Thirdly, combine those codes to become complete program:

M.Sc. Physics

12

(6)
(1)

(2)

M.Sc. Physics

13

M.Sc. Physics

14

Anda mungkin juga menyukai