Second-Order Taylor Series Method

2.3k Views Asked by At
2

There are 2 best solutions below

1
On BEST ANSWER

To the newly added part c)

Your ODE system is linear, thus also the relation of boundary values is linear. As in both runs the initial value of $y$ is $y(0)=1$, any linear continuation of these initial values will preserve that constant and only change $x(0)$. Now we have the values \begin{array}{l|l}x(0)&x(0.5)\\ \hline 0.5&1.49089\\ 1.0&2.32867 \end{array} we need to find a value $s$ so that $(1-s)\,1.49089+s\,2.32867=2$ and then use $x(0)=(1-s)\,0.5+s\,1.0$ as the appropriate initial value.

Or in other terms, the empirical linear relation is $$ x(0.5)=0.65311+ 1.67556\,x(0) $$ which can be solved for $x(0.5)=2$.


The Python script

def integrate(x0,y0):
    t,x,y,h = 0.0,x0,y0,0.1
    print "\\begin{array}{l|l|ll|ll}k&t_k&x_k&p_k&y_k&q_k\\\\\\hline"
    for k in range(6):
        p,q = h*(x+y)+0.5*h**2*((t+1)*x+y),h*t*x+0.5*h**2*((t+1)*x+t*y)
        print "%2d & %.2f & %.8f & %.8f & %.8f & %.8f \\\\"%(k,t,x,p,y,q)
        x0,y0,x,y,t = x,y,x+p, y+q, t+h
    print "\\end{array}"
    return x0,y0

a=0.5; fa,ga =  integrate(a,1.)
b=1.0; fb,gb =  integrate(b,1.)
c = b - (fb-2)*(b-a)/(fb-fa); fc,gc = integrate(c,1.)

produces the tables for step size $h=0.1$ where the last satisfies both boundary conditions.

\begin{array}{l|l|ll|ll}k&t_k&x_k&p_k&y_k&q_k\\\hline 0 & 0.00 & 0.50000000 & 0.15750000 & 1.00000000 & 0.00250000 \\ 1 & 0.10 & 0.65750000 & 0.17462875 & 1.00250000 & 0.01069250 \\ 2 & 0.20 & 0.83212875 & 0.19459086 & 1.01319250 & 0.02264854 \\ 3 & 0.30 & 1.02671961 & 0.21810895 & 1.03584104 & 0.03902903 \\ 4 & 0.40 & 1.24482856 & 0.24605801 & 1.07487007 & 0.06065668 \\ 5 & 0.50 & 1.49088657 & 0.27950062 & 1.13552675 & 0.08856479 \\ \end{array} \begin{array}{l|l|ll|ll}k&t_k&x_k&p_k&y_k&q_k\\\hline 0 & 0.00 & 1.00000000 & 0.21000000 & 1.00000000 & 0.00500000 \\ 1 & 0.10 & 1.21000000 & 0.23318000 & 1.00500000 & 0.01925750 \\ 2 & 0.20 & 1.44318000 & 0.26052412 & 1.02425750 & 0.03854694 \\ 3 & 0.30 & 1.70370412 & 0.29303895 & 1.06280444 & 0.06377941 \\ 4 & 0.40 & 1.99674307 & 0.33194281 & 1.12658384 & 0.09610009 \\ 5 & 0.50 & 2.32868588 & 0.37871555 & 1.22268394 & 0.13695615 \\ \end{array} \begin{array}{l|l|ll|ll}k&t_k&x_k&p_k&y_k&q_k\\\hline 0 & 0.00 & 0.80383973 & 0.18940317 & 1.00000000 & 0.00401920 \\ 1 & 0.10 & 0.99324290 & 0.21020914 & 1.00401920 & 0.01589727 \\ 2 & 0.20 & 1.20345204 & 0.23465715 & 1.01991647 & 0.03230967 \\ 3 & 0.30 & 1.43810919 & 0.26364237 & 1.05222614 & 0.05406932 \\ 4 & 0.40 & 1.70175156 & 0.29824844 & 1.10629547 & 0.08219491 \\ 5 & 0.50 & 2.00000000 & 0.33979149 & 1.18849038 & 0.11797123 \\ \end{array}

3
On

First off, we take the second derivatives: $$\begin{align}\ddot x&=\dot x+\dot y=x+y+tx=(t+1)x+y\\ \ddot y&= x+t\dot x=x+t(x+y)=(t+1)x+ty\end{align}$$ To go from one time step to the next we just do $$\begin{align}t_{k+1}&=t+k+\Delta t\\ x_{k+1}&=x_k+\Delta t\dot x_k+\frac12(\Delta t)^2\ddot x_k=x_k+\Delta t(x_k+y_k)+\frac12(\Delta t)^2((t_k+1)x_k+y_k)\\ y_{k+1}&=y_k+\Delta t\dot y_k+\frac12(\Delta t)^2\ddot y_k=x_k+\Delta t\,t_ky_k+\frac12(\Delta t)^2((t_k+1)x_k+t_ky_k)\end{align}$$ Assuming an initial value problem we just set initial values $t_0$, $x_0$, and $y_0$, choose a step size $\Delta t$ and integrate. In the following Matlab code, $t_0=0$, $x_0=1$, $y_0=0$ and we integrate from $t_0$ to $t_f=2$, choosing a suitable step size. The Matlab ODE45 solution is also plotted for comparison.

% taylor2.m

clear all;
close all;

f = @(t,y) [1 1;t 0]*y;

t0 = 0;
tf = 2;
x0 = 1;
y0 = 0;
[tau,eta] = ode45(f,[t0 tf],[x0 y0]);

npts = 30;
t = zeros(npts+1);
x = zeros(npts+1);
y = zeros(npts+1);
Dt = (tf-t0)/npts;
t(1) = t0;
x(1) = x0;
y(1) = y0;
for k = 1:npts,
    t(k+1) = t(k)+Dt;
    x(k+1) = x(k)+Dt*(x(k)+y(k))+1/2*Dt^2*((t(k)+1)*x(k)+y(k));
    y(k+1) = y(k)+Dt*t(k)*x(k)+1/2*Dt^2*((t(k)+1)*x(k)+t(k)*y(k));
end

plot(eta(:,1),eta(:,2),'k-',x,y,'r--');
title(['Solution for \Deltat = ' num2str(Dt)]);
xlabel('x');
ylabel('y');
legend('ODE45','Taylor','Location','best');

Plot of solution to differential equation

As can be seen at this step size, the accuracy seems OK, at least in comparison with the ODE45 results.