Page 310 - Numerical Analysis Using MATLAB and Excel
P. 310

Chapter 7  Finite Differences and Interpolation


                                                                    ⁄
                MATLAB interpolates the impedance at ω =     792 rad s  and displays the following message:

                Magnitude of Z at w=792 rad/s is 217.034 Ohms



                Two−dimensional plots were briefly discussed in Chapter 1. For convenience, we will review the
                following commands which can be used for two−dimensional interpolation.
                1. mesh(Z) − Plots the values in the matrix Z as height values above a rectangular grid, and con-
                   nects adjacent points to form a mesh surface.

                2. [X,Y]=meshgrid(x,y) − Generates interpolation arrays which contain all combinations of the
                   x and y points which we specify. X and Y comprise a pair of matrices representing a rectangular
                                                                                                   ,
                                                                                                 (=
                   grid of points in the x –  y  plane. Using these points, we can form a function z  f x y )   where
                   z  is a matrix.


                Example 7.13

                Generate the plot of the function
                                                            sin R
                                                       Z =  -----------                                (7.61)
                                                             R
                                                                                                        ⁄
                                    xy
                in three dimensions  , , and  . This function is the equivalent of the function y =  sin x x  in
                                              z
                two dimensions. Here,   is a matrix that contains the distances from the origin to each point in
                                      R
                the pair of  XY,[  ]   matrices that form a rectangular grid of points in the x –  y  plane.
                Solution:

                The matrix   that contains the distances from the origin to each point in the pair of  XY,[  ]
                            R
                matrices, is
                                                            2
                                                    R =   X +   Y  2                                   (7.62)
                We let the origin be at  x y,(  0  0 )  ( =  00 )  , and the plot in the intervals  2π–  ≤≤  2π  and
                                                      ,
                                                                                                  x
                – 2π ≤≤  2π . Then, we write and execute the following MATLAB script.
                      y

                % This is the script for Example_7_13
                x=−2*pi: pi/24: 2*pi;          % Define interval in increments of pi/24
                y=x;                           % y must have same number of points as x
                [X,Y]=meshgrid(x,y);           % Create X and Y matrices
                R=sqrt(X.^ 2 + Y.^ 2);         % Compute distances from origin (0,0) to x−y points
                Z=sin(R)./ (R+eps);            % eps prevents division by zero
                mesh(X,Y,Z);                   % Generate mesh plot for Z=sin(R)/R
                xlabel('x'); ylabel('y'); zlabel('z');


               7−32                             Numerical Analysis Using MATLAB® and Excel®, Third Edition
                                                                             Copyright © Orchard Publications
   305   306   307   308   309   310   311   312   313   314   315