Bez tytulu slajdu - PowerPoint PPT Presentation

1 / 157
About This Presentation
Title:

Bez tytulu slajdu

Description:

Matlab uses a row vector of coefficients to represent a polynomial. Many ... x=ceil(100*rand(1)); disp(sprintf('%d is ',x)) switch rem(x,2) case 0, disp('EVEN' ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 158
Provided by: SB789
Category:
Tags: bez | ceil | slajdu | tytulu

less

Transcript and Presenter's Notes

Title: Bez tytulu slajdu


1
(No Transcript)
2
(No Transcript)
3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
(No Transcript)
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
A First Example
  • Using the Interactive or Calculator Mode

gtgt tlinspace(0,2,100) gtgt foft-t.exp(-4t.2)
gtgt plot(t,foft) gtgt xlabel('t') gtgt
ylabel('f(t)') gtgt title('Example Plot')
Sequential, Interpreted, Variable-Based,
Algebraic, Graphic,...
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
(No Transcript)
32
(No Transcript)
33
(No Transcript)
34
(No Transcript)
35
(No Transcript)
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
(No Transcript)
49
Polynomial Manipulation
  • Matlab uses a row vector of coefficients to
    represent a polynomial
  • Many functions to apply...
  • roots()
  • poly()
  • conv()
  • , -
  • polyval()
  • lookfor polynomial

p(s)(s1)(s-1)(s-2) s3-2s2-s2
prts-1 1 2 ppoly(prts) p 1 -2
-1 2 polyval(p,prts) ans 0 0
0 slinspace(-2,3,100) pofspolyval(p,s)
plot(s,pofs)
50
(No Transcript)
51
(No Transcript)
52
(No Transcript)
53
(No Transcript)
54
(No Transcript)
55
(No Transcript)
56
Curves in 3-D
  • 3-D "Static" Curves3-D "Animation"

z00.15(2pi) fexp(jz) xreal(f)
yimag(f) figure(1),plot3(x,y,z)
figure(2),comet3(x,y,z)
57
Surfaces in 3-D
  • Simple
  • Nifty
  • Really Cool

X,Y,Zpeaks(25) figure(gcf),mesh(X,Y,Z)
figure(gcf),surf(X,Y,Z)
figure(gcf),surfl(X,Y,Z) shading('interp')
colormap('copper')
58
A Foundation for Formulation of MATLAB 3-D Plots
  • The Traditional Approach is to Create A Data Grid
    (Uniformly Sampling)

x-pi0.1pi yx X,Ymeshgrid(x,y)
Zcos(X).sin(Y) surfl(X,Y,Z) shading
interp colormap bone rotate3d on
59
Visualizing Numerical Solutions of the PDEs
(ODEs, etc)
  • Traditional 2-D Plots
  • 3-D Possibilities

60
(No Transcript)
61
(No Transcript)
62
(No Transcript)
63
Summary of Basic Commands
64
(No Transcript)
65
(No Transcript)
66
(No Transcript)
67
(No Transcript)
68
(No Transcript)
69
(No Transcript)
70
Basic Looping
  • The for loop is an old friend?
  • Columnar assignment at initialization and at each
    loop.
  • Large collections of these can be very, very
    slow.
  • A clever concept, the empty matrix?

sum0 for n110 sumsumn end sum
sumzeros(3,1) for nrand(3,5)
sumsumn end sum
net for n110 netnet n2 end net
71
Win the Prize, "Matricize"(now that I've shown
you some bad habits...)
  • A large composition of for loops will run much
    slower than matrix algebraic formulations

x for a120, tmp for b110
tmptmp a end xx tmp end x
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
3 3 3 3 3 ... 9 9 9 9 9 9 9 9 9 9 ...
x120'ones(1,10)
72
(No Transcript)
73
(No Transcript)
74
More Basic Looping
  • The while loop is a favorite construct for
    iteration.

sum0 n1 while abs(1-sum)gteps
sumsum(1/2)n nn1 end sum, n
75
(No Transcript)
76
Basic Conditional Tests
  • The conditional if should be familiar
  • Beware of the nesting construct, end terminates
    the most inner test

xrand(1) if xgt0.5 disp('Larger') else
disp('Smaller') end
xrand(1) if xgt0.5 disp('Larger') elseif
xgt0.25 Versus else if disp('Medium') else
disp('Smaller') end
77
(No Transcript)
78
(No Transcript)
79
(No Transcript)
80
Advanced Conditional Tests
  • You've never tested data like this before!
  • switch/case is similar to other language
    implementations.

xrand(1,100) if any(xgt0.5)
x(xgt0.5)0.5 end plot(x)
xceil(100rand(1)) disp(sprintf('d is
',x)) switch rem(x,2) case 0,
disp('EVEN') case 1, disp('ODD')
otherwise, disp('VERY ODD') end
xrand(1,100) if any(xlt0.3)
x(xlt0.3) end plot(x)
81
Summary of Basic Control Commands
82
(No Transcript)
83
(No Transcript)
84
Extending the First Example
  • Putting Your Work Into A MATLAB Script

MYFILE Example script tlinspace(0,2,100) foft
-t.exp(-4t.2) figure(gcf) plot(t,foft) xlabel(
't') ylabel('f(t)') title('Example Plot') End
of myfile.m
gtgt myfile
Extensible, Seamless Graphics, ...
85
(No Transcript)
86
Review Basics of Function M-Files
  • The all important interface line
  • Match name of function with that of m-file (VERY
    STRONGLY SUGGESTED)
  • Fully understand whether vectorization is
    desired (the dot)

function thevalfname(x) FNAME Function
m-file thevalfname(x) theval1./x
Function m-file fname.m
87
Some Numeric Tasks
  • Plotting of a function can be automated
  • Numerical integration
  • Optimization (zeros, local minima)

ezplot(1/x) fplot(fname,0.1
10) quad(fname,1,10) log(10) fmin(fname,
1,10)
88
Summary of FunFun Functions
89
(No Transcript)
90
Taking the First Example Farther
  • Putting Your Work Into A MATLAB Function

function foftmyfunc(t) MYFUNC Example
function foft-t.exp(-4t.2) figure(gcf) plot(t
,foft) xlabel('t') ylabel('f(t)') title('Example
Plot') End of myfunc.m
gtgt myfunc( 00.011 )
Fully Structured Language, (selection and
repetition structures also) .
91
Applying Some Numerical Tools
  • Standard MATLAB Toolbox Functions

fminbnd('myfunc',0,1) ans 0.3535
fzero('myfunc',0.1) ans -2.1156e-19
quad('myfunc',0,2) ans -0.1250
Accurate, Efficient Numerical Methods, ...
92
The Typical MATLAB Project
  • A Mix of Script and Function m-files, Figures and
    Data

RUNFILE Script m-file to command the
overall modeling and solution process ...
function resafunca(p1,p2) FUNCA Model or
utility function ...
function resb1,resb2funcb(p1,p2,p3) FUNCB
Model or utility function ...
... Input and/or output data in text, binary,
worksheet or other forms...
Simple Text Files, No Compiler to Deal With,
Rapid Modeling, ...
93
(No Transcript)
94
(No Transcript)
95
(No Transcript)
96
Systems of Linear Equations
  • Formulate in Linear Algebraic Terms

6x14x2-3x312 5x1-10x2x38 x112x2-4x31
A6 4 -3 5 -10 1 1 12 -4 b12 8 1
xinv(A)b x 3.5714 1.5000 5.1429
  • Other More Robust Methods

A6 4 -3 5 -10 1 b12 8
xpinv(A)b x 1.7182 0.0032 -0.5593
6x14x2-3x312 5x1-10x2x38
LINPACK and EISPACK are at your disposal
97
ODEs (Initial Value Problems)
  • Formulate as a Set of Coupled First Order ODEs

d2v/dt2(R/L)(dv/dt)(1/LC)v(1/LC)sin(wt),
v(0)1, dv(0)/dt0
x1(t) v(t) x2(t) dv(t)/dt
dx1(t) /dt x2(t) dx2(t)/dt (1/LC)
sin(wt)-(R/L)x2(t)-(1/LC)x1(t)
  • Code as a Function m-file in ODE Solver Format

function xdotmyode(t,x) MYODE Model of second
order systemglobal R L C w xdotzeros(2,1) xdot(
1)x(2) xdot(2)(1/(LC))sin(wt)-(R/L)x(2)-(1/
(LC))x(1) End of function m-file myode.m
Runge-Kutta, Adams-Bashforth, Adaptive Stepsize,
Stiff Solvers...
98
ODEs (Initial Value Problems)
  • Write a Script to Drive the Solution and Results

MYODERUN Numerical solution of second order
systemglobal R L C w R1000 L0.001 C1e-6
w2pi315 x01 0 td0 0.01 t,xode45('my
ode',td,x0) plotyy(t,x(1,),t,x(2,)) xlabel('t,
sec'), title('Numerical System Solution') End
of script m-file myoderun.m
  • Run the Script

gtgt myoderun
99
(No Transcript)
100
(No Transcript)
101
Simple Polynomial Fitting
  • Typical setting is the task of fitting measured
    data with an analytical function (a favorite is a
    polynomial)
  • Things to Try...
  • Increase the order of the specified fit (larger
    is not always better)
  • Decrease the order of the specified fit ("smooth"
    the fit)
  • Add more "noise" to the data which is being "fit"

PD A "test" of the curve fitting capabilities
of MATLAB xrough0110 xsmooth00.110 yp
olyval(1 -11 31 -21,x)5randn(size(xrough)) pc
oefspolyfit(xrough,y,3) plot(xrough,y,'ko',xsmoot
h,polyval(pcoefs,xsmooth),'b-') End of script
m-file pd.m
102
Spline Fits (Piecewise Polynomials)
  • With local attention (piecewise), much better
    behavior
  • Things to Note...
  • The piecewise polynomials are available
    viappspline(xrough,y)
  • The re-evaluation of the fit can then be carried
    out viaysmoothppval(pp,xsmooth)

SP A "test" of the spline curve fitting
capabilities of MATLAB xrough0110
xsmooth00.110 ypolyval(1 -11 31
-21,x)5randn(size(xrough)) ysmoothspline(xrou
gh,y,xsmooth) plot(xrough,y,'ko',xsmooth,ysmooth,
'b-') End of script m-file sp.m
103
Advanced Fits Via Parameter Searching
  • Utilize the "local" minimum search capabilities
    of MATLAB's fminsearch() Example y(x)A
    exp(ax)sin(wxf) where A, a, w
    and f are unknown
  • Algorithm
  • Write an objective function which computes the
    "distance" between measured y(x) and that
    obtained by a parameter guess.
  • Let fminsearch() "search" for a set of parameters
    which "locally" minimize the error.

104
Advanced Fits Continued
function thediffwierdfunc(plist) WIERDFUNC
Example function for nonlinear fit
solution global xraw yraw yfitplist(1)exp(plist
(2)xraw).sin(plist(3)xrawplist(4)) thediffno
rm(yraw-yfit) End of function m-file
wierdfunc.m
WIERDRUN Driver for nonlinear fit
solution global xraw yraw xrawlinspace(0,5,100)
A2 alpha-1 omega2pi phipi/4 yrawAexp(a
lphaxraw).sin(omegaxrawphi)0.1randn(size(xra
w)) slistfminsearch('wierdfunc',3 -2 pi
0.2) yfitslist(1)exp(slist(2)xraw).sin(slist(
3)xrawslist(4)) plot(xraw,yraw,'k.',xraw,yfit,'
b-') End of script m-file wierdrun.m
105
Advanced Fits Continued
gtgt wierdrun slist 2.0845 -1.0308
6.3030 0.8202
106
(No Transcript)
107
(No Transcript)
108
(No Transcript)
109
(No Transcript)
110
(No Transcript)
111
(No Transcript)
112
(No Transcript)
113
(No Transcript)
114
(No Transcript)
115
(No Transcript)
116
(No Transcript)
117
(No Transcript)
118
(No Transcript)
119
(No Transcript)
120
(No Transcript)
121
(No Transcript)
122
(No Transcript)
123
(No Transcript)
124
(No Transcript)
125
(No Transcript)
126
(No Transcript)
127
(No Transcript)
128
(No Transcript)
129
(No Transcript)
130
(No Transcript)
131
(No Transcript)
132
(No Transcript)
133
(No Transcript)
134
(No Transcript)
135
(No Transcript)
136
(No Transcript)
137
(No Transcript)
138
(No Transcript)
139
(No Transcript)
140
(No Transcript)
141
(No Transcript)
142
(No Transcript)
143
(No Transcript)
144
(No Transcript)
145
(No Transcript)
146
(No Transcript)
147
(No Transcript)
148
(No Transcript)
149
(No Transcript)
150
(No Transcript)
151
(No Transcript)
152
(No Transcript)
153
(No Transcript)
154
(No Transcript)
155
(No Transcript)
156
(No Transcript)
157
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com