0% found this document useful (0 votes)
16 views

Lecture 4

Uploaded by

Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 4

Uploaded by

Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

RK4 for pendulum

continue last lecture


Simple of RK4 for Pendulum

2
Solving ODE by using RK4

3
Simple of RK4 for Pendulum

clear all; clc% dao dong cua con lac


m=1000;
teta=zeros(m,1); % mang luu gia tri cua goc
w=zeros(m,1); %mang luu gai tri cua van toc goc
g=10; l=1 ; w(1)=0; t(1)=0;
teta(1)=pi/180*5;
h=0.01; % h=delta(t)=0.01 s
F1=@(t, teta, w) w; % phuong trinh dteta/dt=w
F2=@(t, teta, w) -g/l*teta; % dw/dt=-g/l*teta

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));

k2_w=F2(t(i)+h/2, teta(i)+k1_teta*h/2, w(i)+k1_w*h/2);


k2_teta=F1(t(i)+h/2, teta(i)+ k1_teta*h/2, w(i)+k1_w*h/2);

k3_w=F2(t(i)+h/2, teta(i)+k2_teta*h/2, w(i)+k2_w*h/2);


k3_teta=F1(t(i)+h/2, teta(i)+ k2_teta*h/2, w(i)+k2_w*h/2);

k4_w=F2(t(i)+h, teta(i)+k3_teta*h, w(i)+k3_w*h);


k4_teta=F1(t(i)+h, teta(i)+ k3_teta*h, w(i)+k3_w*h);

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)

clear all; clc


g = 10; l = 1.0;
f = @ equation_system;
tspan = [ 0.0, 10.0 ];
uv0 = [ pi/180*5; 0.0 ];
n=100;
[ t, uv ] = rk4 ( f, tspan, uv0, n );
u = uv(:,1); v = uv(:,2);
plot(t, u,'r', t, v, 'b', 'LineWidth', 2)
xlabel('time (s)');
ylabel('theta (radians)');
title('RK4 method')

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)

%program using ode45


tspan = [0 9]; % set time interval
y0 = 10; % set initial condition
% equation_1: right hand side of Equation
[t,y] = ode45( @equation_1 ,tspan ,y0);
plot(t,y)
% eqation
disp([t,y]) % displays t and y(t)
%--------------------------------------
function dydt = equation_1(t,y)
alpha=2; gamma=0.0001;
dydt = alpha* y-gamma *y^2;
end
12
Solving systems of first-order ODEs

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

function dydt = EquationSystem(t,y) 1


dydt = [y(2);
0.5
1000*(1 - y(1)^2)*y(2) - y(1)];
0
%Here y(1) is y1 and y(2) is y2, and dydt(1)
%is dy1/dt and dydt(2) is dy2/dt. -0.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

Using ODE45 function


0.3

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’)

function dtheta = odeFun(t, theta)


g = 10; l = 1;
% theta(1) = theta, theta(2) = dtheta
dtheta = zeros(2, 1);
dtheta(1) = theta(2);
dtheta(2) = -g/l*theta(1);
end
Boundary value problem
(page 784)

16
Giải số bài toán giá trị biên (BVP) bằng Phương Pháp số

Ví dụ cần giải phương trình y’’=f(x, y)


Initial Value
y(0)=y0
Problems (IVP)
y’(0)=y’0

Ví dụ cần giải phương trình y’’=f(x, y)


Boundary Value
y(0)=y0
Problems (BVP)
y(L)=yL

Computational Physics
Boundary Value Problems

18
Boundary Value Problems

19
Boundary Value Problems

h’ : heat transfer coefficient


Ta: the temperature of the surrounding air (oC)
A simple case, temperatures at the ends of the rod are held at fixed values.

20
Boundary Value Problems (shooting method)

h’= 0.01; L=10;


T(0)=40; T(L)=T(10)=200

we guess a value—say, z(0) =10,


solving=>T(10) = 168.3797

21
Boundary Value Problems

we guess a value—say, z(0) =20,


solving=>T(10) =285.8980

22
Boundary Value Problems for linear problem (BVP)

12.6907

Z(0)=12.6907 T(10)=200 Final exact “hit”


23
The Shooting Method for Nonlinear Problems (BVP)

For nonlinear boundary-value problems, linear interpolation will not get


necessarily result as requirement

Giải bằng phương


pháp Secant tìm z0

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

 Use secant line instead of tangent line at f(xi)


Secant Method
The formula for the secant method is

f ( x i )( x i  1  x i )
xi1  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 )
xi1  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

function [tv,yv] = euler2_(h,t0,tmax,y0,dy0)


% use Euler's method to solve 2nd order ODE y''=0.01*(y-20).^a
% return tvec and yvec sampled at t=(t0:h:tmax) as col. vectors
% y(t0)=y0, y'(t0)=dy0
global a te ye ss; % coeff of nonlinear acceleration
%g=9.8 %using for exercise 4
y = y0;
dy = dy0;
tv = [t0];
yv = [y0];
for t = t0:h:tmax
y = y+dy*h; % this and following line are Euler's method
dy = dy+(0.01*(y-20).^a)*h;%
tv = [tv; t+h];
yv = [yv; y];
end
return;
end
Function scant to
function x = secant1_(x1,x2,tol)
% secant method for one-dimensional root finding
global ye h te;
[tv,yv] = euler2_(h,0,te,40,x1); % x1 is the dy0, that is unknown and assume It is x1)
y = yv(te/h+1);% returns y at t=te; invariant: tv(te/h+1)==te
y1=y-ye;
[tv,yv] = euler2_(h,0,te,40,x2); %x1 is the dy0, that is unknown and assume It is x2)
y = yv(te/h+1); % returns y at t=te
y2=y-ye;
while abs(x2-x1)>tol
disp(sprintf('(%g,%g) (%g,%g)', x1, y1, x2, y2));
x3 = x2-y2*(x2-x1)/(y2-y1); % interporation value of dy0 is x3
%================
[tv,yv] = euler2_(h,0,te,40,x3);
plot(tv,yv,'*b', 'LineWidth',2);
y3 = yv(te/h+1)-ye; % yv(te/h+1)returns y at t=te; solve equation y=ye
%===================
x1 = x2; y1 = y2; x2 = x3; y2 = y3;
end
x = x2; return;
end
Shooting Method on y''=0.01(y-20)
300

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

clc;clear all 180

Ta=20; heso=0.01; dx=0.1; 160

140

for i=1:101 120

for j=1:101 100

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

You might also like