MMA307 Lecture 2: Introduction To (Matlab) Programming
MMA307 Lecture 2: Introduction To (Matlab) Programming
Introduction to Matlab
Getting started The command window (the command prompt)
Numerical differentiation
Introduction to Matlab
Getting started The command window (the command prompt)
1 Introduction to Matlab
Numerical differentiation
2 Numerical differentiation
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
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
Introduction to Matlab
Desktop windows
Start MATLAB. How many windows there are by default? Take a guess of their usages?
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.
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
Introduction to Matlab
Getting started The command window (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.
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
Introduction to Matlab
Review-Questions:
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
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)
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.
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
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
Introduction to Matlab
continued..
%How many variables are there now? who clc who clear who
Numerical differentiation
12
Introduction to Matlab
Questions:
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
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
14
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
Introduction to Matlab
Getting started The command window (the command prompt)
Numerical differentiation
http://www.mathworks.se/help/matlab/ref/colon.html
16
Introduction to Matlab
Getting started The command window (the command prompt)
Numerical differentiation
http://www.mathworks.se/help/matlab/ref/nd.html
17
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
18
%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
19
Numerical differentiation
20
Introduction to Matlab
Getting started The command window (the command prompt)
Numerical differentiation
21
Functions
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)
22
In-Class exercise1
Introduction to Matlab
Getting started The command window (the command prompt)
Numerical differentiation
23
In-Class exercise2
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
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
x 0
Introduction to Matlab
Getting started The command window (the command prompt)
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
= 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
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
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
f (x ) f (x x )
Introduction to Matlab
Getting started The command window (the command prompt)
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
Introduction to Matlab
Getting started The command window (the command prompt)
Numerical differentiation
29
Homework2-to be continued
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).
Numerical differentiation
30
Homework2-continued
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
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