BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

12
BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01

Transcript of BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Page 1: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

BEH.420 Matlab Tutorial

Bambang Adiwijaya

09/20/01

Page 2: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Starting Matlab

• On athena: – Athena% add matlab– Athena% matlab

• On PC: – Point and click

Page 3: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Matlab basic

• Functions: – Matrix formulation: fast calculation

– Strong in numerical, but weak in analytical

– General purpose math solvers: nonlinear equations, ODEs, PDEs, optimization

• Basic mode of usage:– Interactive mode

– M-script

– Functions

• M-script and Functions must be written in separate files

Page 4: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Basic Syntax

• Case sensitive variable name

• Ending a statement with a “;”

• Vector: Vec(i)

• Matrix: Mat(i,j,…)

• Element by element matrix operations: – “.*, ./, .^2”

• General matrix operations:– Cross product (*)

• Looping in matlab: for I = 1:N,

for J = 1:N,

A(I,J) = 1/(I+J-1);

end

end

• If statement:if I == J

A(I,J) = 2;

elseif abs(I-J) == 1

A(I,J) = -1;

else

A(I,J) = 0;

end

Page 5: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Basic Matlab Commands

Matlab commands Functions and descriptions

help functionname Matlab on-line help for functions

lookfor searchphrase To find matlab function with descriptions containing the search phrase

who To list all variables currently used

size(matrix) To identify the dimensionality of the matrix

ones(m,n) To create a unit matrix of size m x n

print –depsc filename.ps To print an active plot (later use lpr to print in athena)

Page 6: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Function

• A function must be created under a separate file• Function name and filename should be the same• Example:

function [res] = no1fun(x,p1); res(1)=x(1)*x(2)-p1; res(2)=x(1)-2*x(2);

Page 7: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Plotting

• figure; %to create a new popup window• plot(X,Y,'c+:‘); semilogx(X,Y,’bx-’);• subplot(row,col,figno), title([‘example title’]);• ylabel(‘temp’); xlabel(‘conc’);• axis([minX maxX minY maxY]);• Example:a=(1:10); b=a.^2;c=a.^0.5;figure;subplot(2,1,1), semilogx(a,b,’bx-’);subplot(2,1,2), plot(a,c, ‘rs:’);

Page 8: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Fsolve: solving nonlinear equations

• FSOLVE solves equations of the form:F(X)=0 where F and X may be vectors or matrices.

• Syntax: X=fsolve('FUN',X0,OPTIONS, P1,P2,...)

• Problem:Solve:

x(1)*x(2)-3=0

x(1)-2*x(2)=0

Page 9: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

ODE solvers: solving ordinary differential equations

• ODE23, ODE45, ODE113, ODE15S, ODE23S– ODE45: Runge Kutta (4,5) formula, best first try function

– ODE23: Runge Kutta (2,3) formula.

– ODE113: variable order Adams-Bashforth-Moulton PECE solver. More efficient when the function evaluation is expensive.

• Stiff vs non-stiff ODEs

• Function: solving y’=F(t,y).

• ODE file must be in the form of: dydt=F(t,y,flag,p)

• Syntax: [T,Y] = ode45('F',TSPAN,Y0,OPTIONS,P1,P2,...)

Page 10: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

ODE solvers: how to solve higher order ODE?

• Example: how to solve: y’’+2y’+3=0?

• Answer: make substitution.Y1=y

Y2=y’

New formulation:Y1’=Y2

Y2’=-2Y2-3

Page 11: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Curvefitting

• CURVEFIT solves problems of the form: min sum {(FUN(X,XDATA)-YDATA).^2} where FUN, XDATA

and YDATA are X vectors.

• Syntax:X=curvefit('FUN',X,XDATA,YDATA,OPTIONS,'GRADFUN',P1,P2,..)

• Examples:Fit the following data into the following model and regress x

parameters:ydata=p*x(1)+x(2)*exp(-xdata);

Page 12: BEH.420 Matlab Tutorial Bambang Adiwijaya 09/20/01.

Importing data

• Textread, dlmreadDLMREAD read ASCII delimited file.

M = dlmread(FILENAME,DLM)

TEXTREAD read text file in a certain format

[A B] = textread(‘datafile','%f %f');