Applied Numerical Methods Using MATLAB. Won Y. Yang

Applied Numerical Methods Using MATLAB - Won Y. Yang


Скачать книгу
nargin # of input arguments nargout # of output arguments varargin Variable input argument list varargout Variable output argument list

      MATLAB also allows us to define our own function and store it in a file named after the function name so that it can be used as if it were a built‐in function. For instance, we can define a scalar‐valued function:

equation

      and a vector‐valued function

equation

      as follows:

function y=f1(x) y=1./(1+8*x.̂2); function y=f49(x) y(1)= x(1)̂2+4*x(2)̂2 -5; y(2)=2*x(1)̂2-2*x(1)-3*x(2) -2.5;

       >f1([0 1]) % several values of a scalar function of a scalar variable ans= 1.0000 0.1111 >f49([0 1]) % a value of a 2D vector function of a vector variable ans= -1.0000 -5.5000 >feval('f1',[0 1]), feval('f49',[0 1]) % equivalently, yields the same ans= 1.0000 0.1111 ans= -1.0000 -5.5000

      1 (Q7) With the function f1(x) defined as a scalar function of a scalar variable, we enter a vector as its input argument to obtain a seemingly vector‐valued output. What's going on?

      2 (A7) It is just a set of function values [f1(x1) f1(x2) …] obtained at a time for several values [x1 x2 …] of x. In expectation of one‐shot multioperation, it is a good practice to put a dot ( .) just before the arithmetic operators *(multiplication), /(division), and ̂(power) in the function definition so that the element‐by‐element (element‐wise or term‐wise) operation can be done any time.

       >f1h=@(x)1./(1+8*x.̂2); % Using function handle >f1i=inline('1./(1+8*x.̂2)','x'); % Usng inline() >f1h([0 1]), feval(f1h,[0 1]), f1i([0 1]), feval(f1i,[0 1]) ans= 1.0000 0.1111 ans= 1.0000 0.1111 >f1='1./(1+8*x.̂2)'; x=[0 1]; eval(f1) ans= 1.0000 0.1111

      As far as a polynomial function is concerned, it can simply be defined as its coefficient vector arranged in descending order. It may be called to yield its value for certain value(s) of its independent variable by using the command ‘ polyval()’.

       >p=[1 0 –3 2]; % Polynomial function p(x) = x3-3x + 2 >polyval(p,[0 1]) % Polynomial function values at x=0 and 1 ans= 2.0000 0.0000

      The multiplication of two polynomials can be performed by taking the convolution of their coefficient vectors representing the polynomials in MATLAB, since

equation

      where

equation

      This operation can be done by using the MATLAB built‐in command ‘ conv()’ as illustrated beneath:

       >a=[1 –1]; b=[1 1 1]; c=conv(a,b) c= 1 0 0 -1 % meaning that (x-1)(x2 + x + 1) = x3 + 0 ⋅ x2 + 0 ⋅ x-1

      We can define a new scalar/vector/matrix or redefine any existing ones in terms of the existent ones or irrespective of them. In the MATLAB Command window, let us define A and B as

equation

      by running

       >A=[1 2 3;4 5 6], B=[3;-2;1]

      We can modify them or take a portion of them. For example:

       >A=[A;7 8 9] A= 1 2 3 4 5 6 7 8 9 >B=[B [1 0 -1]'] B= 3 1 -2 0 1 -1

      Here, the apostrophe (prime) operator ( ') takes the complex conjugate transpose and functions virtually as a transpose operator for real‐valued matrices. If you want to take just the transpose of a complex‐valued matrix, you should put a dot ( .) before ', i.e. ‘ .'’.

      When extending an existing matrix or defining another one based on it, the compatibility of dimensions should be observed. For instance, if you try to annex a 4 × 1 matrix into the 3 × 1 matrix B, MATLAB will reject it squarely, giving you an error message.

       >B=[B ones(4,1)] Error using horzcat Dimensions of matrices being concatenated are not consistent.

      We can modify or refer to a portion of a given matrix.

       >A(3,3)=0 A= 1 2 3 4 5 6 7 8 0 >A(2:3,1:2) % from 2nd row to 3rd row, from 1st column to 2nd column ans= 4 5 7 8 >A(2,:) % 2nd row, all columns ans= 4 5 6

       >t=0:0.1:2

      which makes

       t=[0.0 0.1 0.2 … 1.9 2.0]

      1 (Q8) What if we omit the increment between the left/right boundary numbers?

      2 (A8) By default, the increment is 1.>t=0:2 t= 0 1 2

      3 (Q9) What if the right boundary number is smaller/greater than the left boundary number with a positive/negative increment?

      4 (A9) It yields an empty matrix, which is useless.>t=0:-2 t= Empty matrix: 1-by-0

      5 (Q10) If we define just some elements of a vector not fully, but sporadically, will we have a row vector or a column vector and how will it be filled in between?

      6 (A10) We will have a row vector filled with zeros between the defined elements.>D(2)=2; D(4)=3 D= 0 2 0 3

      7 (Q11) How do we make a column vector in the same style?

      8 (A11) We must initialize it as a (zero‐filled) row vector, prior to giving it a value.>D=zeros(4,1); D(2)=2; D(4)=3 D= 0 2 0 3

      9 (Q12) What happens if the specified element index of an array exceeds the defined range?

      10 (A12) It is rejected. MATLAB does not accept nonpositive or noninteger indices.>D(5) Index exceeds matrix dimensions. >D(0)=1; Subscript indices must either be real positive integers or logicals. >D(1.2) Subscript indices must either be real positive integers or logicals.

      11 (Q13) How do we know the size (the numbers of rows/columns) of an already‐defined array?

      12 (A13)


Скачать книгу