The function f (x) is sampled using the sampling interval ΔX=Π/5, and the first derivative is approximated by a forward difference quotient, a backward difference quotient, and a center difference quotient of three formulas.
Code in the book:
Percent------------------------------------------------------------------------------percent Output Info about this M-fi lefprintf (' \n****************************************************************\n '); fprintf (' \ n <FDTD 4 Electromagnetics with MATLAB simulations> \ n '); fprintf (' \ n Figure 1.2 \ n '); time_stamp = Datestr (now, 31); [Wkd1, WKD2] = weekday (today, ' long '); fprintf (' Now was%20s, and it is%7s \ n \ nthe ', time_stamp, wkd2);-------- ----------------------------------------------------------------------% Create exact function and its derivativen_ exact = 301; % number of sample points for exact functionx_exact = linspace (0, 6*pi, n_exact); f_exact = sin (x_exact). * EXP ( -0.3*X_EXAC t); f_derivative_exact = cos (x_exact). * EXP ( -0.3*x_exact)-0.3*sin (x_exact). *exp ( -0.3*x_exact);% plot exact Functionfigure (' Numbertitle ', ' off ', ' Name ', ' figure 1.2.a '); set (GCF, ' Color ', ' white '); Plot (X_exacT, F_exact, ' k ', ' linewidth ', 1.5), set (GCA, ' fontsize ', N, ' fontweight ', ' demi '), axis ([0 6*pi-1 1]); Grid On;xlabel (' $x $ ', ' interpreter ', ' latex ', ' fontsize '), Ylabel (' $f (x) $ ', ' interpreter ', ' latex ', ' fontsize ', 16); Title (' exact function ');% create exact function for PI/5 sampleing peroid and% its finite difference derivativesn_a = 31; % number of points for PI/5 sampling period x_a = linspace (0, 6*pi, n_a); % [0, 6pi], row vector with pointsf_a = sin (x_a). * EXP ( -0.3*x_a); f_derivative_a = cos (x_a). * EXP ( -0.3*X_A)-0. 3*sin (x_a). * EXP ( -0.3*x_a);d x_a = pi/5;f_derivative_forward_a = Zeros (1, n_a); % 1x31 Zero matrixf_derivative_backward_a = zeros (1, n_a); f_derivative_central_a = zeros (1, n_a); f_derivative_forward_a (1:n_a-1) = (f_a (2:n_a)-f_a (1:n_a-1))/dx_a;f_derivative_backward_a (2:n_a) = (f_a (2:n_a)-f_a (1:n_a-1))/dx_a;f_ Derivative_central_a (2:n_a-1) = (f_a (3:n_a)-f_a (1:n_a-2))/(2*dx_a);% Create exact function foR PI/10 sampleing peroid and% its finite difference derivativesn_b = 61; % number of points for PI/10 sampling period X_b = linspace (0, 6*pi, n_b); f_b = sin (x_b). * EXP ( -0.3*x_b); f_derivative_b = cos (x_b). * EXP ( -0.3*x_b)-0.3*sin (X_b). * EXP ( -0.3*x_b);d X_b = Pi/10;f_derivative_forward_b = Zeros (1, n_b); F_derivati Ve_backward_b = Zeros (1, n_b); f_derivative_central_b = zeros (1, N_b); F_derivative_forward_b (1:n_b-1) = (F_b (2:N_b)-f_b (1:n_b-1)) /dx_b;f_derivative_backward_b (2:n_b) = (F_b (2:n_b)-f_b (1:n_b-1))/dx_b;f_derivative_central_b (2:N_b-1) = (F_b (3:N_b )-f_b (1:n_b-2))/(2*dx_b);% plot exact derivative of the function and its finite difference% derivatives using PI/5 sampli Ng periodfigure (' numbertitle ', ' off ', ' Name ', ' figure 1.2.b '); set (GCF, ' Color ', ' white '); Plot (x_exact, F_derivative_exact, ' K ', ... x_a (1:n_a-1), f_derivative_forward_a (1:n_a-1), ' b--', ... x_a (2:n_a), f_ Derivative_backward_a (2:n_a), ' R. ', ... x_a (2:n_a-1), F_derivative_central_a (2:n_A-1), ': Ms ', ... ' markersize ', 4, ' linewidth ', 1.5 ', set (GCA, ' fontsize ', ' fontweight ', ' demi '); axis ([0 6*pi-1 1]); Grid on;legend (' exact ', ' forward difference ', ' backward difference ', ' central Difference '); Xlabel (' $x $ ', ' interpreter ' , ' latex ', ' fontsize ', +), Ylabel (' $f ' (x) $ ', ' interpreter ', ' latex ', ' fontsize ', ' + '), text (pi, 0.6, ' $\delta x = \pi/5$ ', ' Interpreter ', ' latex ', ' fontsize ', +, ' backgroundcolor ', ... ' w ', ' Edgecolor ', ' k ');% plot error for finite difference de rivatives% using PI/5 sampling perioderror_forward_a = f_derivative_a-f_derivative_forward_a;error_backward_a = F_deri Vative_a-f_derivative_backward_a;error_central_a = f_derivative_a-f_derivative_central_a;figure (' NumberTitle ', ' Off ', ' Name ', ' figure 1.2.c '); set (GCF, ' Color ', ' white '); Plot (X_a (1:n_a-1), error_forward_a (1:n_a-1), ' b--', ... x_a (2:n_a), error_backward_a (2:n_a), ' r--', ... x_a (2:n_a-1), Error_central_a (2:n_a-1), ': Ms ', ... ' markersize ', 4, ' linewidth ', 1.5 ', set (GCA, ' fontsize ', ' n ', ' FontWeight ', ' Demi '); axis ([0 6*pi-0.2 0.2]); Grid On;legend (' Forward difference ', ' backward difference ', ' central Difference '); Xlabel (' $x $ ', ' interpreter ', ' latex ') , ' fontsize ', +); Ylabel (' Error $[f ' (x)]$ ', ' interpreter ', ' latex ', ' fontsize '), Text (pi, 0.15, ' $\delta x = \pi/5$ ', ' Interpreter ', ' latex ', ' fontsize ', ... ' backgroundcolor ', ' w ', ' Edgecolor ', ' k ');% plot error for finite difference D erivatives% using PI/10 sampling perioderror_forward_b = F_derivative_b-f_derivative_forward_b;error_backward_b = F_de Rivative_b-f_derivative_backward_b;error_central_b = f_derivative_b-f_derivative_central_b;figure (' NumberTitle ', ' Off ', ' Name ', ' figure 1.2.d '); set (GCF, ' Color ', ' white '); Plot (X_b (1:n_b-1), Error_forward_b (1:n_b-1), ' b--', ... X_b (2:n_b), Error_backward_b (2:n_b), ' R ', ... X_b (2:n_b-1), Error_central_b (2:n_b-1), ': Ms ', ... ' markersize ', 4, ' linewidth ', 1.5 ', set (GCA, ' fontsize ', ' fontweight ', ' demi '); Axis ([0 6*pi-0.2 0.2]); Grid On;legend (' Forward difference ', ' backward diFference ', ' central Difference '); Xlabel (' $x $ ', ' interpreter ', ' latex ', ' fontsize '); Ylabel (' Error $[f ' (x)]$ ', ' Interpreter ', ' latex ', ' fontsize ', +), text (pi, 0.15, ' $\delta x = \pi/10$ ', ' interpreter ', ... ' latex ', ' fontsize ', 16, ' BackgroundColor ', ' w ', ' Edgecolor ', ' K ');
Operation Result:
is the function graph, see the amplitude is exponential attenuation. is the exact value of the first derivative (formula calculation) and the approximate result of three Tanesashi quotient. Approximate results of central difference quotient close
Exact value.
Is the comparison of the error between the three Tanesashi quotient approximation and the exact value at the ΔX=Π/5 sampling interval. It can be seen that the center difference quotient approximate error is least.
Is the error comparison between the three Tanesashi quotient approximation and the exact value at the ΔX=Π/10 sampling interval. It can be seen that the center difference quotient approximate error is least. In addition due to the forward difference quotient and
The backward Difference quotient approximation is 1 order accuracy, the center difference quotient approximate is 2 order precision, therefore the sampling interval changes from the Π/5 to the Π/10, the forward difference quotient and the backward difference quotient approximate error becomes the original One-second,
and the center difference quotient approximate error becomes one-fourth.
"FDTD electromagnetic field using MATLAB" Reading notes Figure 1.2