本文收录了青岛大学几届的Matlab竞赛的试题的参考代码,本文仅包含代码部分,完整试题见作者另一篇blog
代码为本人自己书写,可能不是最优解法,敬请谅解。
- 第二届matlab竞赛初赛 2017年4月8日(周六)【网上可找到参考代码,本文仅包含部分代码】
- 第二届matlab竞赛决赛 2017年 【本文包含全部代码】
- 第五届matlab竞赛初赛 2021年6月5日(周六)【笔试题,本文暂不提供参考代码】
- 第五届matlab竞赛决赛 2021年6月13日(周日)【本文包含全部代码】
(2020年疫情原因停办一届)
允许转载,转载请注明出处。
青岛大学matlab应用竞赛
1.第二届初赛(2017年4月8日)
A = floor(100*rand(5,4));
i1 = find(mod(A, 3) == 0);
S1 = sum(A(i1));
M1 = max(max(A))
M2 = min(min(A))
% M2 = min(A(:))
i2 = find(A>10)
(A(i2))'
clear
syms x
f(x) = x^2*atan(x);
dy2 = diff(f(x), 2);
subs(dy2, x, pi/4)
S = int(f(x), 0, pi/4)
略
略
略
略
略
略
略
略
略
2.第二届决赛
x=-3:0.1:3;
y=-3:0.1:3;
[X,Y] = meshgrid(x,y);
Z1=X.^2+2*Y.^2;
Z2=6-2*X.^2-Y.^2;
t=linspace(0,2*pi);
x1=sqrt(2)*cos(t);
y1=sqrt(2)*sin(t);
z1=x1.^2+2*y1.^2;
mesh(X,Y,Z1); hold on;
mesh(X,Y,Z2); hold on;
plot3(x1,y1,z1,'r', 'LineWidth',2)
function clen = curlen(f,g,t,a,b)
df = diff(f,t);
dg = diff(g,t);
clen = int(sqrt(df.^2+dg.^2), [a,b]);
end
clear; clc
syms t
k=1:10;
lens = zeros(1,10);
for i=1:10
x = 2*t-1;
y = k(i)*t^2-t;
lens(i) = curlen(x,y,t,0,1);
end
lens
clear; clc
syms x y
f(x,y) = (x+y)^2*sin(x+2*y)*exp(2*y);
g(x)=int(f,y,0,x);
h(x)=int(f,y,0,2);
x=-1:0.01:1;
gy=g(x);
hy=h(x);
plot(x,gy); figure
plot(x,hy); figure
plot(x,gy, x, hy)
legend('g(x)','h(x)')
(i)解析解
%解析解 solution
clear; clc
syms x y
y = dsolve('x^2*D2y+x*Dy+(x^2-0.25)*y=0','y(pi/2)=2, Dy(pi/2)=-2/pi','x')
xi = pi/64:pi/64:pi; %xi从0 开始,会出现 除0错误
yi=zeros(size(xi));
for i=1:64
yi(i) = subs(y,x,xi(i));
end
plot(xi,yi)
% y = (2*sin(x)*(pi/2)^(1/2))/x^(1/2)
%计算y(0) = 0.5551. y'(0) = 4.6651 , 用于求数值解
y0 = yi(1)
dy0 = (yi(2) -yi(1))/(xi(2)-xi(1))
解析解:y=(2sin(x)(pi/2)(1/2))/x(1/2)
初值:y(0)= 0.5551; dy(0)=4.6651
(ii)数值解
使用第一问解析解计算的初值
% 定义 odefun1.m文件
function dy = odefun1(x,y)
dy = zeros(2,1);
dy(1)=y(2);
dy(2)=-y(1)+0.25*y(1)^2/x^2-y(2)/x;
end
tspan = [pi/64, pi]; %ts = [0, pi] 不行
y0=[0.555, 4.67]; %可也取值 [0, 4.67], 但是y的取值严重不符合实际, y max 小于1。实际 y max 大约为2
[x2,y2]=ode45('odefun1',tspan,y0);
plot(x2,y2(:,1)); figure
plot(x2,y2(:,1), x2, y2(:, 2));
legend("y", "dy")
解析法得到的图与数值解得到的图几乎一致。
clear; clc
xi=[-1.000,-0.900, -0.470,-0.100, 0.500, 1.020, 1.800, 2.300, 2.950, 3.450, 3.600];
yi =[0.03 0.04 0.04 0.03 0.07 0.27 0.72 0.49 0.07 0.03 0.02 ];
normf = @(x, xi) (1/sqrt(2*pi))/x(2).*exp((-(xi-x(1)).^2)/(2*x(2)^2)); %注意点乘,不然报错
x = lsqcurvefit(normf, [1,1],xi,yi)
% x = 1.7996 0.5570
plot(xi, yi, 'o'); hold on
xn = linspace(xi(1),xi(end));
yn=normf(x,xn);
plot(xn,yn,'o',xn,yn);
略
clear; clc;
x = [0 0.20 0.40 0.60 0.80 1.00 1.20 1.40 1.60 1.80 2.00 ];
y = [0.10 0.30 0.50 0.70 0.90 1.10 1.30 1.50 1.70 1.90 2.00 ];
z=[0.03 0.20 0.97 2.34 4.31 6.88 10.05 13.82 18.19 23.16 28.73
0.27 0.18 0.69 1.80 3.51 5.82 8.73 12.24 16.35 21.06 26.37
0.75 0.40 0.65 1.50 2.95 5.00 7.65 10.90 14.75 19.20 24.25
1.47 0.86 0.85 1.44 2.63 4.42 6.81 9.80 13.39 17.58 22.37
2.43 1.56 1.29 1.62 2.55 4.08 6.21 8.94 12.27 16.20 20.73
3.63 2.50 1.97 2.04 2.71 3.98 5.85 8.32 11.39 15.06 19.33
5.07 3.68 2.89 2.70 3.11 4.12 5.73 7.94 10.75 14.16 18.17
6.75 5.10 4.05 3.60 3.75 4.50 5.85 7.80 10.35 13.50 17.25
8.67 6.76 5.45 4.74 4.63 5.12 6.21 7.90 10.19 13.08 16.57
10.83 8.66 7.09 6.12 5.75 5.98 6.81 8.24 10.27 12.90 16.13
12.00 9.70 8.00 6.90 6.40 6.50 7.20 8.50 10.40 12.90 16.00 ];
% mesh(x, y, z); figure;
xi = 0:0.05:2;
[xn,yn] = meshgrid(xi,xi);
zn = interp2(x,y,z,xn,yn);
mesh(xn,yn,zn); figure;
v=[2,5,10,15,20];
% v = 2:2:20;
[C,h] = contour(x,y,z, v); %x,y的值为 x,y轴
% [C,h] = contour(z, v); %高度值为 x,y轴
clabel(C,h); % 在图上标记等高线的值
%曲面与平面的交线 并求交线长度
x1 = 0:0.05:1;
z1 = 0:30;
[x11, z11] = meshgrid(x1, z1);
y11 = -2*x11 + 2;
mesh(xn,yn,zn); hold on;
mesh(x11,y11,z11);
%求曲面的表面积
ssum = 0;
lenz = length(z);
for i = 3:lenz
for j = 1:lenz
ssum = ssum + zn(i,j);
end
end
ssum
ssum*0.05*0.05
3.第五届初赛 (2021年6月5日)
由于是线下笔试,无法提供参考代码。
4.第五届决赛 (2021年6月13日)
clc; clear;
str = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way--in short, the period was so. far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.There were a king with a large jaw and a queen with a plain face, on the throne of England; there were a king with a large jaw and a queen with a fair face, on the throne of France. In both countries it was clearer than crystal to the lords of the State preserves of loaves and fishes, that things in general were settled for ever. It was the year of Our Lord one thousand seven hundred and seventy-five. Spiritual revelations were conceded to England at that favoured period, a sat this. Mrs. Southcott had recently attained her five-and-twentieth blessed birthday, of whom a prophetic private in the Life Guards had heralded the sublime appearance by announcing that arrangements were made for the swallowing up of London and Westminster. Even the Cock-lane ghost had been laid only a round dozen of years, after rapping out its messages, as the spirits of this very year last past (supernaturally deficient in originality) rapped out theirs. Mere messages in the earthly order of events had lately come to the English Crown and People, from a congress of British subjects in America: which, strange to relate, have proved more important to the human race than any communications yet received through any of the chickens of the Cock-lane brood. France, less favoured on the whole as to matters spiritual than her sister of the shield and trident, rolled with exceeding smoothness down hill, making paper money and spending it. Under the guidance of her Christian pastors, she entertained herself besides, with such humane achievements as sentencing a youth to have his hands cut off, his tongue torn out with pincers, and his body burned alive, because he had not kneeled down in the rain to do honour to a dirty procession of monks which passed within his view, at a distance of some fifty or sixty yards. It is likely enough that, rooted in the woods of France and Norway, there were growing trees, when that sufferer was put to death, already marked by the Woodman, Fate, to comedown and be sawn into boards, to make a certain movable framework with a sack and a knife in it, terrible in history. It is likely enough that in the rough outhouses old some tillers of the heavy lands adjacent to Paris, there were sheltered from the weather that very day, rude carts, be spattered with rustic mire, snuffed about by pigs, and roosted in by poultry, which the Farmer, Death, had already set apart to be his tumbrils of the Revolution. But that Woodman and that Farmer, though they work unceasingly, work silently, and no one heard them as they went about with muffled tread: the rather, for as much as to entertain any suspicion that they were awake, was to be atheistical and traitorous.In England, there was scarcely an amount of order and protection to justify much national boasting. Daring burglaries by armed men, and highway robberies, took place in the capital itself every night; families were publicly cautioned not to go out of town without removing their furniture to upholsterers warehouses for security; the highwayman in the dark was a City tradesman in the light, and, being recognised and challenged by his fellow-tradesman whom he stopped in his character of the Captain, gallantly shot him through the head and rode away; the mail was waylaid by seven robbers, and the guard shot three dead, and then got shot dead himself by the other four, in consequence of the failure of his ammunition: after which the mail was robbed in Peace; that magnificent potentate, the Lord Mayor of London, was made to stand and deliver on Turnham Green, by one highwayman, who despoiled the illustrious creature insight of all his retinue; prisoners in London gaols fought battles with their turnkeys, and the majesty of the law fired blunderbusses in among them, loaded with rounds of shot and ball; thieves snipped off diamond crosses from the necks of noble lords at Court drawing-rooms; musketeers went into St. Giless, to search for contraband goods, and the mob fired on the musketeers, and the musketeers fired on the mob, and nobody thought any of these occurrences much out of the common way. In the midst of them, the hangman, ever busy and ever worse than useless, was in constant requisition; now, stringing up long rows of miscellaneous criminals; now, hanging a house-breaker on Saturday who had been taken on Tuesday; now, burning people in the hand at Newgate by the dozen, and now burning pamphlets at the door of Westminster Hall; to-day, taking the life of an atrocious murderer, and to-morrow of a wretched pilferer who had robbed a farmers boy of sixpence.All these things, and a thousand like them, came to pass in and close upon the dear old year one thousand seven hundred and seventy-five. Environed by them, while the Woodman and the Farmer worked unheeded, those two of the large jaws, and those other two of the plain and the fair laces, trod with stir enough, and carried their divine rights with a high hand. Thus did the year one thousand seven hundred and seventy-five conduct their Greatnesses, and myriads of small creatures--the creatures of this chronicle among the rest--along the roads that lay before them.";
% A = count(str, ["A"])
% d = count(str, ["d"])
sm = ["A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
s = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
cnt = zeros(1,26);
for i = 1:26
cnt(i) = count(str, [s(i)]);
cnt(i) = cnt(i) + count(str, sm(i));
end
bar(cnt); figure;
cnt1 = cnt/sum(cnt)
bar(cnt1)
clear;
x = [-3 -2.4000 -1.80000 -1.20 -0.600 0 0.600 1.200 1.800 2.400 3];
y = [0.10 0.1479 0.2358 0.409 0.7352 1 0.7352 0.4098 0.2358 0.1479 0.1];
k = polyfit(x,y,4);
xi = -3:0.1:3;
yi=polyval(k,xi);
plot(x,y,'o',xi,yi)
本题我用的是图形学中齐次坐标的思想,当不完全是齐次坐标
clear;
%3-1 ------------------------------------------------------
%单位球, sphere函数的图太粗糙,我打算自己画
% [x,y,z] = sphere;
% mesh(x,y,z)
% axis equal;
%3-2 ------------------------------------------------------
%单位球
t = 0:0.01:2*pi;
r = 0:0.01:1;
[mr, mt] = meshgrid(r,t);
tx = mr.*cos(mt);
ty = mr.*sin(mt);
z1 = real(sqrt(1-(tx.^2 + ty.^2)));
z2 = real(-sqrt(1-(tx.^2 + ty.^2)));
% % mesh(x,y,z1); hold on
% % mesh(x,y,z2)
% % axis equal
%上下球面拼接,存储在一个矩阵里
x = [tx,tx];
y = [ty,ty];
z = [z1,z2];
mesh(x,y,z)
axis equal
title('单位球')
xlabel('x'); xlim([-1.2,1.2]);
ylabel('y'); ylim([-1.2,1.2]);
zlabel('z'); zlim([-1.2,1.2]);
%3-3 ------------------------------------------------------
%将x,y,z组合成一个3维的整体, 便于与变换矩阵的乘法
[m,n] = size(x); % 629 x 202
x = reshape(x, 1, numel(x));
y = reshape(y, 1, numel(y));
z = reshape(z, 1, numel(z));
%
XYZ = zeros(3,numel(x));
XYZ = [x;y;z];
%3-4 ------------------------------------------------------
% z轴压缩1/2
Trans1 = [1,0, 0;
0,1, 0;
0,0,1/2];
ans1 = Trans1 * XYZ;
% mesh(ans1) %忘了reshape, 这个图挺好看的
tx = reshape(ans1(1,:),m,n);
ty = reshape(ans1(2,:),m,n);
tz = reshape(ans1(3,:),m,n);
figure;
mesh(tx,ty,tz)
title('单位球在 z轴缩短1/2')
xlabel('x'); ylabel('y'); zlabel('z');
axis equal
%3-5 ------------------------------------------------------
% 单位球 在z=0, y = x方向变为原来的2/3
f = @(t) [cos(t), sin(t),0;
-sin(t), cos(t),0;
0, 0, 1];
Mr45 = f(pi/4); % 顺时针45°
Mr_45 = f(-pi/4); % 逆时针45°
M2 = [2/3, 0, 0; 0, 1, 0; 0, 0, 1];
% 先顺时针45, 缩短为2/3, 逆时针45回去, 注意左乘的顺序。下同
Trans2 = Mr_45 * M2 * Mr45;
ans2 = Trans2 * XYZ;
tx = reshape(ans2(1,:),m,n);
ty = reshape(ans2(2,:),m,n);
tz = reshape(ans2(3,:),m,n);
%划线 y=x, z=0
tmpx = -2:0.01:2;
tmpz = zeros(size(tmpx));
figure;
mesh(tx,ty,tz); hold on;
plot3(tmpx,tmpx,tmpz,'Color','g','LineWidth',2)
title('单位球在 y=x, z=0 缩短为2/3')
legend('结果','y=x, z=0')
xlabel('x'); ylabel('y'); zlabel('z');
axis equal
%3-6 ------------------------------------------------------
% 单位球 在z=0, y = -x方向变为原来的1.5倍
Mr135 = f(3*pi/4); % 顺时针135°
Mr_135 = f(-3*pi/4); % 逆时针135°
M2 = [1.5, 0, 0; 0, 1, 0; 0, 0, 1];
% 先顺时针135, 伸长为3/2, 逆时针135回去
Trans3 = Mr_135 * M2 * Mr135;
ans3 = Trans3 * XYZ;
tx = reshape(ans3(1,:),m,n);
ty = reshape(ans3(2,:),m,n);
tz = reshape(ans3(3,:),m,n);
figure;
mesh(tx,ty,tz); hold on;
plot3(tmpx,-tmpx,tmpz,'Color','r','LineWidth',2)
title('单位球在 y=-x, z=0 伸长1.5倍')
legend('结果','y=-x, z=0')
xlabel('x'); ylabel('y'); zlabel('z');
axis equal
%3-7 ------------------------------------------------------
% 单位球
% 1.z轴方向压缩1/2; 2.z=0,y=x方向压缩2/3; 3.z=0,y=-x方向伸长3/2
%同样注意左乘的顺序
Trans4 = Trans3 * Trans2 * Trans1;
ans4 = Trans4 * XYZ;
tx = reshape(ans4(1,:),m,n);
ty = reshape(ans4(2,:),m,n);
tz = reshape(ans4(3,:),m,n);
figure;
mesh(tx,ty,tz); hold on;
title('三次变换结果')
plot3(tmpx,tmpx,tmpz,'Color','g','LineWidth',2)
plot3(tmpx,-tmpx,tmpz,'Color','r','LineWidth',2)
legend('结果','y=x, z=0','y=-x, z=0')
xlabel('x'); ylabel('y'); zlabel('z');
axis equal
clear;
t = 0:0.01:2*pi;
x = 3*cos(t);
y = 4*sin(t);
plot(x,y); hold on;
axis equal
%椭圆周长 大致估计 2*pi*b + 4(a-b)
a = 4; b= 3;
len = 2*pi*b + 4*(a-b)
len = len/3
%画第一个点
x0 = 3*cos(t(1));
y0 = 4*sin(t(1));
xi=0;yi=0;
plot(x0,y0,'ro'); hold on;
%画第二、三个点
slen = 0;
i = 2;
for j = 1:2
while slen < len
xi = 3*cos(t(i));
yi = 4*sin(t(i));
slen = slen + sqrt((xi-x0).^2 + (yi-y0).^2);
x0 = xi; y0 = yi;
i = i + 1;
end
plot(xi,yi,'ro'); hold on;
slen = 0;
end
legend('椭圆','1','2','3')
x = linspace(-3,3,15);
z = [6.67E-05 0.000507999 0.002332306 0.005222464 -0.002933173 -0.05325323 -0.156268833 -0.244954041 -0.23464842 -0.143033067 -0.056117 -0.014082585 -0.002194105 -0.00019331 -5.86E-06
0.000262925 0.001986216 0.008702825 0.015437503 -0.040499172 -0.308012042 -0.826318904 -1.25706829 -1.188448954 -0.719239784 -0.280491875 -0.069771948 -0.010668413 -0.000889921 -1.73E-05
0.000647413 0.005243892 0.02435928 0.047307517 -0.103197968 -0.877543953 -2.432288837 -3.767491144 -3.590766881 -2.16613236 -0.827748387 -0.195233782 -0.025981816 -0.001211082 0.000166233
0.000541674 0.007119803 0.045616588 0.145439079 0.095663252 -0.912826861 -3.472249426 -6.041869836 -6.037528777 -3.625367444 -1.283013023 -0.234622932 -0.005095229 0.007174128 0.001646486
-0.002777653 -0.009474256 0.008699738 0.207991926 0.734050042 0.863954502 -0.93722495 -3.985423282 -4.866195677 -2.780586212 -0.560204908 0.194379722 0.1510287 0.04255871 0.006772569
-0.013025404 -0.072411141 -0.229023319 -0.259202021 0.652331714 2.587171194 3.0579872 0.661397823 -1.096766109 0.013672278 1.335883079 1.153804903 0.473873169 0.113050972 0.016804276
-0.027935662 -0.170940952 -0.653422577 -1.397069265 -1.09055258 1.421425395 3.423955144 1.941893458 0.403486529 1.784090416 2.986061669 2.120030227 0.821123475 0.191416765 0.028166964
-0.036506205 -0.230871544 -0.933553875 -2.254862661 -2.780189719 -0.699128363 1.692156351 0.981011843 0.198366231 2.198500335 3.592406283 2.514703848 0.968331367 0.225237535 0.03312495
-0.031409813 -0.201033299 -0.828613567 -2.076310763 -2.819502137 -1.398721548 0.610443711 0.408039884 0.121548952 1.882146307 3.004401595 2.084846731 0.800074 0.185851662 0.0273207
-0.018093429 -0.115195347 -0.468738395 -1.133340281 -1.342790588 0.011078228 1.921318528 2.255696198 1.824021729 2.11470183 2.141212784 1.311924629 0.481206977 0.109577578 0.015948072
-0.006544781 -0.038508884 -0.130105679 -0.151773609 0.560410656 2.76980684 5.590608398 6.719447098 5.583021325 3.645534364 1.972690378 0.832253861 0.250987014 0.051516548 0.007076012
-0.001076796 -0.002658171 0.024345501 0.273397797 1.296703612 3.628284821 6.515164186 7.831902222 6.516733048 3.874608626 1.689663142 0.546367941 0.129922971 0.022265156 0.00269252
1.73E-04 0.003624344 0.035838118 0.215208262 0.83747163 2.170924289 3.808970699 4.577764622 3.809568973 2.21842955 0.912914408 0.267547705 0.056070425 0.008399123 0.000895326
0.00013448 0.001694729 0.013908815 0.076363044 0.284053823 0.721286471 1.257080128 1.510657363 1.257176223 0.727605055 0.294069205 0.083307588 0.016592667 0.002328034 0.000230235
3.22E-05 0.00037217 0.002910301 0.015541855 0.056938004 0.143529878 0.249545315 0.299871028 0.249554793 0.144110902 0.057858224 0.016179777 0.003156814 0.000430336 4.10E-05];
%原图
mesh(x,x,z);figure;
xi = -3:0.1:3;
[xx,yy] = meshgrid(x,x);
[X,Y] = meshgrid(xi,xi);
Z = interp2(xx,yy,z,X,Y,'cubic');
mesh(X,Y,Z)
% 计算面积: 微积分近似:S = SUM (f(x,y)DS)
% 方块大小0.1x0.1, S= 41.0490
ssum = sum(sum(abs(Z)));
ssum = ssum * 0.1 * 0.1
% 方块大小0.01x0.01, S= 41.0094
xii = -3:0.01:3;
[Xi,Yi] = meshgrid(xii,xii);
Z = interp2(xx,yy,z,Xi,Yi,'cubic');
ssum2 = sum(sum(abs(Z)));
ssum2 = ssum2 * 0.01 * 0.01