慕工大数值分析第一课第二题

clc;
clear;

%% 1.2.1 创建一个1000个0到1数所构成的随机向量

a = rand(1, 1000);
disp(a)

%% 1.2.2 判断a的第一个数是大于0.5还是小于0.5

if a(1) >= 0.5
    disp('a1 >= 0.5')
else
    disp('a1 < 0.5')
end

%% 1.2.3 给出ai大于0.5的数目

m = numel(a(a >= 0.5));
disp(m)

%% 1.2.4 找出ai小于0.499大于0.501的数和index,如果不存在就说没有

total = 0;
for index = 1:1000
    if (a(index) <= 0.501) && (a(index) >= 0.499)
        disp(['index: ' , num2str(index) , ' and ' , 'wert ' , num2str(a(index))])
    else
        total = total + 1;
    end
end

if total == 1000
    disp('Kein Element 0.499 <= a_i <= 0.501')
end

%% 1.2.6 测试n=0和n=5的斐波拉契数列

y_1 = facultaet(0);
y_2 = facultaet(5);
disp(y_1)
disp(y_2)

function [y] = facultaet(n)
y = 1;
if n == 0
    y = 1;
else
    for i = 1:n
        y = y * i;
    end
end
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值