File:ODE Exp Imp Euler method.svg

Summary

Description
English: Compares the forward/explicit and backward/implicit Euler methods for the numerical solution of the ODE y'(x)=-200*(y-cos(x))-sin(x), y(0)=1. With the substituton z=y-cos(x), the ODE z'=-200*z, z(0)=0, results. Numerical integration of the second ODE should always result in the constant 0. By the nonlinear substitution in the first ODE, numerical errors are introduced in every step that are magnified by the factor 200. For not too small stepsizes, this results in explosive behavior of the explicit method whereas the implicit method remains close to the exact solution.
Date
Source Own work
Author LutzL
SVG development
InfoField
 The SVG code is valid.
 This plot was created with gnuplot.
Category:Valid SVG created with Gnuplot#ODE%20Exp%20Imp%20Euler%20method.svg
 Category:Translation possible - SVGThis plot uses embedded text that can be easily translated using a text editor.

Licensing

I, the copyright holder of this work, hereby publish it under the following licenses:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-3.0#ODE%20Exp%20Imp%20Euler%20method.svg
GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
Category:License migration redundant#ODE%20Exp%20Imp%20Euler%20method.svgCategory:GFDL#ODE%20Exp%20Imp%20Euler%20method.svg
You may select the license of your choice.
Category:Self-published work

Source

Using a small C program and gnuplot.

exp_imp_euler.c

/*
implizites und explizites Eulerverfahren für y'(x)=-2000*(y-cos(x))-sin(x)

*/

#include<stdio.h>
#include<math.h>

#define ALPHA 200

double f(double x,double y) {
	return -ALPHA*(y-cos(x))-sin(x);
	}
	
double explicit(double x,double y,double h) {
	return y+h*f(x,y);
}
	
double implicit(double x,double y, double h) {
	/* solve yp=y+h*f(xp,yp) for yp
		explicit solution is
		y=yp-h*f(xp,yp)
		 =(1+2000*h)*yp-2000*h*cos(xp)+h*sin(xp)
	*/
	double xp=x+h;
	
	return (y+h*(ALPHA*cos(xp)-sin(xp)))/(1+ALPHA*h);
}


int main(){
	double x,ye,yi,h;
	
	FILE *fp=fopen("imp_euler.dat","w");
	
	x=0; ye=yi=1; h=1.2e-2;
	
	for(;x<1; x+=h){
		fprintf(fp,"%8.5f\t%8.5f\t%8.5f\t%8.5f\n",x,cos(x),ye,yi);
		ye=explicit(x,ye,h);
		yi=implicit(x,yi,h);
	}
	return 0;
}

gnuplot commands

set term push
set term svg size 1200,900 fsize 28 linewidth 2

set output "exp_imp_euler.svg"

pl [:] [0:2] 'imp_euler.dat' u 1:3 ti 'explizit' w po pt 3, 'imp_euler.dat' u 1:4 w lin lt 3 lw 3 ti 'implizit'

unset outp
set term pop
Category:Euler method Category:Images with C source code Category:Images with Gnuplot source code Category:Numerical analysis
Category:CC-BY-SA-3.0 Category:Euler method Category:GFDL Category:Images with C source code Category:Images with Gnuplot source code Category:License migration redundant Category:Numerical analysis Category:Self-published work Category:Translation possible - SVG Category:Valid SVG created with Gnuplot