4.1
for
t=-9:0.5:9
if(t>=0)
y=-(3*t^2)+5;
fprintf('y=%.2ft=%.2f\n',y,t);
else
y=(3*t^2)+5;
fprintf('y=%.2ft=%.2f\n',y,t);
end
end
编译结果:
y=248.00t=-9.00
y=221.75t=-8.50
y=197.00t=-8.00
y=173.75t=-7.50
y=152.00t=-7.00
y=131.75t=-6.50
y=113.00t=-6.00
y=95.75t=-5.50
y=80.00t=-5.00
y=65.75t=-4.50
y=53.00t=-4.00
y=41.75t=-3.50
y=32.00t=-3.00
y=23.75t=-2.50
y=17.00t=-2.00
y=11.75t=-1.50
y=8.00t=-1.00
y=5.75t=-0.50
y=5.00t=0.00
y=4.25t=0.50
y=2.00t=1.00
y=-1.75t=1.50
y=-7.00t=2.00
y=-13.75t=2.50
y=-22.00t=3.00
y=-31.75t=3.50
y=-43.00t=4.00
y=-55.75t=4.50
y=-70.00t=5.00
y=-85.75t=5.50
y=-103.00t=6.00
y=-121.75t=6.50
y=-142.00t=7.00
y=-163.75t=7.50
y=-187.00t=8.00
y=-211.75t=8.50
y=-238.00t=9.00
4.3
for
i=1:50
ii=i^2;
fprintf('i=%dii=%d\n',i,ii);
add=add+i+ii;
end
fprintf('和为%d',add);
编译结果:
i=2ii=4
i=3ii=9
i=4ii=16
i=5ii=25
i=6ii=36
i=7ii=49
i=8ii=64
i=9ii=81
i=10ii=100
i=11ii=121
i=12ii=144
i=13ii=169
i=14ii=196
i=15ii=225
i=16ii=256
i=17ii=289
i=18ii=324
i=19ii=361
i=20ii=400
i=21ii=441
i=22ii=484
i=23ii=529
i=24ii=576
i=25ii=625
i=26ii=676
i=27ii=729
i=28ii=784
i=29ii=841
i=30ii=900
i=31ii=961
i=32ii=1024
i=33ii=1089
i=34ii=1156
i=35ii=1225
i=36ii=1296
i=37ii=1369
i=38ii=1444
i=39ii=1521
i=40ii=1600
i=41ii=1681
i=42ii=1764
i=43ii=1849
i=44ii=1936
i=45ii=2025
i=46ii=2116
i=47ii=2209
i=48ii=2304
i=49ii=2401
i=50ii=2500
和为46750>>
4.5
n=input('请输入N的值');
if(n>0)
x=n*(n-1);
fprintf('N!=%d',x);
elseif(n==0);
disp('N!=1');
else
disp('error');
end
编译结果:
请输入N的值0
N!=1
请输入N的值-8
error
请输入N的值3
N!=6
4.7
a.ires=21循环21次;
b.ires=28循环4次;
c.ires=28循环4次;
d.ires=24循环11次;
4.9
a.
arr1
=
1-23-4
5-67-8
9-1011-12
b.
arr1
=
0000
0364964
81100121144
4.11
假设一个小球的初始位置为(x0,y0)为(0,0),初速度为20m/s,水平速度和合速度之间
的夹角为θ
度,编写一个程序,画出这个小球的轨迹,并计算小球再次落地与初始位置之间的距离。这程序应当能画出多个抛物线,θ 的取值从5 到85
度,每隔10 度取一次,计算水平位移,θ 的取值从0 到90 度,每隔1 度取一次。最终应当确定那一个θ
值使得水平位移最大。还有打印不同的抛物线时要用不同的颜色。
conv =
pi / 180;
g =
-9.81;
vo =
20;
range
= zeros(1,91);
for ii
= 1:91
theta
= ii - 1;
vxo =
vo*
cos(theta*conv);
vyo =
vo * sin(theta*conv);
traj_time
= -2 * vyo / g;
range(ii)
= vxo * traj_time;
end
fprintf
('Range versus angle theta:\n');
for ii
= 1:91
theta
= ii - 1;
fprintf('
- %8.4f\n',theta, range(ii));
end
[maxrange
index] = max(range);
maxangle
= index - 1;
fprintf
('\nMax range is %8.4f at - degrees.\n',maxrange,
maxangle);
for ii
=[5 15 25 35 45 55 65 75 85];
theta
= ii;
vxo =
vo * cos(theta*conv);
vyo =
vo * sin(theta*conv);
traj_time
= -2 * vyo / g;
x =
zeros(1,21);
y =
zeros(1,21);
for jj
= 1:21
time =
(jj - 1) * traj_time/20;
x(jj)
= vxo * time;
y(jj)
= vyo * time + 0.5 * g * time^2;
end
plot(x,y,'b');
if ii
== 5
hold
on;
end
end
title
('\bfTrajectory of Ball vs Initial Angle \theta');
xlabel
('\bf\itx \rm\bf(meters)');
ylabel
('\bf\ity \rm\bf(meters)');
axis
([0 45 0 25]);
grid
on;
vxo =
vo * cos(maxangle*conv);
vyo =
vo * sin(maxangle*conv);
traj_time
= -2 * vyo / g;
x =
zeros(1,21);
y =
zeros(1,21);
for jj
= 1:21
time =
(jj - 1) * traj_time/20;
x(jj)
= vxo * time;
y(jj)
= vyo * time + 0.5 * g * time^2;
end
plot(x,y,'r','LineWidth',3.0);
hold
off
4.13
conv = pi / 180;
g = -9.81;
vo = input(':');
range = zeros(1,91);
forii
= 1:91
theta = ii - 1;
vxo = vo * cos(theta*conv);
vyo = vo * sin(theta*conv);
traj_time = -2 * vyo / g;
range(ii) = vxo * traj_time;
end
fprintf ('Range
versus angle theta:\n');
forii
= 1:91
theta = ii - 1;
fprintf('
- %8.4f\n',theta,
range(ii));
end;
[maxrange index] = max(range);
maxangle = index - 1;
fprintf ('\nMax
range is %8.4f at - degrees.\n',maxrange,
maxangle);
forii
= 5:10:85
theta = ii;
vxo = vo * cos(theta*conv);
vyo = vo * sin(theta*conv);
traj_time = -2 * vyo / g;
x = zeros(1,21);
y = zeros(1,21);
forjj
= 1:21
time =(jj - 1) * traj_time/20;
x(jj) = vxo * time;y(jj) = vyo * time + 0.5 * g *
time^2;
end
plot(x,y,'b');
ifii
== 5
holdon;
end
end
title ('\bfTrajectory
of Ball vs Initial Angle \theta');
xlabel ('\bf\itx
\rm\bf(meters)');
ylabel ('\bf\ity
\rm\bf(meters)');
axis ([0 11 0 6]);
gridon;
vxo = vo * cos(maxangle*conv);
vyo = vo * sin(maxangle*conv);
traj_time = -2 * vyo / g;
x = zeros(1,21);
y = zeros(1,21);
forjj
= 1:21
time = (jj - 1) * traj_time/20;
x(jj) = vxo * time;
y(jj) = vyo * time + 0.5 * g * time^2;
end
plot(x,y,'r','LineWidth',3.0);
holdoff
输入为10m/s时:
输入为20m/s时:
输入为30m/s时:
4.19
斐波那契数列。含有n个数的斐波那契数列的定义如下:
f(1)
=1
f(2)
=2
f(n) = f(n-1)
+ f(n-2)
所以f(3)=f(2)+f(1)=2+1=3,还有更多的数。在M文件中编写一程序,计算并写斐波那
契数列中第n(n>2)个数的值,n由用户输入。用for循环进行计算。
4.21
4.23
工程师们经常用分贝或dB来描述两功率之比。1dB的定义如下,
P2是已测量的功率,P1代表参考功率。假设参考功率P1是1瓦。P2从1到20瓦每隔
0.5瓦取一次值,编写程序,计算相应的dB值,并画出dB-P2
4.25
4.27