matlab编程入门学习(3)

时间:2021-03-03 06:46:57

之前学过了matlab的选择语句,这节讲述下3大语句中的第3大语句,循环语句

第四章、循环结构

4.1、基本結構

在matlab中循环结构基本上有两种,while循环和for循环,和C语言套路一样,我就不在赘述他们之间的区别了!

简单的阶乘例子

sum=1;
for i=1:5
sum=sum*i;
end
fprintf('5!=%d\n',sum);

我们在看一下稍微综合一点的例子


%this is a program how to calculate the day of year
disp('please input the date of the day u want to calc!!');
while 1
day_of_year = input('Please input the day:');
if day_of_year>31||day_of_year<0
disp('out of range!!');
else
break;
end
end
while 1
month = input('Please input the month:');
if month>12||month<0
disp('out of range!!');
else
break;
end
end
while 1
year = input('Please input the year:');
if year<0
disp('out of range!!');
else
break;
end
end

for ii=1:month-1
switch (ii)
case {1,3,5,7,8,10,12};
day_of_year = day_of_year + 31;
case {4,6,9,11};
day_of_year = day_of_year + 30;
case 2,
if mod(year,400)==0||(mod(year,100)==0&&mod(year,4)~=0)
day_of_year = day_of_year + 29;
else
day_of_year = day_of_year + 28;
end

end
end
fprintf('the day of year is %d\n',day_of_year);



很容易就求出了一天在一年中是第几天,主要的知识点就是闰年的判断,以及错误的简单处理。

这个程序有很多的错误,譬如说2月份只有28天或者29天但是我输入的是30天这种简单的错误其实判读不出来,还有一个月的最大天数也是个问题,所以改进如下:

year = input('please input a year:');
while 1
if length(int2str(year))<4 || length(int2str(year))>4
disp('wrong year!please input again!');
year = input('please input a year:');
else
break;
end
end
if rem(year,400)==0 || (rem(year,4)==0 && rem(year,100)~=0)
yeap = 1;
else
yeap = 0;
end
month = input('please input a month:');
while 1
if month>12 || month <1
disp('wrong month!please input again!');
month = input('please input a month:');
else
break;
end
end
day = input('please input a day:');
while 1
switch month
case {1,3,5,7,8,10,12},
if day>31 || day<1
disp('wrong day!please input again!');
day = input('please input a day:');
else
break;
end
case {4,6,9,11},
if day>30 || day <1
disp('wrong day!please input again!');
day = input('please input a day:');
else
break;
end
case 2
if yeap == 1 && day<1 || day>29
disp('wrong day!please input again!');
day = input('please input a day:');
elseif yeap == 0 && day<1 || day>28
disp('wrong day!please input again!');
day = input('please input a day:');
else
break;
end
end
end

dl = day;
for i = 1:month-1
switch (i)
case {1,3,5,7,8,10,12},
dl = dl + 31;
case {4,6,9,11},
dl = dl +30;
case 2
dl = dl + 28 + yeap;
end
end
fprintf('the day of this year is %d\n',dl);


4.2、逻辑数组

逻辑数组有一个重要的属性——它在算术运算中能提供一个屏蔽(mask)。屏蔽(mask)是指一个数组,它从另一个数组选择所需的元素参与运算。指定的运算只在选择的元素上执行,而不执行原有的元素。
a = [1,2,3;4,5,6;7,8,9];
b = a>5

b =

0 0 0
0 0 1
1 1 1

>> a

a =

1 2 3
4 5 6
7 8 9
我们可以看到所谓的逻辑数组我是这么理解的,相当于一个threshold作用,选取我们想要的,淘汰不需要的。
下面给一个稍微综合一点的例子,看看选择循环以及逻辑运算
利用最小二乘法拟合出一系列点的直线
X = [1.1 2.1 3.1 4.1 5.3 6.5 7.1 8.1 9.1 10.5];
Y = 1:10;
sum_x = sum(X);
sum_y = sum(Y);
ave_x = sum_x./10;
ave_y = sum_y./10;
sum_x_ave_y = sum_x * ave_y;
sum_x_ave_x = sum_x * ave_x;
sum_x_x = sum(X.^2);
for i=1:10
fprintf('%6.1f\t',X(i));
end
ave_x = sum_x/10;
fprintf('\n');
for i=1:10
fprintf('%d\t',Y(i));
end
ave_y = sum_y/10;
fprintf('\n');
for i=1:10
sum_XY = X.*Y;
fprintf('%6.1f\t',A(i));
end
fprintf('\n');
sum_xy = sum(sum_XY);
m = (sum_xy-sum_x_ave_y)/(sum_x_x-sum_x_ave_x);
b = ave_y - m*ave_x;
plot(X,Y,'bo');
hold on;
xmin = min(X);
xmax = max(X);
ymin = m*xmin + b;
ymax = m*xmax + b;
plot([xmin,xmax],[ymin,ymax],'r','LineWidth',2);
hold off;
title ('\bfLeastSquaresFit');
xlabel('\bf\itx');
ylabel('\bf\ity');
legend('Input data','Fitted line');
grid on

我在此就直接给的点,如果想换成其他的点,直接把赋值语句改成input就ok了

据说这个程序很多考试都会考这个程序

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
% Write out table of ranges
fprintf ('Range versus angle theta:\n');
for ii = 1:91
theta = ii - 1;
fprintf(' %2d %8.4f\n',theta, range(ii));
end
% Calculate the maximum range and angle
[maxrange index] = max(range);
maxangle = index - 1;
fprintf ('\nMax range is %8.4f at %2d degrees.\n',maxrange, maxangle);
% Now plot the trajectories
for ii = 5:10:85
% Get velocities and max time for this angle
theta = ii;
vxo = vo * cos(theta*conv);
vyo = vo * sin(theta*conv);
traj_time = -2 * vyo / g;
% Calculate the (x,y) positions
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
% Add titles and axis lables
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;
% Now plot the max range trajectory
vxo = vo * cos(maxangle*conv);
vyo = vo * sin(maxangle*conv);
traj_time = -2 * vyo / g;
% Calculate the (x,y) positions
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
matlab编程入门学习(3)