数学物理方程的Matlab实现

教材:《工程数学·数学与物理方程与特殊函数(第四版)》


P25 例题1:对应程序(老师提供)

%picture of string vibration
clc;
clear;
% -----------The boundary conditions of string vibration   ------------------
L_min_u=0;                                              % length of string
L_max_u=10;                                             % length of string
L_delt_u=0.01;                                          % Step of Length
a_string=1;                                             % constant of vibration
%
n_min_Cn=1;                                             % minium number of n for coefficient Cn in u-function
n_max_Cn=7;                                             % maxium number of frequences used for vibration simulation
n_delt_Cn=1;                                            % incresing step of above number

% ********   END of boundary conditions of string vibration  *******
%   ---------initial condition of string vibration ------------------- 
t_min_u=0;                                              % minium number of n for coefficient Cn in u-function
t_max_u=.2;                                             % maxium number of frequences used for vibration simulation
t_delt_u=0.001;                                         % incresing step of above number
num_t=(t_max_u-t_min_u)/t_delt_u+1;                     % Number of time points
%-----------The calulated coefficients of the u-function -----------------
x_grid=L_min_u:L_delt_u:L_max_u;                        % define the x increment of u-function
t_grid=t_min_u:t_delt_u:t_max_u;                        % define the t  increment of u-function
[X_uxt,T_uxt]=meshgrid(x_grid,t_grid);                  % define the x and t grids of u-function


%----------
Cn_u=0;                                                 % the factor of coefficient Cn in u-function
Dn_u=0;                                                 % the factor of coefficient Cn in u-function

% ********   END of calulated coefficients of the u-function  *******
%
%---------------- To plot the vibration picture  ---------------------
n_un=n_min_Cn:n_delt_Cn:n_max_Cn;                       % the factor of coefficient Cn in u-function
t_u_xt=t_min_u:t_delt_u:t_max_u ;                       % the time series of u-function
x_u_xt=L_min_u:L_delt_u:L_max_u ;                       % the space series of u-function
u_xt=0;                                                 % initial value of calulating u_xt
pi_3_5=5*pi*pi*pi;                                      % An constant to be used in following loops;
for i=n_min_Cn:n_delt_Cn:n_max_Cn                       % loop for each frequency related with Cn and Dn
        Cn_u(i)=2*(1-cos(i*pi))/(i*i*i*pi_3_5);         % the Magnutude of each harmonic Cn in u-function
        un_x_u_xt(i,:)=sin(i*pi.*x_grid/10);            % single harmonic wave distribution along X in u-function
        un_t_u_xt(i,:)=cos(10*i*pi.*t_grid);            % vibration curve vs t in u-function  
        un_xt(i,:)=Cn_u(i).*un_x_u_xt(i,:);             % Amplitude modified harmonic wave in u-function
        ux_u_xt=sin(i*pi.*X_uxt./10);                   % the Space distribution of u-function
        ut_u_xt=cos(10*i*pi.*T_uxt);                    % the Time distribution of u-function
        u_xt=u_xt+Cn_u(i).*ux_u_xt.*ut_u_xt;            % the picture of u-function

end

figure(1)                                               % To create the first picture handle
for i=n_min_Cn:n_delt_Cn:n_max_Cn                       % The value n of each harmonic wave
plot(x_grid,un_xt(i,:))                                 % pictures of each harmonic wave curve
hold on                                                 % Keep the picture axe properties
end
plot(x_grid,u_xt(1,:),'r')                              % draw the wave picture of certain moment


figure(2)                                               % To create the second picture handle 
mesh(X_uxt,T_uxt,u_xt)                                  % Draw the two dimension picture of string wave
hold on                                                % Keep the recent picture existent

figure(3)                                               % To create the third picture handle
%axis([L_min_u  L_max_u  -0.3  0.3])
for j=1:1:num_t;                                        % Time points to plot the wave curves
   for i=n_min_Cn:n_delt_Cn:n_max_Cn                    % The value n of each harmonic wave 
       axis([L_min_u  L_max_u  -0.03  0.03])            %To fix the axis scale
       plot(x_grid,un_xt(i,:)*un_t_u_xt(i,j))           % pictures of each harmonic wave curve
       hold on                                          % Keep the recent picture handle existent
   end
   plot(x_grid,u_xt(j,:),'r')                           % Pictures of sum of some waves. 
   pause(0.1)                                           % pause time(second) after each drawing of the all waves.
   hold off                                             % clear the recent picture axe properties
end

P56第五题,课后作业(CLAY)

% CLAY
clc;                                                    % 清除命令行
clear;                                                  % 清除工作区变量
% -----------        使能开关控制定义        ------------------
static_single2D     = 0;                                % 1-开启捕捉一个2D状态
action_2D           = 0;                                % 1-开启2D动画
static_single3D     = 0;                                % 1-开启一个3D视窗
static_multiple3D   = 0;                                % 1-开启多个3D视窗
action_3D           = 1;                                % 1-开启动态3D视窗
% ----------- 有限长杆上的热传导相关变量定义   ------------------
L_min_u=0;                                              % 杆的长度最小值  0 - 题目未设定,这里方便绘制,定义杆的长度为10个单位
L_max_u=10;                                             % 杆的长度最大值 10
L_delt_u=0.01;                                          % 杆的长度步进值 1
a_pole=13;                                              % 杆自身系数 - 修改它对应单个2D状态的衰减
%
n_min_Cn=1;                                             % 定义n的最小值 1
n_max_Cn=7;                                             % 定义n的最大值 7
n_delt_Cn=1;                                            % 定义n的步进值 1
%
t_min_u=0;                                              % 定义时间的起始值 0
t_max_u=.2;                                             % 定义时间的最终值 0.2
t_delt_u=0.001;                                         % 定义时间的步进值 0.001
num_t=(t_max_u-t_min_u)/t_delt_u+1;                     % 计算时间点数 201个时间点
%
x_grid=L_min_u:L_delt_u:L_max_u;                        % 定义二维的长度
t_grid=t_min_u:t_delt_u:t_max_u;                        % 定义二维的时间
[X_uxt,T_uxt]=meshgrid(x_grid,t_grid);                  % 把二维的长度和时间推到三维空间,用以绘制三维网格线
%
Cn_u=0;                                                 % 初始化Cn
Dn_u=0;                                                 % 初始化Dn

% ********  结束 - 有限长杆上的热传导相关变量定义  *******

%---------------- 绘图  ---------------------
n_un=n_min_Cn:n_delt_Cn:n_max_Cn;                       % 定义n
t_u_xt=t_min_u:t_delt_u:t_max_u ;                       % 定义u函数的时间取值
x_u_xt=L_min_u:L_delt_u:L_max_u ;                       % 定义u函数的长度取值
u_xt=0;                                                 % 初始化三维空间的u_xt

for i=n_min_Cn:n_delt_Cn:n_max_Cn                       % 遍历依赖Cn、Dn相关的每一个频率
        Cn_u(i) = (400/(pi*pi*pi))*((1-power(-1,i))/(i*i*i));
        un_x_u_xt(i,:) = sin(i*pi.*x_grid/10);          % 对应带入n值后的二维u函数的长度函数
        un_t_u_xt(i,:) = exp(-(i*pi*a_pole/L_max_u)*(i*pi*a_pole/L_max_u).*t_grid);% 对应带入n值后的二维u函数的时间函数
        un_xt(i,:) = Cn_u(i).*un_x_u_xt(i,:);           % 对应带入n值后的二维u函数
        ux_u_xt=sin(i*pi.*X_uxt./10);                   % 对应带入n值后的三维u函数的长度函数
        ut_u_xt = exp(-(i*pi*a_pole/L_max_u)*(i*pi*a_pole/L_max_u).*T_uxt);% 对应带入n值后的三维u函数的时间函数
        u_xt=u_xt+Cn_u(i).*ux_u_xt.*ut_u_xt;            % 对应带入n值后的三维u函数    
end

% 静态单个2D图
if(static_single2D)
    figure()                                            % 创建图形句柄
    for i=n_min_Cn:n_delt_Cn:n_max_Cn                   % 遍历各个谐波对应的n值
        plot(x_grid,un_xt(i,:))                         % 绘制二维的u函数和x的关系图(t固定)
        hold on                                         % 保持住图形
    end                                                 % 遍历结束
    plot(x_grid,u_xt(1,:),'r')                          % 用红线绘制当前时刻的总的波形 - 用的是三维空间的u_xt(叠加后的un_xt),参数1表示的是1时刻点
end

% 动态2D图
if(action_2D)
    figure()                                            % 创建图形句柄
    for j=1:1:num_t;                                    % 遍历各个时刻点产生动态的效果
       for i=n_min_Cn:n_delt_Cn:n_max_Cn                % 遍历各个谐波对应的n值
           axis([L_min_u  L_max_u  -5  30]);            % x y对应的边界限定
           plot(x_grid,un_xt(i,:)*un_t_u_xt(i,j))       % 绘制二维各个时刻的波形,外侧j控制时刻点
           hold on                                      % 保持住单个时刻点的图形直到hold off
       end
       plot(x_grid,u_xt(j,:),'r')                       % 用红线绘制当前时刻的总的波形
       pause(0.1)                                       % 单个时刻点图形的停留时间
       hold off                                         % 清除单个时刻点的图形
    end
end

% 静态单个3D图
if(static_single3D)
    figure()                                            % 创建图形句柄
    mesh(X_uxt,T_uxt,u_xt)                              % 绘制三维空间对应的各个时刻的波形
    hold on                                             % 保持住图形
end

% 静态多个3D图
if(static_multiple3D)
    figure()                                            % 创建图形句柄

    %杆系数a_pole = 1;
    u_xt = 0;                                           % 出于程序的严谨性,因为前面操作过了u_xt的3D情况,避免对本次3D产生叠加影响,先清0
    for i=n_min_Cn:n_delt_Cn:n_max_Cn                   % 遍历各个谐波对应的n值
        ux_u_xt=sin(i*pi.*X_uxt./10);                   % 对应带入n值后的三维u函数的长度函数
        ut_u_xt = exp(-(i*pi*1/L_max_u)*(i*pi*1/L_max_u).*T_uxt);% 对应带入n值后的三维u函数的时间函数
        u_xt=u_xt+Cn_u(i).*ux_u_xt.*ut_u_xt;            % 对应带入n值后的三维u函数    
    end
    subplot(1,3,1);                                     % 该图形位于同一个figure下的第一个
    mesh(X_uxt,T_uxt,u_xt)                              % 绘制三维空间对应的各个时刻的波形
    title('杆系数为 1');                                 % 添加标题更加直观

    %杆系数a_pole = 10;
    u_xt = 0;                                           % 出于程序的严谨性,因为前面操作过了u_xt的3D情况,避免对本次3D产生叠加影响,先清0
    for i=n_min_Cn:n_delt_Cn:n_max_Cn                   % 遍历各个谐波对应的n值
        ux_u_xt=sin(i*pi.*X_uxt./10);                   % 对应带入n值后的三维u函数的长度函数
        ut_u_xt = exp(-(i*pi*10/L_max_u)*(i*pi*10/L_max_u).*T_uxt);% 对应带入n值后的三维u函数的时间函数
        u_xt=u_xt+Cn_u(i).*ux_u_xt.*ut_u_xt;            % 对应带入n值后的三维u函数    
    end
    subplot(1,3,2);                                     % 该图形位于同一个figure下的第二个
    mesh(X_uxt,T_uxt,u_xt)                              % 绘制三维空间对应的各个时刻的波形
    title('杆系数为 10');                                % 添加标题更加直观

    %杆系数a_pole = 100;
    u_xt = 0;                                           % 出于程序的严谨性,因为前面操作过了u_xt的3D情况,避免对本次3D产生叠加影响,先清0
    for i=n_min_Cn:n_delt_Cn:n_max_Cn                   % 遍历各个谐波对应的n值
        ux_u_xt=sin(i*pi.*X_uxt./10);                   % 对应带入n值后的三维u函数的长度函数
        ut_u_xt = exp(-(i*pi*100/L_max_u)*(i*pi*100/L_max_u).*T_uxt);% 对应带入n值后的三维u函数的时间函数
        u_xt=u_xt+Cn_u(i).*ux_u_xt.*ut_u_xt;            % 对应带入n值后的三维u函数    
    end
    subplot(1,3,3);                                     % 该图形位于同一个figure下的第三个
    mesh(X_uxt,T_uxt,u_xt)                              % 绘制三维空间对应的各个时刻的波形
    title('杆系数为 100');

    suptitle('杆的系数对温度耗散的演示');                 % 添加总标题
    hold on                                             % 保持住图形
end

%动态3D(改变杆的系数)
if(action_3D)
    figure()                                            % 创建图形句柄
    for j=1:1:100;                                      % 遍历a_pole 从1 到 100
        u_xt = 0;                                       % 出于程序的严谨性,因为前面操作过了u_xt的3D情况,避免对本次3D产生叠加影响,先清0
        for i=n_min_Cn:n_delt_Cn:n_max_Cn               % 遍历各个谐波对应的n值
            ux_u_xt=sin(i*pi.*X_uxt./10);               % 对应带入n值后的三维u函数的长度函数
            ut_u_xt = exp(-(i*pi*j/L_max_u)*(i*pi*j/L_max_u).*T_uxt);% 对应带入n值后的三维u函数的时间函数
            u_xt=u_xt+Cn_u(i).*ux_u_xt.*ut_u_xt;        % 对应带入n值后的三维u函数    
        end
        mesh(X_uxt,T_uxt,u_xt)                          % 绘制三维空间对应图形
        pause(0.1)                                      % 每个a_pole值间的停留间隔
        hold off                                        % 为了显示动态效果,清除当前对应a_pole的显示
    end   
end

% ********  结束 - 绘图  *******

猜你喜欢

转载自blog.csdn.net/recclay/article/details/80379163