Docsity

Fluid Mechanics: Worksheet 2, Summaries of Fluid Mechanics

Toggle sidebar
bg1
Fluid Mechanics: Worksheet 2
June 2019
Excercises
The idea of this worksheet is to program forward and backward Euler. By some of the
examples, we want to highlight the numerical instability of forward methods.
For the following exercises, we set the following notation. We consider a curve
X: [0,โˆž)โ†’Rn,
which is a solution to the ODE equation
(dX(t)
dt =F(t, X(t)) for t > 0
X(0) = X0,(1)
where X0โˆˆRnand F: [0,โˆž)ร—Rnโ†’Rn.
1. Implement forward Euler as a function that given the input of initial condition
X0, the step size h, the final simulation time Tand the function F, it computes
the discrete approximation Xhto X.
โ€ขCreate an m file script called Feuler.m
โ€ขTo make it a function write in the first line
function Xh=Feuler(X0,h,T,F)
โ€ขCompute the number of steps necessary by taking the floor function of
N iterations=floor(T/h)
โ€ขInitialize Xh:
Xh=zeros(n,N iterations)
where nis the dimension of the problem.
โ€ขImplement the initial condition:
Xh(:,1)=X0
โ€ขImplement a for loop that computes the approximated value of the rest of
Xh:
for k=2:N iterations
Xh(k)=Xh(k-1)+h F((k-1)*h,Xh(k-1))
end
1
pf3

Partial preview of the text

Download Fluid Mechanics: Worksheet 2 and more Summaries Fluid Mechanics in PDF only on Docsity!

Fluid Mechanics: Worksheet 2

June 2019

Excercises

The idea of this worksheet is to program forward and backward Euler. By some of the examples, we want to highlight the numerical instability of forward methods. For the following exercises, we set the following notation. We consider a curve X : [0, โˆž) โ†’ Rn,

which is a solution to the ODE equation { (^) dX(t) dt =^ F^ (t, X(t))^ for^ t >^0 X(0) = X 0 ,

where X 0 โˆˆ Rn^ and F : [0, โˆž) ร— Rn^ โ†’ Rn.

  1. Implement forward Euler as a function that given the input of initial condition X 0 , the step size h, the final simulation time T and the function F , it computes the discrete approximation Xh to X. - Create an m file script called Feuler.m - To make it a function write in the first line function Xh=Feuler(X0,h,T,F) - Compute the number of steps necessary by taking the floor function of N iterations=floor(T/h) - Initialize Xh: Xh=zeros(n,N iterations) where n is the dimension of the problem. - Implement the initial condition: Xh(:,1)=X - Implement a for loop that computes the approximated value of the rest of Xh: for k=2:N iterations Xh(k)=Xh(k-1)+h F((k-1)*h,Xh(k-1)) end
  • For 1-dimensional problems, you can easily plot the solution by using: Y=linspace(0,T,N iterations) plot(Y,Xh) For 2-dimensional problems you can use: plot(Xh(1,:),Xh(2,:)) For 3-dimensional problems you can use: plot3(Xh(1,:),Xh(2,:),Xh(3,:))
  1. Use your implementation to solve the following problems. Do you see any in- stabilities? Do they dissapear with smaller h? Make sure you take into consideration the dimensionality of each problem.
  • n=1, h=0.1, T=10, F (t, x) = โˆ’ 100 โˆ— x.
  • n=1, h=0.01, T=10, F (t, x) = โˆ’ 100 โˆ— x.
  • n=1, h=0.1, T=10, F (t, x) = cos(t) โˆ— x.
  • (Lotka-Volterra) n=2, h=0.1, T=

F (t, x, y) =

ax โˆ’ bxy โˆ’ cx^2 dy + exy โˆ’ jy^2

with parameters

  • a=2, b=2, c=0, d=-1, e=1, j=
  • a=1, b=8, c=1, d=-1, e=4, j= To handle functions in Matlab or Octave. You can input in the console

F = @(t, x, y) [2 โˆ— x โˆ’ 2 โˆ— x โˆ— y, โˆ’y + 4 โˆ— x โˆ— y]

This function you can then feed into your implementation of forward or backward matlab. I have added the time variable, even if we are not using it.

  1. For the 1-dimensional examples, perform an error analysis. Solve explicitly the ODE and compare the value of the exact solution with the approximated one. For example, for F(t,x)=-100*x, we have

X(t) = X 0 eโˆ’^100 t.

We can feed this into a vector, by doing the following steps

  • Initialize X=zeros(N iterations);
  • Load information for i=1:N iterations X(i)=X0exp(-100 (i-1)*h); end