Applied Numerical Methods Using MATLAB. Won Y. Yang. Читать онлайн. Newlib. NEWLIB.NET

Автор: Won Y. Yang
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Математика
Год издания: 0
isbn: 9781119626824
Скачать книгу
alt="equation"/>

Image described by caption and surrounding text.

      %nm01f05a.m N=1e4; x=rand(N,1); % An Nx1 noise vector with U(0,1) mx=mean(x); sgm2=sum((x-mx).̂2)/(N-1), var(x) subplot(221), M=20; hist(x,M) % Histogram having M=20 bins a=-1; b=1; y=(b-a)*x+a; %Eq.(1.1.4): 1000x1 noise vector with U(-1,1) subplot(222), hist(y,M) % Histogram

       1.1.8.2 Random Number with Normal (Gaussian) Distribution

      The numbers in a matrix generated by the MATLAB function ‘ randn(M,N)’ have normal (Gaussian) distribution with average m = 0 and variance σ2 = 1, as described by N(0,1). The random number x generated by ‘ rand()’ has the probability density function

      (1.1.6)equation

      The probability density function of the new Gaussian number generated by this transformation is obtained by substituting x = (ym)/σ into Eq. (1.1.5) and dividing the result by the scale factor σ (which can be seen in dx = dy/σ) so that the integral of the density function over the whole interval ( ∞,+∞) amounts to 1.

      (1.1.7)equation

      %nm01f05b.m N=1e4; x=randn(N,1); % An Nx1 noise vector with N(0,1) subplot(223), M=20; hist(x,M) % Histogram having M=20 bins f=@(x,m,sgm)exp(-(x-m).̂2/2/sgm̂2)/sqrt(2*pi)/sgm; %Gaussian density ftn [Ns,Cs]=hist(x,20); dx=Cs(2)-Cs(1); % Bin width x_=[-5:0.01:5]; fx=f(x_,0,1); hold on, plot(x_,fx*dx*N,'r:') sgm=1/2; m=1; y=sgm*x+m; % An Nx1 noise vector with N(m,sgm̂2) subplot(224), hist(y,M) % Histogram [Ns,Cs]=hist(y,M); dy=Cs(2)-Cs(1); % Bin width y_=[-5:0.01:5]; fy=f(y_,m,sgm); hold on, plot(y_,fy*dy*N,'r:')

      For practice, we make a vector consisting of 1000 standard Gaussian numbers, transform it to make a vector of numbers having normal distribution N(1,1/4), with mean m = 1 and variance σ2 = 1/4, and then draw the histograms for the two Gaussian number vectors (Figure 1.5b1,b2) by running the above block of MATLAB statements.

      1.1.9.1 if‐end and switch‐case‐end Statements

Relational operator Remark
< Less than
<= Less than or equal to
== Equal
> Greater than
>= Greater than or equal to
∼= Not equal (≠)
& and
| or
not

      1 (Ex. 1) A Simple if‐else‐end Block%nm119_1.m: example of if-end block t=0; if t>0 sgnt= 1; else sgnt= -1; end

      2 (Ex. 2) A Simple if‐elseif‐end Block%nm119_2.m: example of if-elseif-end block if t>0 sgnt= 1 elseif t<0 sgnt= -1 end

      3 (Ex. 3) An if‐elseif‐else‐end Block%nm119_3.m: example of if-elseif-else-end block if t>0, sgnt= 1 elseif t<0, sgnt= -1 else sgnt= 0 end

      4 (Ex. 4) An if‐elseif‐elseif‐..‐else‐end Block%nm119_4.m: example of if-elseif-elseif-else-end block point= 85; if point>=90, grade= 'A' elseif point>=80, grade= 'B' elseif point>=70, grade= 'C' elseif point>=60, grade= 'D' else grade= 'F' end

      5 (Ex. 5) A switch‐case‐end Block%nm119_5.m: example of switch-case-end block point= 85; switch floor(point/10) %floor(x): integer less than or equal to x case 9, grade= 'A' case 8, grade= 'B' case 7, grade= 'C' case 6, grade= 'D' otherwise grade= 'F' end

      1.1.9.2 for index=i_0:increment:i_last‐end Loop

      A for loop makes a block of statements executed repeatedly for a specified number of times, with its loop index increasing from i_ 0 to a number not greater than i_ last by a specified step ( increment) or by 1 if not specified. The loop iteration normally ends when the loop index reaches i_ last, but it can be stopped by a break statement inside the for loop. The for loop with a positive/negative increment will never be iterated if the last value ( i_ last) of the index is smaller/greater than the starting value ( i_0).

      1 (Ex. 6) A for Loop%nm119_6.m: example of for loop point= [76 85 91 65 87]; for n=1:length(point) if point(n)>=80, pf(n,:)= 'pass'; elseif point(n)>=0, pf(n,:)= 'fail'; else %if point(n)<0 pf(n,:)= '????'; fprintf('\n\a Something