Lecture 4
Lecture 4
2
Solving ODE by using RK4
3
Simple of RK4 for Pendulum
4
for i=1:m
t(i+1)=i*h;
k1_w=F2(t(i), teta(i), w(i));
k1_teta=F1(t(i), teta(i), w(i));
w(i+1)=w(i)+1/6*h*(k1_w+2*k2_w+2*k3_w+k4_w);
teta(i+1)=teta(i)+1/6*h*(k1_teta+2*k2_teta+2*k3_teta+k4_teta);
end
plot(t, teta,'r', t, w, 'b', 'LineWidth', 2)
xlabel('time (s)');
ylabel('theta (radians)');
%title('Euler-Cromer method, dt=0.01')
title('Range-Kutta Method, dt=0.01')
tetaw=[teta, w];
5
General Program using
RK4 for pendulum
6
RK4 method (General Pragram for pendulum)
7
function [ t, y ] = rk4 ( dydt, tspan, y0, n )
mRK4 method
= size ( y0, 1(General
); Pragram for pendulum) continue
t = zeros ( n + 1, 1 ); y = zeros ( n + 1, m );
tfirst = tspan(1); tlast = tspan(2);
dt = ( tlast - tfirst ) / n;
t(1,1) = tspan(1);
y(1,:) = y0(:);
for i = 1 : n
f1 = dydt ( t(i,1), y(i,:) );
f2 = dydt ( t(i,1) + dt / 2.0, y(i,:) + dt * f1' / 2.0 );
f3 = dydt ( t(i,1) + dt / 2.0, y(i,:) + dt * f2' / 2.0 );
f4 = dydt ( t(i,1) + dt, y(i,:) + dt * f3' );
t(i+1,1) = t(i,1) + dt;
y(i+1,:) = y(i,:) + dt * ( f1' + 2.0 * f2' + 2.0 * f3' + f4' ) / 6.0;
end
return
end
function duvdt = equation_system( t, uv )
g = 10; l = 1.0;
u = uv(1);
v = uv(2);
dudt =v;
dvdt = - ( g / l ) * u;
duvdt = [ dudt; dvdt ];
return
end
8
9
Solving ODEs using ODE45
function
10
Solving ODEs using ODE45 function
11
Using ODE45 function
[t,state] = ode45(@equations,tspan,ICs,options)
tspan = [0 5000];
y1_0 = 2;
y2_0 = 0;
[T,Y] = ode45(@EquationSystem,tspan,[y1_0 y2_0]);
plot(T,Y(:,1),'r', 'linewidth', 3);
2.5
xlabel('time (s)');
ylabel('Y'); 2
1.5
end -1
-1.5
-2
-2.5
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
time (s)
13
Using ODE45 function for Pendulum
theta
0.2 dtheta
0.1
-0.1
-0.2
-0.3
0 1 2 3 4 5 6 7 8 9 10
time (s)
2023
clc, clear all
theta_ic = [pi/180*5; 0]; %initial conditions:theta(t=0)=pi/180*5;dtheta(t=0)=0.
tspan = [0 10];
[t, theta] = ode45(@odeFun, tspan, theta_ic);
plot(t, theta(:,1), 'r', t, theta(:,2), 'b', 'linewidth', 3);
legend('theta', 'dtheta')
xlabel('time (s)'); ylabel('theta (radians)');
title(' Using ODE45 function’)
16
Giải số bài toán giá trị biên (BVP) bằng Phương Pháp số
Computational Physics
Boundary Value Problems
18
Boundary Value Problems
19
Boundary Value Problems
20
Boundary Value Problems (shooting method)
21
Boundary Value Problems
22
Boundary Value Problems for linear problem (BVP)
12.6907
24
Shooting Method on y''=0.01(y-20)a
300
250
200 y'(0)=12.7007
150
100
50
0
0 2 4 6 8 10 12
t
25
Secant Method
f ( x i )( x i 1 x i )
xi1 xi
f ( x i 1 ) f ( x i )
Notice that this is very similar to the false position method in form
Still requires two initial estimates
But it doesn't bracket the root at all times - there is no sign test
Algorithm for Secant method
Open Method
1. Begin with any two endpoints [a, b] = [x0 , x1]
2. Calculate x2 using the secant method formula
f ( x i )( x i 1 x i )
xi1 xi
f ( x i 1 ) f ( x i )
3. Replace x0 with x1, replace x1 with x2 and
repeat from (2) until convergence is reached
Use the two most recently generated points in subsequent iterations (not a
bracket method!)
Secant Method
Advantage of the secant method -
It can converge even faster and it doesn’t need to bracket the root
Disadvantage of the secant method -
It is not guaranteed to converge!
It may diverge (fail to yield an answer)
Program: shooting method
%Main-program
clc, clear all % program shooting method for fireworks problem y''=0.01*(y-20).^a
global a te ye h ss;
a = 1; % coeff of nonlinear acceleration
te = 10; % length of rod: L=te=10
ye = 200; % temperature at end of rod, boundary condition T(L)=ye=200
h = 0.01; % length step
clf; hold on; ss=[];
for dy0=10:10:20 % non linear dy0=6:10, a=3
[tv,yv] = euler2_(h,0,te,40,dy0);
plot(tv,yv,'o-', 'LineWidth',2);
ss=[ss; dy0, yv(te/h+1)-ye]; %test error
end
dy = secant1_(10,20,1e-5);
[tv,yv] = euler2_(h,0,te,40,dy);
plot(tv,yv,'o-', 'LineWidth',2); % draw last curve
% invariant: tv(te/h+1)==te, point at the end of rod
y = yv(te/h+1); % returns y at the end t=te
text(te+.2,y,sprintf('y\047(0)=%g',dy), 'FontSize',15);
text(te+.2,y,sprintf('y\047(0)=%g',dy), 'Color','r', 'FontSize',15);
Function: euler2_ for solving eqation y''=0.01*(y-20).^a
250
200 y'(0)=12.7007
150
100
50
0
0 2 4 6 8 10 12
t
35
Boundary Value Problems (Finite Difference method)
The most common alternatives to the shooting method are finite-difference
approaches
2
2
𝑖 𝑖 𝑖 2
2
𝑖 𝑖 𝑖
2 2
2 2 36
37
L=10; x=2;
38
Tridiagonal Matrix Algorithm (Matlab code)
function x = trid(A,b)
% solve tridiagonal system of equations
N = size(A,2);
for m = 2:N % Upper Triangularization
tmp = A(m,m - 1)/A(m - 1,m - 1);
A(m,m) = A(m,m)-A(m - 1,m)*tmp; A(m,m - 1) = 0;
b(m,:) = b(m,:)-b(m - 1,:)*tmp;
end
x(N,:) = b(N,:)/A(N,N)
for m = N - 1: -1: 1 % Back Substitution
x(m,:) = (b(m,:)-A(m,m + 1)*x(m + 1))/A(m,m);
end
end
39
Main program
200
140
if i==j
80
60
T0(i, j)=2+heso*dx^2; 40
0 1 2 3 4 5 6 7 8 9 10
elseif(i==j+1)|(j==i+1)
x(m)
T0(i,j)=-1;
else A(i,j)=0;
end
end
end
dx=0.1;
for i=1:101
TP(i)=dx*dx*Ta*heso;
end
TP(1)=40; TP(101)=200; TP=TP'; T0*T=TP
T=trid(T0, TP);
x=0:0.1:10;
plot(x,T)
40
Matrix method
=[T0][T]=[TP]
[T]=[T0]\[TP]
41
Matrix Method
clc;clear all
Ta=20; heso=0.1; dx=0.1; L=10; n=L/dx;
for i=1:n+1
for j=1:n+1
if i==j
T0(i, j)=2+heso*dx^2;
elseif(i==j+1)|(j==i+1)
T0(i,j)=-1;
else T0(i,j)=0;
end
end
end
for i=1:n
TP(i)=dx*dx*Ta*heso;
end
TP(1)=200; TP(n+1)=40; TP=TP';
x=0:dx:10;
T=T0\TP;
plot( x, T, '*r'); xlabel('x(m)'); ylabel('T(^oC)');
42
Iterative methods for BVP
T(i)=(T(i-1)+T(i+1)+h’ h’
43
Iterative methods for BVP
Iterative methods for BVP: y''=0.01(y-20)
200
clear all; clc
Ta=20; heso=0.01; dx=0.1; L=10; n=L/dx; 180
T0=zeros(n+1,1); 160
T0=T0+2+heso*dx^2; 140
T0(1)=40;T0(n+1)=200; 120
tam=1e-3; 100
T1=T0;ss=10 80
60
while ss>1e-6 40
0 1 2 3 4 5 6 7 8 9 10
for i=2:n x (m)
T1(i)=(T0(i-1)+T0(i+1)+dx*dx*Ta*heso)/(2+heso*dx^2);
ss=max(abs(T1-T0));
end
T0=T1;
end
x=linspace(0, 10, n+1);
plot(x, T1, 's-b'); hold on
xlabel('x (m)', 'FontSize',20);
ylabel('T (^oC)', 'FontSize',20);
title(sprintf('Iterative methods for BVP: y\047\047=0.01(y-20)'), 'FontSize',14);
44