matlab实验报告

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验报告

2. The Branching statements

一、实验目的:

1.To grasp the use of the branching statements;

2.To grasp the top-down program design technique.

二、实验内容及要求:

1.实验内容:

1).编写 MATLAB 语句计算 y(t)的值

(Write the MATLAB program required to calculate y(t) from the equation)

⎩⎨⎧<+≥+-=0

530 53)(22t t t t t y 已知 t 从-5到 5 每隔0.5取一次值。运用循环和选择语句进行计算。 (for values of t between -5 and 5 in steps of 0.5. Use loops and branches to perform this calculation.)

2).用向量算法解决练习 1, 比较这两个方案的耗时。

(tic ,toc 的命令可以帮助你完成的时间计算,请使用'help'函数)。 Rewrite the program 1 using vectorization and compare the consuming time of these two programs.

(tic, toc commands can help you to finish the time calculation, please use the …help ‟ function).

2.实验要求:

在报告中要体现top-down design technique, 对于 3 要写出完整的设计过程。

三、设计思路:

1.用循环和选择语句进行计算:

1).定义自变量t :t=-5:0.5:5;

2).用循环语句实现对自变量的遍历。

3).用选择语句实现对自变量的判断,选择。

4).将选择语句置入循环语句中,则实现在遍历中对数据的选择,从而实现程序的功能。

2. 用向量法实现:

1).定义自变量t :t=-5:0.5:5;

2).用 b=t>=0 语句,将t>=0得数据选择出,再通过向量运算y(b)=-3*t(b).^2 + 5; 得出结果。

3).用取反运算,选择出剩下的数据,在进行向量运算,得出结果。

四、实验程序和结果 1.实验程序

实验程序:创建m 文件:y_t.m

%Script file: y_t.m

%

% Purpose:

% calculate y(t) from the equation

% _

% | -3t^2 + 5 x >= 0

% y(t)= |

% | 3t^2 + 5 x < 0

% |_

%

%for values of t between -5 and 5 in steps of 0.5. Use loops and branches %to

%perform this calculation.

%

%

% Record of revisions:

% Date Programmer Description of change

% ====== =========== =====================

% 10/09/10 Ma Qichong Original code

%

clear

%clc

tic; %Start a stopwatch timer.

t=-5:0.5:5;

for ii=1:size(t,2) % SIZE(X,1) returns the number

% of rows; SIZE(X,2) returns the numberof columns.

if(t(ii)<0)

y(ii) = 3*t(ii)^2+5;

else

y(ii)= -3*t(ii)^2+5;

end

end

figure(1);

plot(t,y);

title('Plot of y(t) and its derivative----(1)');

xlabel('x');

ylabel('y');

grid on;

toc; %Read the stopwatch timer,

%prints the number of seconds required for the operation. clear

%clc

tic;

t=[-5:0.5:5];

b=t>=0;

y(b)=-3*t(b).^2 + 5;

%b=t<0;

y(~b)=3*t(~b).^2 + 5;

figure(2);

plot(t,y);

title('Plot of y(t) and its derivative----(2)'); xlabel('x');

ylabel('y');

grid on;

toc;

2.实验结果:

>> clear

>> y_t

Elapsed time is 0.998095 seconds.

Elapsed time is 0.338708 seconds.

>>

相关文档
最新文档