Applied Numerical Methods
OVERVIEW OF MATLAB
MATLAB is a powerful computing platform that has many useful built-in features. In this course, we use only a few of these features. Here is an overview.
- Getting Help. The help pages in MATLAB are very useful – they are actually comprehensible, and have clear examples. Click on Help in the upper left, or type helpwin command in the Command Window. Examples:
>helpwin plot
>helpwin fprintf
- Assigning and viewing variables. The equal sign is used to assign a value to a variable:
> x = pi^2
x =
9.869604401089358e+000
If you want the output suppressed (you don’t want to see most of your intermediate calculations), put a semicolon at the end of the statement. Once a value is assigned, you can display it in 2 ways: unformatted and formatted. For an unformatted display, simply type the name of the variable, as below. The format of the print is determined by the format you have specified in Preferences.
> x
x =
9.869604401089358e+000
Use the fprintf command for formatted displays. The command requires a formatting string containing formatting commands and a list of variables to print:
> fprintf(' %15.6f %15.6f \n',x,x^2)
9.869604 97.409091
> fprintf(' %15.6e %15.6e \n',x,x^2)
9.869604e+000 9.740909e+001
The \n is for a carriage return, so that the next print statement goes to the next line.
CHANGING THE DISPLAY DOES NOT CHANGE THE STORED VALUE OF THE VARIABLE.
Commonly used symbols:
√xsqrt(x)e, e2exp(1), exp(2)10-6, 2×10-61e-6, 2e-6
ln 17log(17)log(17) log10(17)πpi
tan-1(x) atan(x)
- Vectors and matrices. MATLAB treats all variables as if they were matrices (a scalar is a 1x1 matrix, for example), and hence it does linear algebra operations very easily. Sometimes, though, you don’t want to treat a variable as a matrix, you want to treat it as a set of scalars. As an example, consider the code below, showing a complicated computation on a set of x values. Instead of using *, / and ^ (which are matrix operators), MATLAB has scalar operators called .*, ./ and .^
> x = [2 4 7]
x =
2 4 7
> y = (1+x.^3) ./ (x.*sin(x))
y =
4.948875766325774e+000 -2.147191651817716e+001 7.480039507456047e+001
To understand the difference between * (matrix multiplication) and .* (scalar multiplication), consider the following example:
> A = [1 2; 3 4]
A =
1 2
3 4
> A*A
ans =
7 10
15 22
> A.*A
ans =
1 4
9 16
A common task for us is to specify a uniform grid of x values on an interval, with a spacing of h between points. The x values are labeled x_0 through x_n, with the left endpoint x_0=a and the right endpoint x_n = b. MATLAB can set up the vector quite easily:
> a = 0, b=4, n=5
a =
0
b =
4
n =
5
> h = (b-a)/n
h =
8.000000000000000e-001
> x=a:h:b
x =
Columns 1 through 3
0 8.000000000000000e-001 1.600000000000000e+000
Columns 4 through 6
2.400000000000000e+000 3.200000000000000e+000 4.000000000000000e+000
The syntax x=a:h:b means the starting point is a, the increment is h, and the ending point is b. If the increment is not included, it is assumed to be 1. This syntax is used in loops.
> z = 3:7
z =
3 4 5 6 7
- Defining functions. The easiest way to define a function is to use ‘anonymous functions’, as follows:
> f = @(x) sin(x) - cos(x)
f =
@(x) sin(x) - cos(x)
> g = @(x,y) exp(x*y) - 2*x + cos(y^2)
g =
@(x,y) exp(x*y) - 2*x + cos(y^2)
To evaluate the functions, use the standard notation:
> f(2),g(1,3)
ans =
1.325444263372824e+000
ans =
1.717440666130299e+001
The name of the function (f and g) here is called the handle, and must be different from all the other variables you are using.
An alternate approach is to create a function file and evaluate using the feval command. Create the file and give the file the same name as the function.
File name: fhw1.m
File contents:
function y = fhw1(x)
y = sin(x).*exp(-x);
To evaluate the function at x=3.6, type
>fhw1(3.6)
or
> feval(‘fhw1’,3.6)
- Plotting functions. We plot functions often in this class. The plot command is very powerful (see helpwin plot for details), and you should read the help page, by typing >helpwin plot. You input an x vector, a y vector of the same size and an optional set of formatting commands (color, line style and symbol). To create the y vector, you can create an anonymous function, or more commonly, just create the vector using the dot commands:
> x=0:.01:5; y = sin(x).*exp(-x); plot(x,y,'--')
or use
>f = @(x) sin(x).*exp(-x);
>x=0:.01:5; y = f(x); plot(x,y, '--')
or
>x=0:.01:5; plot(x,f(x), '--')
or
>x=0:.01:5; y = feval(‘fhw1’,x); plot(x,y, '--')
- Printing numbers. All numbers in MATLAB are stored in double precision (15-16 decimal digits), but can be printed in many different ways and in different formats. First, the command window display is specified in File/Preferences/Command Window/Numeric Format. This format applies to printing that doesn’t use fprintf:
>x = pi;
>x
>disp(x)
In fprintf, the symbol d is used to print integers, and e (scientific notation), f (floating point) and g (MATLAB chooses the better form) are used to print real numbers.
>fprintf(‘pi equals %15.10e \n’,x)
>fprintf(‘pi equals %15.3e \n’,x)
>fprintf(‘pi equals %15.10f \n’,x)
>fprintf(‘pi equals %15.3f \n’,x)
>fprintf(‘pi equals %15.10g \n’,x)
>fprintf(‘pi equals %15.3g \n’,x)
>fprintf(‘pi equals %15d \n’,x)
- Computer programs and file structure. A computer program in MATLAB is called a script file (in other languages, it is called the main code or driver). It is a set of commands to be executed in order. Generally the script file is created in MATLAB’s editor and saved, so that it is easy to modify and so that you have an ongoing record of what you’ve worked on. To launch the editor, click on the white page icon in the upper left corner.
Often, we also need to have auxiliary files, called subprograms. In other languages, there are subroutines and functions, which can be in the same file as the main code or in different files. In MATLAB, there are only functions and they must be in different files (the name of the function should be the name of the file). Here is a sample script file to estimate the derivative of a function. The file is saved as sample.m:
% sample.m – illustrate the use of a function file
f = @(x) x.^2;
x0 = 3;
h = .1;
df = fordiff(f,x0,h);
fprintf(‘The derivative at %6.2f is %15.10f \n’,x0,df)
The function fordiff is saved in fordiff.m:
% fordiff.m – the forward difference approximation for f’(x0)
function y = fordiff(f,x,h)
y = ( f(x+h) – f(x) ) / h;
The function line must follow a fixed format. The output variable, or variables appear on the left, and all the inputs appear in parentheses. If there is more than one output, the syntax is
function [a,b,c] = myfcn(x,y,z)
It is also possible to define simple functions in a script file. For example,
>f = @(x) x.^2;
>fordiff = @(x,h) (f(x+h)-f(x))/h;
>fordiff(3,.1)
>fordiff(3,.01)
>fordiff(3,.001)