《MATLAB编程》测试题
院系:计算机科学与技术学院 年级: 专业:计算机科学与技术
学号: 姓名:
在下面各题的题后分别贴出MATLAB代码及相应的代码结果:
目录
1、... 1
2、... 4
3、... 5
4、... 6
5、... 7
6、... 8
7、... 12
8、... 13
9、... 15
10、... 16
设求AB、BTA、A-1与A-1B,求A2、A的2次数幂,并求矩阵A的秩、转置和行列式。
代码:
A = [21 2 3; 4 35 6; 7 8 49];
B = [3; 2; 1];
ans1 = A * B
ans2 = B' * A
ans3 = inv(A)
ans4 = A \ B
ans5 = A^2
ans6 = A.^2
ans7 = rank(A)
ans8 = A'
ans9 = det(A)
结果:
ans1 =
70
88
86
ans2 =
78 84 70
ans3 =
0.0489 -0.0022 -0.0027
-0.0045 0.0296 -0.0033
-0.0063 -0.0045 0.0213
ans4 =
0.1398
0.0423
-0.0065
ans5 =
470 136 222
266 1281 516
522 686 2470
ans6 =
441 4 9
16 1225 36
49 64 2401
ans7 =
3
ans8 =
21 4 7
2 35 8
3 6 49
ans9 =
34060
分别对A=[11, 12, 13]、A=[11; 12; 13]和A=diag([11 12 13]),求diag(diag(A))。
代码:
A = [11, 12, 13];
ans1 = diag(diag(A))
A = [11; 12; 13];
ans2 = diag(diag(A))
A = diag([11 12 13]);
ans3 = diag(diag(A))
结果:
ans1 =
11
12
13
ans2 =
11
12
13
ans3 =
11 0 0
0 12 0
0 0 13
若x=[1, 3], y=[2, 4],[X, Y]=meshgrid(x, y),写出X与Y。
代码:
x = [1, 3];
y = [2, 4];
[X, Y] = meshgrid(x, y)
结果:
X =
1 3
1 3
Y =
2 2
4 4
设A=[6, 4; 5, 3; 2, 1],则求reshape(A, 2, 3)。
代码:
A = [6, 4; 5, 3; 2, 1];
reshape(A, 2, 3
结果:
ans =
6 2 3
5 4 1
求矩阵的特征值和特征向量。
代码:
A = [10, 2, 3; 2, 9, 4; 3, 4, 8];
[V,D] = eig(A)
结果:
V =
0.2113 0.7887 0.5774
0.5774 -0.5774 0.5774
-0.7887 -0.2113 0.5774
D =
4.2679 0 0
0 7.7321 0
0 0 15.0000
编写一个判断闰年的函数来判断从2010年到2100年之间有哪些年份是闰年。(闰年可以被4整除但不能被100整除,或者可以被400整除)
代码:
for i = 2010 : 2100
if IsLeapYear(i) == true
fprintf('%d is a leap year\n', i);
else
fprintf('%d is not a leap year\n', i);
end
end
%判断year是否是闰年
%是闰年则返回true,否则返回false
function result = IsLeapYear(year)
if (mod(year, 4) == 0 && mod(year, 100) ~= 0) || mod(year, 400) == 0
result = true;
else
result = false;
end
end
结果:
2010 is not a leap year
2011 is not a leap year
2012 is a leap year
2013 is not a leap year
2014 is not a leap year
2015 is not a leap year
2016 is a leap year
2017 is not a leap year
2018 is not a leap year
2019 is not a leap year
2020 is a leap year
2021 is not a leap year
2022 is not a leap year
2023 is not a leap year
2024 is a leap year
2025 is not a leap year
2026 is not a leap year
2027 is not a leap year
2028 is a leap year
2029 is not a leap year
2030 is not a leap year
2031 is not a leap year
2032 is a leap year
2033 is not a leap year
2034 is not a leap year
2035 is not a leap year
2036 is a leap year
2037 is not a leap year
2038 is not a leap year
2039 is not a leap year
2040 is a leap year
2041 is not a leap year
2042 is not a leap year
2043 is not a leap year
2044 is a leap year
2045 is not a leap year
2046 is not a leap year
2047 is not a leap year
2048 is a leap year
2049 is not a leap year
2050 is not a leap year
2051 is not a leap year
2052 is a leap year
2053 is not a leap year
2054 is not a leap year
2055 is not a leap year
2056 is a leap year
2057 is not a leap year
2058 is not a leap year
2059 is not a leap year
2060 is a leap year
2061 is not a leap year
2062 is not a leap year
2063 is not a leap year
2064 is a leap year
2065 is not a leap year
2066 is not a leap year
2067 is not a leap year
2068 is a leap year
2069 is not a leap year
2070 is not a leap year
2071 is not a leap year
2072 is a leap year
2073 is not a leap year
2074 is not a leap year
2075 is not a leap year
2076 is a leap year
2077 is not a leap year
2078 is not a leap year
2079 is not a leap year
2080 is a leap year
2081 is not a leap year
2082 is not a leap year
2083 is not a leap year
2084 is a leap year
2085 is not a leap year
2086 is not a leap year
2087 is not a leap year
2088 is a leap year
2089 is not a leap year
2090 is not a leap year
2091 is not a leap year
2092 is a leap year
2093 is not a leap year
2094 is not a leap year
2095 is not a leap year
2096 is a leap year
2097 is not a leap year
2098 is not a leap year
2099 is not a leap year
2100 is not a leap year
编写一个计算阶乘的函数,分别计算1到10内所有正整数的阶乘。
代码:
for i = 1 : 10
fprintf('%d! = %d\n', i, GetFactorial(i));
end
%求一个数的阶乘
function result = GetFactorial(num)
result = 1;
for i = 2 : num
result = result .* i;
end
end
结果:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
设y = sin (2x)+n, 其中n为服从标准正态分布的随机数,画出区间[0, 2*pi]上等间隔取20个点的y的散点图,并用2次多项式对这些点进行拟合,画出拟合曲线。
代码:
n = normrnd(0,1,1,20);
x = 0: 2*pi/19: 2*pi; %generate 20 points
y = sin(2 * x) + n;
plot(x, y, 'b*');
hold on
p = polyfit(x, y, 2);
t = 0: 0.003: 2*pi;
y = polyval(p, t);
plot(t, y, 'r');
grid on;
xlabel('x');
ylabel('y');
title('quadratic polyfit');
set(gca, 'XTick', 0: pi/2: 2*pi);
set(gca, 'XTickLabel', {'0', 'pi/2', 'pi', 'pi*3/2', '2*pi'});
结果:
画图判断函数是否为周期函数。
代码:
x = -4*pi: 0.005: 4*pi;
y = sin(x) + cos(2 * x);
plot(x, y, 'r');
grid on;
xlabel('x');
ylabel('y');
title('y = sin(x) + cos(2 * x)');
set(gca, 'XTick', -4*pi: pi: 4*pi);
set(gca, 'XTickLabel', {'-2*pi', '-pi*3/2', '-pi', '-pi/2', '0', 'pi/2', 'pi', 'pi*3/2', '2*pi'});
结果:
在同一图形中作出向零取整函数和函数的图形,用不同的线型、点型和颜色来表示并标记出来。
代码:
hold on
fplot('fix(x)',[-4, 4], 'm:*');
fplot('x - fix(x)',[-4, 4], 'b-.');
legend('y=[x]', 'y=x-[x]', 'Location', 'NorthEastOutside');
xlabel('x');
ylabel('y');
结果: