matlab入门作业

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/beijiafei/article/details/80188562

对以下问题,编写M文件:
(1)用起泡法对10个数由小到大排序. 即将相邻两个数比较,将小的调到前头.

function f = fun7(x)
i = 1;
while i<=10
    j = i+1;
    while j <=10
        if x(i)>x(j)
            temp = x(i);
            x(i) = x(j);
            x(j) = temp;
        end
        j = j + 1;
    end
   i = i + 1;
end
f = x;
end

(2)有一个4x5矩阵,编程求出其最大值及其所处的位置.

function f = fun8(x)
i = 1;
max = realmin;
while i<=4
    j = 1;
    while j<=5
        if max < x(i, j)
            row = i;
            col = j;
            max = x(i, j);
        end
        j = j + 1;
    end
    i = i + 1;
end
f = [max, row, col];% 加上分号后,可保证只输出一个结果,不会f和ans同时输出,这样只有ans输出。
end

(3)编程求 n = 1 20 n !

function f = fun5()
sum = 0;
i = 1;
while i<21
    p = 1;
    j = i;
    while j>0
        p = p * j;
        j = j - 1;
    end
    sum = sum + p;
    i = i + 1;
end
f = sum;

end

(4)一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下. 求它在第10次落地时,共经过多少米?第10次反弹有多高?

function f = fun6()
length = -100;
height = 100;
i = 1;
while i<=10
    length = height * 2 + length;
    height = height / 2;
    i = i+1;
end
f = [length height];
end

(5)有一函数 f ( x , y ) = x 2 + sin ( x y ) + 2 y ,写一程序,输入自变量的值,输出函数值.

function f = fun4( x , y)
%UNTITLED4 此处显示有关此函数的摘要
%   此处显示详细说明
f = x^2 + sin(x*y) + 2*y

end

新手上道,不足之处,请多指教。

猜你喜欢

转载自blog.csdn.net/beijiafei/article/details/80188562