# 有限差分(下)
---
## 開檔/關檔
fopen - 開檔
fclose - 關檔
---
```
fid = fopen(filemame,permission)
```
### permssion
| r | w | a | a+ |
| -------- | -------- | -------- | -------- |
| 只讀取檔案 | 只寫入檔案 | 只准加入檔案 | 可讀取及加入檔案 |
---
* 若要加入 binary 檔,則permission 必須是'rb'
* 每次使用 fopen 時, 都測試其傳回fid是否為有效值
**fid=1** -標準輸出
**fid=2** -標準錯誤輸出
* 完成檔案讀寫之後,需使用`fclosed`關閉檔案`status=fclosed(fid);`
**若關檔順利,`status=0`**
**若無法關閉檔案,`status=1`**
---
ex.
```
[fid,message] = fopen('fopen.o2.m','r');
fprint('fid= %d\n',fid);
fprintf('message= %s\n',message);
fclose(fid);
```
---
## 格式化資料
`fscanf` -從檔案讀取格式化資料
`fprintf` -從格式化資料寫入檔案
`sscanf` -從字串讀取格式化資料
`sprintf`將式化資料寫至字串
---
## function handle(@)
Handle used in calling function inderictly
### Syntax
handle = @function name
handle = @carglist , anoymous_function
### Description
handle = @functionname returns ahamdle to the specified MATLAB function
**ex.**
```matlab
function [s,p] =get_handles;
s= @mysubfun;
p= @myprivatefun;
functioon mysubfun
disp 'Exicuting subfunction mysubfun'
Function main()
...initial input (fun)
for i=1,...
w( , )=start(x,t,...@f,@g,...)
for j =1,...
w( , )=step(x,t,...,@f,@g,...)
return
end
function start
...
return
end
function step
...
return
end
```
---
## Boudary Condition
### Dirichlet type
u(0,t)=0 ==> a fixed termination 指定微分方程的解在邊界處的值
### Neumann type
指定微分方程的解在邊界處的微分
---
### Absorbing boundary
Ut vanish at the boundary, which is in fact implied by the more realistic condition.
A simple lossy condition is given by the so called ***Robin type condition***
$$ u_t(0,t) = \alpha u_x(0,t) $$ , for some constant a>0
implying that the energy is in fact monotonically decreasing.
$$ \frac {\partial ^2 u}{\partial t^2}-\frac {\partial ^2 u}{\partial x^2}=f , x>0,t>0$$
let the forcinf vanish outside a bounded region next to the left boundar, that is let f(x,t)=0 for x>=L, and for all time t>0.
## One-Dimensional case
wave equation
$$ \frac{1}{C^2} \frac{\partial ^2 u}{\partial t^2}=\frac{\partial ^2 u}{\partial x^2}$$
where c denotes the velocity of medium we seek boundary conditions ar x= +-a , such that waves incident ar x= +-a will pass throught these boudaries with no reflection.
The boudary conditions
$$u(\pm a,t)=0$$
$$\frac{\partial u}{\partial x}(\pm a,t)=0$$
result in the reflaction of waves incident at x=+-a.
Suppose the desired solution is a plane wave propagating to the right fiven by
$$u=\exp (i(\omega t-kx))$$
where w is the angular, k is the wavenumber
$$\omega =kc=k\frac{\lambda}{T}$$
Then the solution of equation-1 for x<=a is given by
$$u=\exp (i(\omega t-kx))+R\exp (i(\omega t+kx))-----------(4)$$
where R is the reflection coefficient.
substituting (4) into the boudary equation (2) and (3) and solving for R
we find that $|R|=1$
the amplitude of reflected wave at $x=a$ is the same as the amplitude of the incident wave.
Similary, a plane wave traveling to the left will reflect from the boundary $x=a$ and the magnitude of the reflection coefficient will be unity.
it is caused by the fact that the boundary condition (2) (3) are correct.
To eliminate this reflection, we replace the boundary condition, we replace the boundary condition (2) and (3)
$$\frac{1}{c} \frac{\partial u(-a,t)}{\partial t}-\frac{\partial u(-a,t)}{\partial x}=0------(5)$$
and $$\frac{1}{c} \frac{\partial u(a,t)}{\partial t}+\frac{\partial u(a,t)}{\partial x}=0------(6)$$
$\because \frac{1}{C^2} \frac{\partial ^2 u}{\partial t^2}-\frac{\partial ^2 u}{\partial x^2}=(\frac{1}{c} \frac{\partial u(-a,t)}{\partial t}-\frac{\partial u(-a,t)}{\partial x})(\frac{1}{c} \frac{\partial u(a,t)}{\partial t}+\frac{\partial u(a,t)}{\partial x})$積化和差
$\because$ a plane wave $u(x,t)$ propagation to the right satisfies, $(\frac{1}{c} \frac{\partial}{\partial t}+\frac{\partial }{\partial x})u=0$,
and a plane wave $u(x,t)$ propagation to the right satisfies, $(\frac{1}{c} \frac{\partial}{\partial t}-\frac{\partial }{\partial x})u=0$
$\because$ $k=\rho c^2 , \rho \text{:density}$
$$\rho c \frac{\partial u}{\partial t}=-k\frac{\partial u}{\partial x}------(7)$$
Thus, (7) statisfies that the right boundary, the pressure is equalized so that the wave pass through the right boundary with **no reflection**.
If we substitude u as given by (4) into (6) and solve for R, $\Rightarrow R=0$
Similarly, a plane wave travaling to the left will not reflect from the boundary $x=-a$
$$(\frac{1}{c} \frac{\partial}{\partial t}+\frac{\partial }{\partial x})u=0,x=L ,t>0$$
\begin{array}{ll}
u_t = u_x , x=0 \\
u_t = -u_x , x=L
\end{array}