Questions in category: MatLab (MatLab)
软件 >> MatLab

1. [MATLAB]编程计算$\sum_{n=1}^{20}n!$.

Posted by haifeng on 2019-06-29 12:59:07 last update 2020-03-11 17:07:35 | Answers (0) | 收藏


[Exer 1.(3)] 《数学建模与数学实验》

编程计算$\sum_{n=1}^{20}n!$.

 

以下是学生的代码


function Y=myfun3(n)
s=0;
k=1;
for i=1:n
    k=k*i;
    s=s+k;
end
disp(s)


 

使用 Calculator 计算

>>  1!+2!+3!+4!+5!+6!+7!+8!+9!+10!+11!+12!+13!+14!+15!+16!+17!+18!+19!+20!
 

out> 2561327494111820313

 


参见问题2072

2. [MATLAB]有一个4*5矩阵, 编程求出其元素最大值及所处的位置.

Posted by haifeng on 2019-06-29 09:40:26 last update 2019-06-29 11:34:22 | Answers (2) | 收藏


[Exer 1.(2), P.30] 《数学建模与数学实验》

有一个4*5矩阵, 编程求出其元素最大值及所处的位置.

 


以下是学生写的代码, 请改进

function f=zuidazhi(x)
a=1;b=1;c=x(1,1);
for i=1:4
        for j=1:5
           if x(i,j)>c
            a=i;b=j;c=x(i,j);
           end
       end
end
f=[c,a,b]

% for test
% x=rand(4,5)
% zuidazhi(x)

3. [MATLAB]用起泡法(冒泡法)对10个数由小到大进行排序

Posted by haifeng on 2019-06-29 09:06:25 last update 2019-06-29 16:10:46 | Answers (1) | 收藏


[Exer P.30, 1]《数学建模与数学实验》

用起泡法(冒泡法)对10个数由小到大进行排序, 即将相邻两个数进行比较, 小的放到大的前面.


Remark: 当然冒泡法排序不是好的排序算法, 其算法复杂度为 $O(N^2)$.

 

下面是学生写的代码, 请改进

function f=qipaofa(x)
for j=9:-1:1
          for i=1:j
             if(x(i)>x(i+1))
                t=x(i);x(i)=x(i+1);x(i+1)=t;
             end
          end
end
f=x;

% for test
% x=round(10*rand(1,10));


学生2使用C语言编程, 请改进

#include<stdio.h>
main()
{int a[10],i,j,t;
printf("请输入10个数:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);

for(j=0;j<9;j++)
   for(i=0;i<9-j;i++)
            if(a[i]>a[i+1])
            {t=a[i];a[i]=a[i+1];a[i+1]=t;}

            for(i=0;i<10;i++)
                      printf("%3d",a[i]);

            printf("\n");

}

 

4. MatLab 中生成随机数或服从某种分布的随机数矩阵

Posted by haifeng on 2019-03-01 11:11:46 last update 2019-03-01 11:11:46 | Answers (0) | 收藏


rand(m,n) 生成 $m\times n$ 阶随机数矩阵, 矩阵的元素是 $[0,1]$ 之间的等概率分布的随机数.

当 $m=n$ 时, 可直接写 rand(n)

5. 解微分方程组

Posted by haifeng on 2018-05-07 10:40:15 last update 2018-05-07 10:43:11 | Answers (0) | 收藏


解微分方程组

\[
\left\{
\begin{aligned}
y'_1&=y_2 y_3,\\
y'_2&=-y_1 y_3,\\
y'_3&=-0.51y_1 y_2,\\
y_1(0)&=0,\quad y_2(0)=1,\quad y_3(0)=1.
\end{aligned}
\right.
\]
 

6. 求解微分方程 $x''(t)-1000(1-x^2)x'(t)+x=0$.

Posted by haifeng on 2018-05-04 08:29:06 last update 2018-05-07 10:25:31 | Answers (1) | 收藏


求解微分方程

\[
\begin{cases}
\dfrac{d^2 x}{dt^2}-1000(1-x^2)\dfrac{dx}{dt}+x(t)=0,\\
x(0)=0,\quad x'(0)=1.
\end{cases}
\]
 

 


[Hint]

将高阶微分方程转换为与之等价的一阶微分方程组.

 

 

References:

《数学建模与数学实验》(第四版)P.131

 

hello

7. 求下列微分方程组的通解

Posted by haifeng on 2018-05-02 11:01:04 last update 2018-05-02 11:01:44 | Answers (1) | 收藏


求下列微分方程组的通解.

\[
\begin{cases}
\dfrac{dx}{dt}=2x-3y+3z,\\
\dfrac{dy}{dt}=4x-5y+3z,\\
\dfrac{dz}{dt}=4x-4y+2z.\\
\end{cases}
\]

 


References:

《数学建模与数学实验》(第四版)P.130

8. 求下列微分方程 $y''+4y'+29y=0$ 的解.

Posted by haifeng on 2018-05-02 09:53:15 last update 2018-05-02 10:35:37 | Answers (2) | 收藏


求下列微分方程的解.

\[
\begin{cases}
\displaystyle\frac{d^2 y}{dx^2}+4\frac{dy}{dx}+29y=0,\\
y(0)=0,\quad y'(0)=15.
\end{cases}
\]

 


References:

《数学建模与数学实验》(第四版)P.130

9. 求 $\dfrac{du}{dt}=1+u^2$ 的通解.

Posted by haifeng on 2018-05-01 18:52:58 last update 2018-05-02 09:53:28 | Answers (2) | 收藏


求 $\dfrac{du}{dt}=1+u^2$ 的通解.

 

[hint]

使用分离变量法求解.

 


进一步阅读

问题1959

Bernoulli 方程(伯努利方程)

 

References:

《数学建模与数学实验》(第四版)P.130