文档库

最新最全的文档下载
当前位置:文档库 > 测控软件技术基础周鑫

测控软件技术基础周鑫

测控软件技术基础

测控软件技术基础周鑫

姓名:周鑫

班级:232131

学号:20131000982

指导老师:黄玉金

Basic Syntax and Command-Line Exercises

1. Create a vector of the even whole numbers between 31 and 75.

代码:x=32:2:75

2. Let x = [2 5 1 6].

a. Add 16 to each element

b. Add 3 to just the odd-index elements

c. Compute the square root of each element

d. Compute the square of each element

代码:a=x+16;

b = x(1:2:end) + 3

c = sqrt(x)

d = x.^2

5. Create a vector x with the elements ...

代码:a = 2:2:20

b = 10:-2:-4

c1 = 1:5, c2 = 1./c1 , rats(c2)

d1 = 0:4, d2 = 1:5, d3 = d1./d2 , rats(d3)

6. Create a vector x with the elements,

x n = (-1)n+1/(2n-1)

Add up the elements of the version of this vector that has 100 elements. 代码:n = 1:100;

x = ( (-1).^(n+1) ) ./ (2*n - 1);

y = sum(x)

10. Make a good plot (i.e., a non-choppy plot) of the function

f(x) = sin(1/x)

for 0.01 < x < 0.1. How did you create x so that the plot looked good?

代码:x=0.01:0.0001:0.1;y=sin(1./x);plot(x,y)

Basic Array Syntax and Manipulations

1. Given x = [3 1 5 7 9 2 6], explain what the following commands "mean" by

by summarizing the net result of the command.

a. x(3)

b. x(1:7)

c. x(1:end)

d. x(1:end-1)

e. x(6:-2:1)

f. x([1 6 2 1 1])

g. sum(x)

答:x(3)表示x行向量中的第三个元素。x(1:7)表示x行向量中第1到7个元素。x(1:end)表示x行向量中第1到最后一个元素。x(1:end-1)表示x行向量中第1到倒数第二个元素。x(6:-2:1)表示x行向量中第6个元素到第1个元素中的偶数项元素。x([1 6 2 1 1]):方括号中表示x行向量中元素的序号,整个表示由这些元素组成的新行向量。sum(x):表示x中所有元素求和。

2. Given the array A = [ 2 4 1 ; 6 7 2 ; 3 5 9], provide the commands needed to

a. assign the first row of A to a vector called x1

b. assign the last 2 rows of A to an array called y

c. compute the sum over the columns of A

d. compute the sum over the rows of A

e. compute the standard error of the mean of each column of A (NB. the standard

error of the mean is defined as the standard deviation divided by the

square root of the number of elements used to compute the mean.) 代码: A = [ 2 4 1 ; 6 7 2 ; 3 5 9]

x1 = A(1,:)

y = A(end-1:end,:)

c = sum(A)

d = sum(A,2)

N = size(A,1), e = std(A)/sqrt(N)

6. Give the following commands to create an array called F:

>> randn('seed',123456789)

>> F = randn(5,10);

a. Compute the mean of each column and assign the results to the elements of a

vector called avg.

b. Compute the standard deviation of each column and assign the results to the

elements of a vector called s.

c. Compute the vector of t-scores that test the hypothesis that the mean of each

column is no different from zero.

d. If Pr(|t| > 2.132 ) = 0.1 with 4 degrees of freedom, are any of the mean values

in the vector avg statistically different from 0?

代码:randn('seed',123456789)

F = randn(5,10);

N = size(F,1)

avg = mean(F)

s = std(F)

tscore = (avg - 0)./(s/sqrt(N))

Exercises on Relational and Logical Operations

2. The exercises here show the techniques of logical-indexing (indexing with

0-1 vectors). Given x = 1:10 and y = [3 1 5 6 8 2 9 4 7 0], execute and

interpret the results of the following commands:

a. (x > 3) & (x < 8)

b. x(x > 5)

c. y(x <= 4)

d. x( (x < 2) | (x >= 8) )

e. y( (x < 2) | (x >= 8) )

f. x(y < 0)

输入代码: x = 1:10;y=[3 1 5 6 8 2 9 4 7 0];

运行结果:a: ans =

0 0 0 1 1 1 1 0 0 0

b: ans =

6 7 8 9 10

c: ans =

3 1 5 6

d: ans =

1 8 9 10

e: ans =

3 4 7 0

f: ans =

Empty matrix: 1-by-0

4. Given x = [3 15 9 12 -1 0 -12 9 6 1], provide the command(s) that will

a. ... set the values of x that are positive to zero

b. ... set values that are multiples of 3 to 3 (rem will help here)

c. ... multiply the values of x that are even by 5

d. ... extract the values of x that are greater than 10 into a vector called y

e. ... set the values in x that are less than the mean to zero

f. ... set the values in x that are above the mean to their difference from the mean

代码:x = [3 15 9 12 -1 0 -12 9 6 1]

a = x, idxa = x > 0, a(idxa) = 0

b = x, idxb = ~rem(x,3), b(idxb) = 3

c = x, idxc = ~rem(x,2), c(idxc) = 5*c(idxc)

5. Create the vector x = randperm(35) and then evaluate the following function using

only logical indexing:

y(x) = 2 if x < 6

= x - 4 if 6 <= x < 20

= 36 - x if 20 <= x <= 35

You can check your answer by plotting y vs. x with symbols. The curve should be

a triangular shape, always above zero and with a maximum of 16. It might also be

useful to try setting x to 1:35. Using multiple steps (or a simple Mfile) is

recommended for this problem.

代码:x = 1:35;

y = zeros(size(x));

idx1 = x < 6;

idx2 = (x >= 6) & (x < 20);

idx3 = (x >= 20) & (x <= 35);

y(idx1) = 2;

y(idx2) = x(idx2) - 4;

y(idx3) = 36 - x(idx3);

disp([x(:) idx1(:) idx2(:) idx3(:) y(:)]) plot(x,y,'o')

Control of Flow: if-blocks

1. if n > 1 a. n = 7 m = ?

m = n+1 b. n = 0 m = ?

else c. n = -10 m = ?

m = n - 1

end

结果:

a:m=8;

b:m=-1;

c:m=-11

2. if z < 5 a. z = 1 w = ?

w = 2*z b. z = 9 w = ?

elseif z < 10 c. z = 60 w = ?

w = 9 - z d. z = 200 w = ?

elseif z < 100

w = sqrt(z)

else

w = z

end

结果:a:w=2;

b:w=0;

c:w= 7.7460;

d:w=200

3. if T < 30 a. T = 50 h = ?

h = 2*T + 1 b. T = 15 h = ?

elseif T < 10 c. T = 0 h = ?

h = T - 2

else

h = 0

end

结果:a:h=0;

b:h=31;

c:h=1;

5. h(T) = T - 10 when 0 < T < 100

= 0.45 T + 900 when T > 100

Test cases: a. T = 5, h = -5

b. T = 110, h = 949.5

结果:代码:if(T>0&&T<100)

h(T) = T - 10

elseif(T>100)

h(T)= 0.45*T + 900

end

a:ans =

-5

b:ans =

949.5000

6. f(x) = -1 if x < 0

= 0 if x = 0

= 1 if x > 0

Compare your results to the MATLAB function sign.

代码:定义函数:

function y=ckyjjs(x)

[m,n]=size(x);

y=zeros(size(x));

for a=1:m

for b=1:n

if(x(a,b)>0)

y(a,b)=1;

elseif(x(a,b)==0)

y(a,b)=0;

elseif(x(a,b)<0)

y(a,b)=-1;

end

end

end

end

Loop Constructs

1. Given the vector x = [1 8 3 9 0 1], create a short set of commands that will

a. Add up the values of the elements (Check with sum.)

b. Computes the running sum (for element j, the running sum is the sum of the

elements from 1 to j, inclusive. Check with cumsum.)

c. computes the sine of the given x-values (should be a vector)

代码:

1. x = [1 8 3 9 0 1]

a: total = 0;

for j = 1:length(x)

total = total + x(j);

end

b: runningTotal = zeros(size(x));

runningTotal(1) = x(1);

for j = 2:length(x)

runningTotal(j) = runningTotal(j-1) + x(j);

end

c: s = zeros(size(x));

for j = 1:length(x)

s(j) = sin(x(j));

end

2. Create an M-by-N array of random numbers (use rand). Move through the array, element by element, and set any value that is less than 0.2 to 0 and any

value that is greater than (or equal to) 0.2 to 1.

代码:A = rand(4,7);

[M,N] = size(A);

for j = 1:M

for k = 1:N

if A(j,k) < 0.2

A(j,k) = 0;

else

A(j,k) = 1;

end

end

end

3. Given x = [4 1 6] and y = [6 2 7], compute the following arrays

a. a

ij = x

i

y

j

b. b

ij = x

i

/y

j

c. c

i = x

i

y

i

, then add up the elements of c.

d. d

ij = x

i

/(2 + x

i

+ y

j

)

e. e

ij = reciprocal of the lesser of x

i

and y

j

代码:x = [4 1 6], y = [6 2 7] N = length(x);

for j = 1:N

c(j) = x(j)*y(j);

for k = 1:N

a(j,k) = x(j)*y(k);

b(j,k) = x(j)/y(k);

d(j,k) = x(j)/(2 + x(j) + y(k));

e(j,k) = 1/min(x(j),y(k));

end

end

c = sum(c)

Programming Exercises

2. The Fibonacci numbers are comuted according to the following relation:

F n = F n-1 + F n-2

with F0 = F1 = 1.

a. Compute the first 10 Fibonacci numbers.

b. For the first 50 Fibonacci numbers, compute the ratio

F n / F n-1

It is claimed that this ratio approaches the value of the golden mean

( (1 + sqrt(5))/2 ). What do your results show?

代码:

a:F(1)=1;

F(2)=1;

for a=3:10

F(a)=F(a-1)+F(a-2);

end

F

b:F(1)=1;

F(2)=1;

for a=3:50

F(a)=F(a-1)+F(a-2);

end

F(50)/F(49)

结果:ans =

1.6180

3. The Legendre polynomials (P n(x)) are defined by the following recurrance relation

(n+1) P n+1(x) - (2n+1)x P n(x) + n P n-1(x) = 0

with P0(x) = 1, P1(x) = x and P2(x) = (3x2- 1)/2. Compute the next three Legendre polynomials and plot all 6 over the interval [-1,1].

For more information on Legendre polynomials, see the Mathworld site. 代码:

x=-1:0.1:1;

[a,b]=size(x)

for i=1:b

P0(i)=1;P1(i)=x(i);P2(i)=(3*x(i).^2-1)/2;P3(i)=(5*x(i).*P2(i)-2.*P1(i ))/3;P4(i)=(7*x(i).*P3(i)-3.*P2(i))/4;P5(i)=(9*x(i).*P4(i)-4.*P3(i))/ 5;

end

plot(x,P0,x,P1,x,P2,x,P3,x,P4,x,P5)

测控软件技术基础周鑫

结果:

12. Follow the directions for Exercise 11 but create a function that computes the

cumulative sum of the elements of a vector. The elements of the cumulative sum

vector are defined by

s j = x1 + x2 + ... + x j

for j = 1: length of the vector x.

The built-in functions sum and cumsum should be used instead of prod and cumprod,respectively.

代码:

function y=cumulative_sum(x)

y=0;

a=length(x);

s(1)=x(1);

for i=2:a

s(i)=s(i-1)+x(i);

end

for j=1:a

y=y+s(j);

end

举例:x=[1 1 1 1 1 1];cumulative_sum(x)

ans =

21