数学物理方程的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

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

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
数学物理方程是描述自然界中各种现象和规律的数学表示方式。它们常常包含未知数、常数以及各种数学运算,通过求解这些方程,我们可以得到自然界中的定量规律。 Matlab是一种强大的数学软件工具,它可以用来求解各种数学物理方程。它提供了各种优化算法和数值计算方法,可以对不同类型的方程进行求解。 对于线性方程组,我们可以使用Matlab的解线性方程组的函数来求解,比如: ```matlab A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; b = [1; 2; 3]; x = A\b; ``` 这段代码中,A是系数矩阵,b是常数项矩阵,x是未知数向量。通过A\b运算,我们可以得到线性方程组的解x。 对于非线性方程,我们可以使用Matlab的数值求解函数,比如fsolve。例如,我们可以用fsolve来求解非线性方程x^2 - exp(x) = 0: ```matlab fun = @(x) x^2 - exp(x); x0 = 1; % 初始猜测值 x = fsolve(fun, x0); ``` 这段代码中,fun是非线性方程的定义,x0是初始猜测值,x是方程的解。 除了求解方程Matlab还可以进行符号计算。对于符号计算,我们可以使用Matlab的符号计算工具箱。例如,我们可以使用符号计算工具箱来求解微分方程: ```matlab syms x(t); ode = diff(x, t) == x^2 - sin(t); xSol(t) = dsolve(ode); ``` 这段代码中,x(t)表示未知函数,ode是微分方程的表达式,dsolve是符号计算工具箱中用来求解微分方程的函数。 综上所述,Matlab是一个强大的数学软件工具,可以用来求解各种类型的数学物理方程。无论是线性方程组、非线性方程还是微分方程Matlab都提供了相应的函数和工具箱来进行求解。这使得我们可以更加方便地研究和应用数学物理方程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReCclay

如果觉得不错,不妨请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值