The book here (sorry not in English) on page 676:
$\begin{cases}y'=-y^{2} \\ y(0)=1\end{cases}$
when $x_{k}=\frac{k}{5}$ which means $h=0.2$ i.e. $\Delta x=x_{k+1}-x_{k}=0.2:=h$. The task is to calculate 5 steps with the logic on pages 670-671:
$\begin{align*} y_{k+1}&=y_{k}+hf(x_{k},y_{k}) \\ h&=x_{k+1}-x_{k} \\ k&=0,1,...\end{align*}$ where $f(x_{k},y_{k})=y'$.
My solution
I calculated the Euler values on the left and the right having the exact values. For example, $y_{1}=y_{0}+h \left|y'(y_{0})\right|=1-0.2*(1)^{2}=\frac{4}{5}$. I got the same as the R verified values below but my exact values are $0.5, 0.66, 0.6, 0.625, 0.61...$ which I think are wrong. The way I calculated the exact values is this:
- separate-integrate $y'=-y^{2}$
- you get $y(x)=\frac{1}{x+1}$ because $y(0)=1$
- Then $y(y_{0})=y(1)=0.5, y(y_{1})=y(0.5)=2/3,...$ until recursion ends to the fifth.
Are my results correct?
R -verification for the Euler -method
I was eventually able to get the same values as here by hand.
> euler<-function(y){a<-y; for(i in 1:5){b<-a-0.2*a**2; print(b); a<-b}} > euler(1) [1] 0.8 [1] 0.672 [1] 0.5816832 [1] 0.5140121 [1] 0.4611704
Thanks to Julian, I had misunderstanding in the parameters. The below should be correct.
> 1/((1:5/5)+1) [1] 0.8333333 0.7142857 0.6250000 0.5555556 0.5000000
Let's compare the values. The exact values are always greater than the Euler values because of the derivative $y'(x)=\frac{-1}{(x-1)^{2}}$, I am not yet sure how to vizualize this in R, basically the above values into data.frame and then matplot(df)
, investigating...
plot(1/(1:100+1), type='l' )
Perhaps related