MATLAB "Mathematical Laboratory" Designed for numerical computation

Preview:

Citation preview

MATLAB

"Mathematical Laboratory"Designed for numerical computation

Getting Started with MATLAB

Start SoftwareStart typing… No compiling

MATLAB Advantages I

Underlying data structure is a matrix common operations on matrices are often

one line long

Built for scientific work libraries include wide range of useful

mathematical and scientific functions

Can be used interactively easy to experiment, little programming

overhead

MATLAB Advantages II

Active developer community cookbooks, on-line help

Add-ons for simulation, electronic measurement, domain-tailored modelsGraphics built-inFairly cheap as a student/researcher $200 for research license through Queen's $5,000 commercially!

Disadvantages

Commercial (not universally available)Not compiled hand-made loops are slower often negated by fast matrix operations

Slow to start, bulkyHard to integrated unit tests as tightly as in Java, C

What it can do

The basic calcs for the beam distortion: function w = computeEulerW(L, q, k) N = 10000; dx = L/N; w4 = k*q * dx * ones(N, 1); % do integration w3 = cumsum(w4);

Beam model calculations can all be done in 4 lines, plus setup code.

How to do it

Create vectors x = [1 2 3 4 5]

y = 3:10

z = 0:2:20

w = linspace(0, 100, 5000)

Create Matrices

a = ones(10, 5)

b = zeros(3, 3)

c = eye(5,3)

Modify Matrices

a * 3

c'

a(1, 1) = 4

a(2, 3) = 9

a(:, 2) = 7

Combining matrices

m1 = ones(3, 5)m2 = 3*ones(3, 5)m1 + m2

m1 * m2

m1 .* m2

m1 * m2'

Other operations

m2^2

m2.^2

Plotting

x = linspace(-5, 5, 1000);y = exp(- x.^2); % why .^2?plot(x, y);

x = -5:5;y = exp(-x.^2); plot(x, y);

% What does plot do with the coords% in x and y?

More complex operations

Eigenvalues d = diag([1, 3, 6]) eig(d) [v1, v2] = eig(d)

sum(d)

Functions

Create a new squareroot.m file function y = squareroot(x) y = sqrt(x);

Run it in MATLAB x = linspace(0, 100); y = squareroot(x); plo(x, y)

Functions in ODE solving

Newton's law of Heating and cooling DE is dy/dt = -k (y – T)

In heat_de.m function dy_dt = heat_de(t, y) k = 0.05; T = -5; % const, ext. temp dy_dt = -k * (y – T);

Solve in MATLAB ode45(@heat_de, [0, 72], 23);

Resources

MATLAB Help <F1> in MATLAB Has full description and examples for all functions

MathWorks website (Developers of MATLAB) http://www.mathworks.com/

On-line tutorial http://www.mathworks.com/academia/student_center/

tutorials/index.html?BB=1

Before next Tuesday

If you have a Windows laptop, and a ~2 gigs of space, install a complete version of Cygwin

Recommended