ماتلاب 12
ماتلاب 12
dx
f ( x, t ) with x(0) x0 (1)
dt
On the left hand side is the derivative of the dependent variable x with respect to the
independent variable t. On the right hand side, there is a function that may depend on both
x and t. The independent variable t often represents time. In contrast to discrete time
equations of the form xt 1 f ( xt ) , where time t is discrete ( t 1, 2,... ), the independent
variable t in Equation (1) is a continuous variable, that is, it takes on real values, for
instance, t ∋ [0, ∞]. In addition, we prescribe the initial value at time 0, namely x0. (The
initial condition could be stated for some other time but time 0 is quite commonly used). A
differential equation is called ordinary if it involves only one independent variable.
Many differential equations cannot be solved exactly. Numerical methods have been
developed to approximate solutions. Numerical analysis is a field in mathematics that is
concerned with developing approximate numerical methods and assessing their accuracy,
for instance for solving differential equations. We will discuss the most basic method such
Taylor and Euler methods.
dy
2 y with y (0) 5 (5)
dx
We know what the solution of equation (5) is, namely y 5 exp(2 x) . We numerically
solve equation (5) using Euler’s method with h=0.1 in the time interval [0, 0.5], and then
check how well this method performs. We have f ( y) 2 y . Then
x0 0
x1 x0 h 0 0.1 0.1
x 2 x1 h 0.1 0.1 0.2
x 3 x 2 h 0 . 2 0 . 1 0. 3
x 4 x3 h 0.3 0.1 0.4
x 5 x 4 h 0 . 4 0 . 1 0. 5
And
y0 5
y1 y 0 hf ( y 0 ) 5 (
0.1)( 2)(5) 6
h f ( y0 )
x y Exact Difference
0 5 5 0
0.1 6 6.107014 0.107014
0.2 7.2 7.459123 0.259123
0.3 8.64 9.110594 0.470594
0.4 10.368 11.1277 0.759705
0.5 12.4416 13.59141 1.149809
The third column contains the exact values, y 5 exp(2 x) . The last error contains the
absolute error after each step, computed as |y-Exact|. We see that when h=0.1, the
numerical approximation is not very good after five steps. If we repeat the same
approximation with a smaller value for h, say h=0.01, the following table results for the
first five steps:
Solution:
x_in=0;
x_fin=0.5;
nsteps=10;
%dx=(x_fin-x_in)/nsteps;
dx=0.01;
x(1)=x_in;
y(1)= 5;
for n=1:nsteps
x(n+1)=x(n)+dx
y(n+1)=y(n)+dx*(2*y(n))
end
yexact=5*exp(2*x)
difference=yexact-y
table=[x',y',yexact',difference']
X y Exact Difference
0 5 5 0
0.01 5.1 5.101007 0.001007
0.02 5.202 5.204054 0.002054
0.03 5.30604 5.309183 0.003143
0.04 5.412161 5.416435 0.004275
0.05 5.520404 5.525855 0.005451
Doing five steps only gets us to x=0.05. We can do more steps until we reach x=0.5.
We find that the final point will be:
X y Exact Difference
0.5 13.45794 13.59141 0.133469
Computer Programming 127
Second Class \ Lec. 13
Choosing a smaller value for h resulted in a better approximation at x=0.5 but also
required more steps. One source of error in the approximation comes from the
approximation itself. Another source comes from rounding errors when we implement the
approximation on a computer. It is therefore not necessarily the case that smaller values of
h always improve the approximation.
4.5
3.5
3
x(t)
2.5
1.5
0.5
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Time (t)
That is
x(0.2) 0.5 0 0.5
The calculations results are plotted in Figure 2 showing the true and the approximate
value for h=0.2. Although the general trend of the true and the approximate values is the
same, the error is large. One way to reduce this error is by choosing a smaller step size.
Figure 2 shows the solution when the step size is halved i.e. h=0.1. Since the Euler's
method is first order, the global error is halved O (h /2) while the local error is quartered O
(h2/4). To get acceptable levels of errors the step size has to be further reduced to very low
values. This will however considerably increase the computational time since it will take a
larger number of iterations for each step. Nevertheless the Euler's method because of its
simplicity and easiness for implementation is still attractive for many engineering
problems.
5.5
4.5
3.5
x(t)
2.5
0.5
0 0.5 1 1.5 2 2.5
Time (t)
2.5
1.5
h(t)
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time(t)