You are on page 1of 28

- 1

1- 1
2- 1
3- 1
- 2

( Inline Functions) inline 1-2

M-file 2-2

3-2

4-2

5- 2
- 3 )( Boundary value problems
- 4
- 5

1-5

2- 5

3
- 1

- 1- 1

y(x) = xy. )(1.1

)( dsolve .

)>>y = dsolve(Dy = y*x,x


)y = C1*exp(1/2*x2

. t

>>eqn1 = Dy = y*x
= eqn1
Dy = y*x
)>>y = dsolve(eqn1,x
)y = C1*exp(1/2*x2

eqn1 .

4
y(1)=1
:

)>>y = dsolve(eqn1,y(1)=1,x
=y
)1/exp(1/2)*exp(1/2*x2

;>>inits = y(1)=1
)>>y = dsolve(eqn1,inits,x
=y
)1/exp(1/2)*exp(1/2*x2

) (1.1 .

. -

(1 ) : ) y(x )^( .*,./,.

) (2 ) y(x .

) y(x

)( vectorize . )( eval .

. ) y(x

;)>>x = linspace(0,1,20
;))>>z = eval(vectorize(y
)>>plot(x,z

: )( eval )( vectorize

. .

- 2- 1

5
:

y(x) + 8y(x) + 2y(x) = cos(x); y(0) = 0, y(0) = 1. )(1.2

;)>>eqn2 = D2y + 8*Dy + 2*y = cos(x


;>>inits2 = y(0)=0, Dy(0)=1
)>>y=dsolve(eqn2,inits2,x
=y
)1/65*cos(x)+8/65*sin(x)+(-1/130+53/1820*14(1/2))*exp((-4+14(1/2))*x
)-1/1820*(53+14(1/2))*14(1/2)*exp(-(4+14(1/2))*x
;))>>z = eval(vectorize(y
)>>plot(x,z
vectorize eval .

- 3- 1

)x(t) =x(t) + 2y(t) z(t


)y(t) =x(t) + z(t
z(t) =4x(t) 4y(t) + 5z(t).
)(3.1

)>>[x,y,z]=dsolve(Dx=x+2*y-z,Dy=x+z,Dz=4*x-4*y+5*z
=x
2*C1*exp(2*t)-2*C1*exp(t)-C2*exp(3*t)+2*C2*exp(2*t)-
)1/2*C3*exp(3*t)+1/2*C3*exp(t
=y

6
2*C1*exp(t)-C1*exp(2*t)+C2*exp(3*t)-C2*exp(2*t)+1/2*C3*exp(3*t)-
)1/2*C3*exp(t
=z
-4*C1*exp(2*t)+4*C1*exp(t)+4*C2*exp(3*t)-4*C2*exp(2*t)-
)C3*exp(t)+2*C3*exp(3*t

t .

)( dsolve . y(0)=2 x(0)=1 z(0)=3

;>>inits=x(0)=1,y(0)=2,z(0)=3
)>>[x,y,z]=dsolve(Dx=x+2*y-z,Dy=x+z,Dz=4*x-4*y+5*z,inits
=x
)6*exp(2*t)-5/2*exp(t)-5/2*exp(3*t
=y
)5/2*exp(t)-3*exp(2*t)+5/2*exp(3*t
=z
)-12*exp(2*t)+5*exp(t)+10*exp(3*t
-

;)>>t=linspace(0,.5,25
;))>>xx=eval(vectorize(x
;))>>yy=eval(vectorize(y
;))>>zz=eval(vectorize(z
)>>plot(t, xx, t, yy, t, zz

1-1
7
- 2
.

ode23 ode45 -

- - .

- 1- 2 Inline
. 1-2

[0, .5]. dy/dx=xy2 + y ; y(0) =1, x


) y'=f(x,y ) f(x,y .
) f(x,y inline .
)>>f=inline(x*y2+y
=f
Inline function:
f(x,y) = x*y2+y
ode45 :
ode45(function , domain , initial condition ).
:
)>>[x,y]=ode45(f,[0 .5],1
. x

y ) .

(. x y

)>>plot(x,y
2.1 .

: x ode45

] [0,5 y

8
y(0.5) .... )y(0.2) y(0.1

] [0,0.1,0.2,0.3,0.4,0.5 ode45 .

>>xvalues=0:.1:.5
= xvalues
0 0.1000 0.2000 0.3000 0.4000 0.5000
)>>[x,y]=ode45(f,xvalues,1
=x
0
0.1000
0.2000
0.3000
0.4000

: 2.1

[0, .5]. dy/dx=xy2 + y ; y(0) =1, x

0.5000
=y
1.0000

9
1.1111
1.2500
1.4286
1.6667
2.0000

) ( option ode45

. ) (RelTol )

(AbsTol . ode45 . yk

) y(xk k ek x

ek max(RelTol * yk,AbsTol),

RelTol=0.001 AbsTol=0.000001 .

yk

ek . RelTol

. y'=xy2+y y(0)=1 y x )(1

;)>>[x,y]=ode45(f,[0,1],1

. x

) (1 y ) . y

1014 (. RelTol

;)>>options=odeset(RelTol,1e-10
;)>>[x,y]=ode45(f,[0,1],1,options
)>>max(y

10
= ans
2.425060345544448e+07

) y(x

y ) ( y

M-files - 2- 2

2.1 ) f(x,y

M-file first.m .

;)function yprime = firstode(x,y


% FIRSTODE: Computes yprime = x*y2+y
;yprime = x*y2 + y
ode45 : @

M-file .

;]>>xspan = [0,.5
;>>y0 = 1
;)>>[x,y]=ode23(@firstode,xspan,y0
>>x
- 3- 2

Inline

M-file .

2-2

dx/dt=-Vx+Vy
dy/dt=Ux-y-xz

11
dz/dt=-Ez+xy,
E=8/3 V=10 U=28 x(0)=-8

y(0)=8 z(0)=27 M-file .

;)function xprime = lorenz(t,x


%LORENZ: Computes the derivatives involved in solving the
%Lorenz equations.
;sig=10
;beta=8/3
;rho=28
)xprime=[-sig*x(1) + sig*x(2); rho*x(1) - x(2) - x(1)*x(3); -beta*x(3
;])+x(1)*x(2
x ) y x(1 ) x(2 z ) x(3 .

. xprime

command window

;]>>x0=[-8 8 27
;]>>tspan=[0,20
)>>[t,x]=ode45(@lorenz,tspan,x0

. x

y z .

x z :

))>>plot(x(:,1),x(:,3
2.2 . y x z

)>>subplot(3,1,1
))>>plot(t,x(:,1

12
)>>subplot(3,1,2
))>>plot(t,x(:,2
)>>subplot(3,1,3
))>>plot(t,x(:,3
2.3 .

2.2

2.3
13
- 4- 2

E V U .

M-file

M-file ode 45 .

lorenz.m lorenz1.m lorenz1.m

p .

;)function xprime = lorenz1(t,x,p


%LORENZ: Computes the derivatives involved in solving the
%Lorenz equations.
;)sig=p(1); beta=p(2); rho=p(3
xprime=[-sig*x(1) + sig*x(2); rho*x(1) - x(2) - x(1)*x(3); -beta*x(3) +
;])x(1)*x(2

command window ode 45

;]>>p=[10 8/3 28
;)>>[t,x]=ode45(@lorenz1,tspan,x0,[],p

command

p window p .

ode 45 .

- 5- 2

14

1.2 1.2 .

y1(x)=y(x) ) y2(x) = y'(x

)y'1(x) = y2 (x

y'2 (x) = - 8y2(x) 2y1(x) + cos(x) .


M-file
)function yprime = sorderd (y,x
])yprime =[ y(2) ; -8*y(2) 2*y(1) + cos(x

;]>>xspan = [0,1
;]>>y0=[0 1
)>>[x , y]= ode 45(@sorderd , xspan , y0

- 3 ) ( Boundary differential equations

) initial

( conditions

) . ( BVPs

y''-3y'+2y=0
y(0)=0
y(1)=10,
y(0)=0 y(1)=10 ] [0,1 ) .

(.

15

y1=y ' y2=y

y'1=y2
y'2 = -2y1 +3y2.
M-file bvpexample.m

)function yprime = bvpexample(t,y


%BVPEXAMPLE: Differential equation for boundary value
%problem example.
;])yprime=[y(2); -2*y(1)+3*y(2
M-file bc.m M-file

)function res=bc(y0,y1
%BC: Evaluates the residue of the boundary condition
;]res=[y0(1);y1(1)-10
M-file res .

y0 . y1

) y(0 ) y(1 .

y . y'(1) = 10 y1(1)

10 y1(2) 10 .

. x

]) [y(0) , y'(0 .

) y'(0 . ) ) y(0

(.

bvp4c .

16
;)]>>sol=bvpinit(linspace(0,1,25),[0 1
;)>>sol=bvp4c(@bvpexample,@bc,sol
>>sol.x
= ans
Columns 1 through 9
0 0.0417 0.0833 0.1250 0.1667 0.2083 0.2500 0.2917 0.3333
Columns 10 through 18
0.3750 0.4167 0.4583 0.5000 0.5417 0.5833 0.6250 0.6667 0.7083
Columns 19 through 25
0.7500 0.7917 0.8333 0.8750 0.9167 0.9583 1.0000
>>sol.y
= ans
Columns 1 through 9
0 0.0950 0.2022 0.3230 0.4587 0.6108 0.7808 0.9706 1.1821
2.1410 2.4220 2.7315 3.0721 3.4467 3.8584 4.3106 4.8072 5.3521
Columns 10 through 18
1.4173 1.6787 1.9686 2.2899 2.6455 3.0386 3.4728 3.9521 4.4805
5.9497 6.6050 7.3230 8.1096 8.9710 9.9138 10.9455 12.0742 13.3084
Columns 19 through 25
5.0627 5.7037 6.4090 7.1845 8.0367 8.9726 9.9999
14.6578 16.1327 17.7443 19.5049 21.4277 23.5274 25.8196

sol.x

x . sol.y

) y(x x

) y'(x .

y''(x) + |y(x)| = 0
y(0) = 0
; y(4) = -2

y'1 = y2

17
y'2 = -|y1|
. y2 = y' y1 = y

function dydx = twoode(x,y)


dydx = [ y(2) ; -abs(y(1))];

function res = twobc(ya,yb)


res = [ ya(1) ; yb(1) + 2];


solinit = bvpinit(linspace(0,4,5),[0 0]);

. y'(0) = 0 y(0) =0
sol = bvp4c(@twoode,@twobc,solinit);

:
>> solinit = bvpinit(linspace(0,4,5),[0 0]);
>> sol = bvp4c(@twoode,@twobc,solinit)

sol =

solver: 'bvp4c'
x: [0 0.2500 0.5000 1 1.2500 1.5000 1.6250 1.7500 2 2.5000 2.7500
2.9306 3.1111 3.1389 3.1412 3.1415 3.1418 3.1901 3.2384 3.3333 3.6667 4]
y: [2x22 double]
yp: [2x22 double]
stats: [1x1 struct]

>> sol.y

ans =

Columns 1 through 16

0 0.5114 0.9909 1.7392 1.9614 2.0617 2.0639 2.0338


1.8795 1.2371 0.7890 0.4332 0.0632 0.0058 0.0010 0.0004
2.0669 2.0026 1.8139 1.1168 0.6518 0.1463 -0.1119 -0.3683 -
0.8600 -1.6558 -1.9104 -2.0210 -2.0659 -2.0669 -2.0669 -2.0669

Columns 17 through 22

18
-0.0002 -0.1001 -0.2003 -0.3985 -1.1356 -2.0000
-2.0669 -2.0693 -2.0766 -2.1050 -2.3583 -2.8761

;)>> x = linspace(0,4
;)y = deval(sol,x
))plot(x,y(1,:

1-3

- 4

19
)

( .

( )

) . (

d2T/dt2 = -( g/l ) sinT


M-file pendode.m l=1 m .

)function thetaprime=pendode(t,x
;]))thetaprime=[x(2);-(9.81/1)*sin(x(1
end
) dT/dt = x(2) T = x(1 .

pendevent.m

)function [lookfor stop direction]=pendevent(t,x


%PENDEVENT: MATLAB function M-file that contains the event
%that our pendulum reaches its center point from the right
lookfor = x(1) ; %Searches for this expression set to 0
stop = 1; %Stop when event is located
direction = -1; %Specifiy direction of motion at event

M-file ) lookfor = x(1 x(1) = 0

) ) . ( x(t) = 0 x(t) = 1

lookfor = x(1) 1 ( stop = 1

direction = -1

20
) ( x' ) x(2 ) .

) (

( .

;)>>options=odeset(Events,@pendevent
;]>>x0=[pi/4 0
;)>>[t, x, te, xe, ie]=ode45(@pendode, [0, 10], x0, options
>>te
= te
0.5215
>>xe
= xe
-0.0000 -2.3981
x0

S/4 .-

)( ode45 : ) (t x

te x ) (

ie . xe

) .

( . . ie = 1

t = 0.5215 P = 2.086 :

( . )

: . sinT T

21
S P=2.001 ) .
H
C

T T (.

. T -

T'=0 .

pendevent.m pendevent1.m .

)function [lookfor stop direction]=pendevent1(t,x


%PENDEVENT1: MATLAB function M-file that contains the event
%that our pendulum returns to its original postion pi/4
lookfor = [x(1);x(2)]; %Searches for this expression set to 0
stop = [0;0]; %Do not stop when event is located
direction = [0;0]; %Either direction accepted
pendevent1.m

. :

;)>>options=odeset(Events,@pendevent1
;]>>x0=[pi/4 0
;)>>[t, x, te, xe, ie]=ode45(@pendode,[0 2],x0,options
>>te
= te
0.0000
0.5216
1.0431
1.5646
>>xe
= xe
0.7854 -0.0000
-0.0000 -2.3972
-0.7853 0.0000
0.0000 2.3970
>>ie
= ie
2

22
1
2
1
0 : ] [0 , 2

1.0431 0.5216 . 1.5646 xe

) ( .

ie :

2 1 .

- 5

- 1- 5

 )( 6.1

x ] [x0 , xn

) y(x x ] P=[x0 ,x1,x2,,xn .

) y(x0 x y(x1) .

y(x1) = y(x0) + y'(x0)(x1 x0) + y'(c) (x1 x0)2 / 2 ,


23
(x0, x1) c .

y(x0) = f(x0, y(x0)), :

y(x1) = y(x0) + f(x0, y(x0))(x1 x0) +y(c)(x1 x0)2/2


P x1-x0

y(c)(x1 x0)2/2 . :

y(x1) y(x0) + f(x0, y(x0))(x1 x0). )(6.2

) y(x2

y(x2) = y(x1) + y'(x1)(x2 x1) +y(c)(x2 x1)2/2.

y(x1) = f(x1, y(x1)), :

y(x2) = y(x1) + f(x1, y(x1))(x2 x1) +y(c)(x2 x1)2/2.

y(c)(x2 x1)2/2 :
y(x2) y(x1) + f(x1, y(x1))(x2 x1),

) y(x1 ) (6.2 .

k = 1,2,,n-1 ) y(xk+1 :

y(xk+1) y(xk) + f(xk, y(xk))(xk+1 xk),

) y(xk ) ( .

(xk+1 xk) = x = (xn x0 ) /n .


) h (.

y(xk+1) y(xk) + f(xk, y(xk))x.


24
yn ... y2 y1 y0 y xn ... x1 x0 ) y0 = y(x0)

) y1 y(x1 ( ) y(x P :

yk+1 = yk + f(xk, yk)x. )(6.3


. 1 6 n= 10

; S

] [0 , 1 .

M-file

. y(0) = S y .

) 0.1 n=10 (

) (6.3 :

y1 = y0 + sin(x0y0)x = S + sin (0) 0.1 = S .


) (x1 , y1 ) = (0.1 , S ) (6.3

y2 = y1 + sin(x1y1)x = S + sin (0.1S)(0.1) = 3.1725


) (x2 , y2 ) = (0.2 , 3.1725 :

y3 = y2 + sin(x2y2)x = 3.1725 + sin(.2(3.1725))(.1) = 3.2318.


M-file :

)function [xvalues, yvalues] = euler(f,x0,xn,y0,n


%EULER: MATLAB function M-file that solve the
%ODE y=f, y(x0)=y0 on [x0,y0] using a partition
%with n equally spaced subintervals
;dx = (xn-x0)/n
;x(1) = x0
;y(1) = y0
for k=1:n
;x(k+1)=x(k) + dx
;y(k+1)= y(k) + f(x(k),y(k))*dx
end

25
;xvalues = x
;yvalues = y
6.1

))>>f=inline(sin(x*y
=f
Inline function:
)f(x,y) = sin(x*y
)>>[x,y]=euler(f,0,1,pi,10
=x
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
=y
3.1416
3.1416
3.1725
3.2318
3.3142
3.4112
3.5103
3.5963
3.6548
3.6764
3.6598
)>>plot(x,y
;)>>[x,y]=euler(f,0,1,pi,100
)>>plot(x,y
x

=x
0

26
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
=y
3.1416
3.1572
3.2029
3.2750
3.3663
3.4656
3.5585
3.6299 x=0.1 6.1
3.6688
3.6708 x = 0.01
3.6383

-2-6

. y(x) .

n n .

) .

(.

P = [x0, x1, ..., xn] ] [x0 , xn

6.1 .

) ( ) y(xk+1 xk .

!y(xk+1) = y(xk) + y(xk)(xk+1 xk) + y"(xk) (xk+1 xk )2 / 2 +y'''(c) (xk+1 xk )3 / 3

27
) (xk, xk+1 . c ) ( -

) y(xk+1 :

; y(xk+1) y(xk) + y(xk)(xk+1 xk) + y"(xk) (xk+1 xk )2 / 2

) y'(xk )) f(xk , y(xk .

) y''(xk .

)) y(x) = f(x, y(x .



; 

. -

:




; 

) y"(xk ) y'(xk )) f(xk , y(xk

x = (xk+1 xk),

28
-2-6 n=10

;  S

x
x
.

f(x,y) = sin (xy),

yk ) y(xk :

y k+1 = yk +sin( xk yk ) (0.1) + ] ) [ yk cos( xk yk) + xk cos(xk yk ) sin(xk yk


(0.1)2 / 2 .

) (x0 , y0) = (0,S :

y 1 = S + S (0.005) = 3.1573 ,

3.1572 .

) (x1 , y1 ) = (0.1 , 3.1573

y 2 = 3.1573 + sin(0.1 3.1573) (0.1) + [3.1573 cos(0.1 3.1573) +


0.1cos(0.1 3.1573) sin (0.1 3.1573) ] (0.12 ) / 2 = 3.2035 ,

3.2029

3.1725 .

M-file

29
function F=taylor(x,y)
f=sin(x*y);
fx=y*cos(x*y);%derivation of f function to x.
fy=x*cos(x*y);%derivation of f function to y.
F=[f;fx;fy];

clc,close all,clear all;


xn=1;
n=10;
x=zeros(1,n+1);y=zeros(1,n+1);
x(1)=0;
y(1)=pi;
dx=(xn-x(1))/n;
for i=1:n
x(i+1)=x(i)+dx;
T=talor(x(i),y(i));
y(i+1)=y(i)+T(1)*dx+(T(2)+T(3)*T(1))*(dx^2)*0.5;
end
disp(x)
disp(y)

1- Solving ODE in MATLAB , P . Howard , Fall 2007


2- Solving ODEs with
MATLAB,L.F.SHAMPINE,I.GLADWELL,S.THOMPSON,CAMBRID
GE UNIVERSITY PRESS ,2003

3-Help of MATLAB software

30

You might also like