You are on page 1of 32

MMA307 lecture 2: Introduction to (Matlab) Programming

MMA307 lecture 2: Introduction to (Matlab) Programming


Jan 24, 2014

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

Division of Applied Mathematics Mlardalen University


1

Contents of todays Lecture

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

1 Introduction to Matlab

Getting started The command window (the command prompt)

Numerical differentiation

2 Numerical differentiation

Coming next in this course

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab programming; numerical differentiation II (lec 3) Nonlinear equations (lec 4,5) Simultaneous linear equations (lec. 6) Polynomial approximation and interpolation (lec. 7, 8) Curve tting (regression, spline interpolation) (lec. 7, 8) Introduction to nancial mathematics (using MATLAB) (lec 9) Numerical integration (lec 10) Ordinary differential equations (lec 11)

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

What is Matlab

MMA307 lecture 2: Introduction to (Matlab) Programming

Mathworks-www.mathworks.com Short for Matrix Laboratory Software package for numerical computing and programming Used throughout the engineering industry Flexible (can extend it with different toolbox for almost all areas in applied math and engineering) easy to program, relieves you of repetitive tasks Available in computer labs-MATLAB R2012b Student version available (not free but not so expensive) Getting help-internet
Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

Let us get started

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab

Desktop windows
Start MATLAB. How many windows there are by default? Take a guess of their usages?

Getting started The command window (the command prompt)

Numerical differentiation

Getting help
In the command window, type help log What happens? On the menu bar, click "Help-Examples, take a look at the output.

Using the command prompt

MMA307 lecture 2: Introduction to (Matlab) Programming

Command window is like a giant calculator, except that it can do more than a calculator. The following concepts will be illustrated.

Introduction to Matlab
Getting started The command window (the command prompt)

Arithmetic operations Built-in functions Assignment statements-creating variables Creating matrices creating vectors Matrix operations How to plot

Numerical differentiation

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Using the command prompt

Numerical differentiation

Part I- Basic arithmetic, Matlab works like a powerful calculator Type the following commands (one line at a time) in Matlab command window, try to interpret the corresponding output.

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab 2+3*4 (2+3) *4 % use the up-arrow key log(10) Numerical pi % What is the precision? differentiation format long pi format short % What is the default format? pi 2^16 2^(-3) sqrt(4) % sqrt(arguments) is a build-in function nthroot(8, 3) % Separate the arguments by comma exp(1) % constant e % open the workspace window, how many variables are there? clc % How many variables now?
Getting started

The command window (the command prompt)

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab

Review-Questions:

Getting started The command window (the command prompt)

How do we write comments in Matlab? What is variable ans What is the function for computing the base 10 log? What does the command clc do?

Numerical differentiation

Part 2- Build-in functions

MMA307 lecture 2: Introduction to (Matlab) Programming

Type the following commands (one line at a time) in Matlab command window, try to interpret the corresponding output.

Introduction to Matlab
Getting started The command window (the command prompt)

abs(-3) 3*cos(sqrt(4.7)) exp(3) log(exp(3)) floor(4.57) round(4.57)


A list of commands can be found here:

Numerical differentiation

www.mathworks.se/help/matlab/functionlist.html

10

Part 3-Creating variables Type the following commands (one line at a time) in Matlab command window, try to interpret the corresponding output.

MMA307 lecture 2: Introduction to (Matlab) Programming

a= 3-floor(exp(2.9)) Introduction to b= sin(a); %What is the use of semi-colon ";"? Matlab b 2*b^2 Numerical differentiation 2*b^2;
Getting started

The command window (the command prompt)

x= 2; y=3; z=x*y averageValue= (x+y)/2 averageValue2= (x+z)/2 %x-z = y % illegal %2AverageValue = (x+y+z)/2 % illegal height = 2; width = 3; area = height*width

11

Part 3-Creating variables

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab

continued..

Getting started The command window (the command prompt)

%How many variables are there now? who clc who clear who

Numerical differentiation

12

Part 3-Creating variables

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab

Questions:

Getting started The command window (the command prompt)

How to suppress the output? How to see the list of the variables in the workspace? What is the assignment operator? What is an illegal variable name? What is the difference between commands "clear" and "clc"?

Numerical differentiation

13

Part 4- Matrices and Vectors

a1=[1 2 3 4] % row vector a2=[1; 2; 3; 4] % col vector a1 % transpose Introduction to a1 Matlab c = 2: 5; d = 2:0.2:5; d Numerical e=linspace(2, 5, 16) differentiation e a1= [ 1 2 3 4] %row vector a2=[1; 2; 3;4] % column vector aTrans = a % transpose a c= 2:5; % the colon operator d = 2:0.2:5; % If you know the increment e = linspace(2, 5, 16) %If you know the number of values f = linspace (2, 5, 50); %double click "f" in workspace window % remember to supress the output, % it can take time to print to the window %Matrices 0:0.001:100000;
Getting started

MMA307 lecture 2: Introduction to (Matlab) Programming

The command window (the command prompt)

14

Part 4- Matrices and Vectors

MMA307 lecture 2: Introduction to (Matlab) Programming

A =[1 2 3; 4 5 6; 7 8 9 ] A B=[0:0.5:1; 4:0.5: 5] ones(3,3) zeros(3,3) B = A A*B % what kind of multiplication is it? A.*B %for element-wise operation A^2 A.^2 A/B % error A./B %subsets A(2,3) A(:, 3) A(3, :) A(3, 1:2)

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

15

The colon operator

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

http://www.mathworks.se/help/matlab/ref/colon.html

16

The "nd" function

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

http://www.mathworks.se/help/matlab/ref/nd.html

17

Part 5- Visualizing data Simple and powerful function plot.

MMA307 lecture 2: Introduction to (Matlab) Programming

x = -5:0.1:5 %independent variable/horizontal axis y = 2.*x +5 % dependent variable y= 2x+5 Introduction to plot(x,y) % can also select in work space and righ click Matlab grid on y2= x.^2-5; %What happens if you type y2=x^2-5 plot(x,y2) Numerical xlabel(x) differentiation ylabel(x^2-5) plot(x,y2,r) help plot plot(x,y2,r--) plot(x,y2,+) %doc LineSpec grid on axis square % make square picture axis equal % spacing is equal for y-axis and x-axis %you can put whatever limit you want axis([0 5 -5 10]) %you zoom in this part xlim([-5 5]) %open the property editor for the plot if you like GUI
Getting started

The command window (the command prompt)

18

Part 5- Visualizing data Simple and powerful function plot.

MMA307 lecture 2: Introduction to (Matlab) Programming

%more than one plot plot(x,y, x, y2) %alternative 1 plot(x,y) Introduction to Matlab hold on plot(x,y2, r) y3=x.^3; Numerical plot(x, y3, k) differentiation grid on axis([-5 5 -2 2]) axis tight t= 0:pi/20:2*pi; y1 = sin(t); %redefine y1,y2, y3 y2= sin(t-pi/2); y3= sin(t-pi); plot(t, y1, --r) hold on plot(t, y2, :b) plot(t, y3,*k) xlabel(t) legend(sin(t),sin(t-pi/2),sin(t-pi)) legend(sin(t),sin(t-\pi/2),sin(t-\pi)) %using LaTex
Getting started

The command window (the command prompt)

19

Part 5- Visualizing data

MMA307 lecture 2: Introduction to (Matlab) Programming

Simple and powerful function plot.


%multiple plots x= -5:0.01:5; y1= x; y2 = x.^2; y3 = x.^3; y4 = x.^4; subplot(2,2,1); subplot(2,2,2); subplot(2,2,3); subplot(2,2,4); y5= sin(x) plot(x,y5) figure; plot(x,y5) the same window (different axis)
Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

plot(x,y1) plot(x,y2) plot(x,y3) plot(x,y4)

20

Writing your own m-les

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Therere two types of m-les, both are .m les.

Numerical differentiation

Scripts. a sequence of MATLAB statements. User-dened functions

21

Functions

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

m-les name should have the same name as the function name

Numerical differentiation

function c=a+b

[c]

= addTwoNumber(a, b)

Save this le as addTwoNumber.m

22

In-Class exercise1

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Plot the following function in Matlab.


y = 1000(1 + x )2 + 1000(1 + x ) + 1000

Numerical differentiation

23

In-Class exercise2

MMA307 lecture 2: Introduction to (Matlab) Programming

Plot the following functions on x [0, 0.25] using the same axes in Matlab. y = (1 + x )10 x y = (1 + )30 3 x 100 y = (1 + ) 10 y = exp(10x )

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

24

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Let us forget about MATLAB for a while and come back to the theory of numerical methods.

Numerical differentiation

25

Forward-divided difference Scheme for nding f (x ). Denition of f (x ) f (x ) = lim f (x + x ) f (x )

MMA307 lecture 2: Introduction to (Matlab) Programming

x 0

Introduction to Matlab
Getting started The command window (the command prompt)

Forwarded-divided difference (FDD) f (x ) f (x + x ) f (x )

Numerical differentiation

Example: Given f (x ) = 2e1.5x , nd f (3) =?, use x = 0.1. f (3) f (3 + 0.1) f (3) 0.1

2e1.53.1 2e1.53 0.1

= 291.36

Question: What is the exact value of f (3)? What is the absolute and relative error of this approximation? (Use MATLAB to compute) f (3) 291.36(x = 0.1); f (3) = 270.05(x 0)
26

Backward-divided difference

MMA307 lecture 2: Introduction to (Matlab) Programming

Denition of f (x ) f (x ) = lim f (x + x ) f (x )
Introduction to Matlab
Getting started The command window (the command prompt)

x 0

Backward-divided difference (BDD) f (x ) f (x ) f (x x )

Numerical differentiation

Example: Given f (x ) = 2e1.5x , nd f (3) =? using BDD, use x = 0.1. Compute also the absolute and relative errors. f (3) 250.77(x = 0.1); f (3) = 270.05(x 0)

27

Central-divided difference

MMA307 lecture 2: Introduction to (Matlab) Programming

The mean of FDD and BDD is f (x ) = 1 2 f (x + x ) f (x )

f (x ) f (x x )

Introduction to Matlab
Getting started The command window (the command prompt)

Central-divided difference (CDD) f (x ) f (x + x ) f (x x ) 2 x

Numerical differentiation

Example: Given f (x ) = 2e1.5x , nd f (3) =? using CDD, use x = 0.1.Compute also the absolute and relative errors. f (3) 271.06(x = 0.1); f (3) = 270.05(x 0)

28

More on numerical differentiation

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab
Getting started The command window (the command prompt)

Accuracy of FDD, BDD and CDD. High-order numerical differentiation

Numerical differentiation

29

Homework2-to be continued

MMA307 lecture 2: Introduction to (Matlab) Programming

Introduction to Matlab

Write a Matlab function that takes in a true value and its approximation and returns the relative error (Rp ) as input. Given f (x ) = 2e1.5x , nd f (3) =? using FDD, BDD and CDD methods and use x = 0.1, x = 0.05 and x = 0.025. Compute and absolute errors and relative errors for each approximation. List your results in a table form. (This problem will be analyzed in next lecture, so please do it immediately).

Getting started The command window (the command prompt)

Numerical differentiation

30

Homework2-continued

MMA307 lecture 2: Introduction to (Matlab) Programming

Using help: use the help menu or help rand at the command prompt to nd out what the (build-in) function rand does. Use rand to create a 5-element vector. Repeat it a few times. Use rand to create a 500-element vector, call it r . Suppress the output. use the help menu or help rand at the command prompt to nd out what the (build-in) function stem do. Use stem to display in a spike plot the vector r . What is the 50th element of r? What happens if you type in r(0) or r(501)? Why? Select a subvector s which contains the 5th and the 50th element of vector r .

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

31

VG-exercise-due on 27th Jan at 13.15


Denition

MMA307 lecture 2: Introduction to (Matlab) Programming

is said to approximate p to d (Denition 1.8)The number p signicant digits if d is the largest nonnegative integer for which | 101d |p p < . |p| 2
Write a Matlab function that takes the exact value and the approximated value as inputs and returns the corresponding d as the output. Test your program on the following exercise. Exercises: Determine the number of signicant digits for the approximations.

Introduction to Matlab
Getting started The command window (the command prompt)

Numerical differentiation

x = 3.141592 and x = 3.14. y = 1, 000, 000 and y = 999, 996. z = 0.000012 and z = 0.000009.
32

You might also like