Num. #1: Recalls on Numerical Methods for Linear Systems and Ordinary differential equations (ODEs) - CORRECTION The programs are written with the SCILAB software. 1 Resolution of a linear system & Computation of the eigenvalues of a matrix For the exercise, the following functions are needed : LU decomposition : //Data n=20; A=2*eye(n,n)-diag(ones(n-1,1),1)-diag(ones(n-1,1),-1); U=A; L=eye(A); //LU decomposition with loops for k=1:n-1 p=u(k,k); if p==0 then disp( la matrice n admet pas de decomposition LU ); abort; for i=k+1:n L(i,k)=U(i,k)/p; for j=k+1:n, U(i,j)=U(i,j)-U(i,k)*U(k,j)/p; U(i,k)=0; // LU decomposition - Vectorial form for k=1:n-1 p=u(k,k); if p==0 then disp( la matrice n admet pas de decomposition LU ); abort; L(k+1:n,k)=U(k+1:n,k)/p; w=(k+1:n) ; U(w,w)=U(w,w)-U(w,k)*U(k,w)/p; U(w,k)=zeros(w); Gradient descent method with optimal step : // Data n=3; T. Goudon, M. Ribot 1/7 Analysis and Numerical Simulations for PDEs
A=[2,-1,0;-1,2,-1;0,-1,2]; b=[1;0;1]; x=zeros(n,1); epsilon=0.0001; erreur=1; iter=0; // Gradient descent method while erreur>epsilon, iter=iter+1; xold=x; d=a*x-b; rho=(d *d)/(d *A*d); x=x-rho*d; erreur=norm(x-xold); Conjugate gradient method : //Data n=3; A=[2,-1,0;-1,2,-1;0,-1,2]; b=[2;3;6]; //b=[1;0;1]; // Conjugate gradient method epsilon=0.0001; test=1;it=0; x=zeros(n,1); d=a*x-b; r=d; while (test>epsilon) it=it+1; rho=(r *d)/(d *A*d); x=x-rho*d; r2=r; r=a*x-b; test=norm(r)/norm(b); d=r+norm(r)^2*d/norm(r2)^2; Power method : // Data n=10; T. Goudon, M. Ribot 2/7 Analysis and Numerical Simulations for PDEs
A=2*diag(ones(1,n))-diag(ones(1,n-1),1)-diag(ones(1,n-1),-1); niter=200; x=zeros(n,1);x(1)=1; // Power method for i=1:niter, q=x/norm(x); x=a*q; vp(i)=x(1)/q(1); Inverse power method : // Data n=10; A=2*diag(ones(1,n))-diag(ones(1,n-1),1)-diag(ones(1,n-1),-1); niter=200; x=zeros(n,1);x(1)=1; // Inverse power method for i=1:niter, q=x/norm(x); x=a\q; vp(i)=1/(x(1)/q(1)); 2 Resolution of ordinary differential equations (ODEs) For the exercise, the following functions are needed Forward/Explicit Euler method : // T is the final time, dt the time step // uinit is the initial value, f is the function of the ODE function [u]=euler(t,dt,uinit,f) u(1)=uinit; u(i+1)=u(i)+dt*f(u(i)); T. Goudon, M. Ribot 3/7 Analysis and Numerical Simulations for PDEs
// Plot a graphic plot(time,u); Midpoint (or Runge-Kutta 2) method : function[u]=rk2(t,dt,uinit,f) u(1)=uinit; v=u(i)+dt*f(u(i))/2; u(i+1)=u(i)+dt*f(v); // Plot a graphic plot(time,u, r ); Forward/Explicit Euler method for a system : // T is the final time, dt the time step // uinit is the initial value, f is the function of the ODE // The output u is a matrix // Column n corresponds to a vector at a discrete time t^n function[u]=eulersystem(t,dt,uinit,f) u(:,1)=uinit; u(:,i+1)=u(:,i)+dt*f(u(:,i)); Midpoint (or Runge-Kutta 2) method for a system : T. Goudon, M. Ribot 4/7 Analysis and Numerical Simulations for PDEs
function[u]=rk2system(t,dt,uinit,f) u(:,1)=uinit; v=u(:,i)+dt*f(u(:,i))/2; u(:,i+1)=u(:,i)+dt*f(v); Exercise 1. Implement the resolution of the following equation : t u = 1 u 2 with u(0) = 0 (1) using the four methods presented above and a time step t = 0.1 until time T = 1. T=1; dt=0.1; f=inline( 1-x^2 ); uinit=0; Euler(T,dt,uinit,f) RK2(T,dt,uinit,f) 2. Remark that the exact solution of equation (1) can be computed exactly and is equal to : u(t) = t anh(t) = e2t 1 e 2t + 1. Enhance the convergence property of the methods by letting t go to 0. Compare the order of the four methods by plotting a graph in a log-log scale, which represents the evolution of L 2 error with respect to the time step t. clear; uinit=0; f=inline( 1-x^2 ); // Different time steps T=1; T. Goudon, M. Ribot 5/7 Analysis and Numerical Simulations for PDEs
TimeStep=[0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001]; for k=1:length(timestep), dt=timestep(k); // Exact solution uexact=(exp(2*time)-ones(size(time)))./(exp(2*time)+ones(size(time))); // Approximated solutions ueuler=euler(t,dt,uinit,f); urk2=rk2(t,dt,uinit,f); // errors between exact and approximated solutions ErrorEuler(k)=sqrt(dt)*norm(uexact-uEuler); ErrorRK2(k)=sqrt(dt)*norm(uexact-uRK2); // Clear the figure clf; // Graph of the errors loglog(timestep,erroreuler) hold on; loglog(timestep,errorrk2, g ) // Leg for the graph leg( Euler, RK2 ); 3. Implement the resolution of the following system of equations, called the Lorenz system using the two first methods presented above. The parameters will be : (x 0, y 0, z 0 ) = (1,1,1), T = 30 and t = 0.001. t x = 10(y x) t y = x(28 z) y t z = x y 8 3 z (2) clear; T=30; dt=0.001; for a system xinit=1;yinit=1;zinit=1; uinit=[xinit;yinit;zinit]; // Function for a system f=inline( [10*(u(2)-u(1));u(1)*(28-u(3))-u(2);u(1)*u(2)-8*u(3)/3], u ); u=eulersystem(t,dt,uinit,f); // RK2System(T,dt,uinit,f); // Plot a graphic T. Goudon, M. Ribot 6/7 Analysis and Numerical Simulations for PDEs
clf; plot3(u(1,:),u(2,:),u(3,:)) T. Goudon, M. Ribot 7/7 Analysis and Numerical Simulations for PDEs