Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.

Similar presentations


Presentation on theme: "Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem."— Presentation transcript:

1 Numerical Analysis 3D Plots

2 A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.

3 SOLVING AN EQUATION WITH ONE VARIABLE Can write an equation with one (independent) variable in form f(x) = 0 A solution or root of the equation is a value of the variable that makes the equation be true, i.e., that makes f(x) have the value zero Making the equation be true is called satisfying the equation

4 Graphically, a solution is a point where the function f(x) touches or crosses the x-axis. Although mathematically if a value of x makes the function touch the x- axis but not cross it that value is a root, in MATLAB the value must make the function cross the x-axis in order to be considered a root SOLVING AN EQUATION WITH ONE VARIABLE

5 The equation f(x) = 0 can have No solutions Example: f(x) = 0 has no solutions if f(x) = 5 A finite number of solutions Example (one solution): f(x) = 0 has exactly one solution if f(x) = x Example (two solutions): f(x) = 0 has exactly two solutions if f(x) = 1 – x2 An infinite number of solutions Example: f(x) = 0 has an infinite number of solutions if f(x) = sin( x ) SOLVING AN EQUATION WITH ONE VARIABLE

6 A numerical solution or root is a value of x that makes the function be zero or almost zero A general procedure for finding a numerical solution is: SOLVING AN EQUATION WITH ONE VARIABLE

7 1. The user provides an initial guess of the numerical root, which the procedure uses as its current estimate of the numerical root 2. If the current estimate makes the function be almost zero, use that estimate as the numerical root and stop computing 3. If the current estimate does not make the function be almost zero, find a new estimate that makes the function be closer to zero and go to Step 2

8 The procedure lets the user define what "almost zero" means, e.g., the absolute value of the estimate must be < 10 -6 Another way for the procedure to stop is when the current and previous estimates are almost the same The procedure lets the user define "almost the same", e.g., absolute value of the difference is less than 10 -4

9 SOLVING AN EQUATION WITH ONE VARIABLE Use the MATLAB function fzero() to find a root of a function fzero is a function that accepts another function (the function to be solved) as an input argument (like fplot that we saw before). You can also use an inline (anonymous) function.

10 x0 can be a scalar or a two-element vector If it is a scalar, it has to be a value of x near the point where the function crosses the x-axis If it is a vector, the two elements have to be points on opposite sides of the solution This implies that f(x0(1)) and f(x0(2)) have different signs SOLVING AN EQUATION WITH ONE VARIABLE

11 If a function has more than one solution, you can find the different solutions by calling fzero once for each solution and passing a different initial value x0 each time If function is a polynomial, using roots to find the solutions will give you all of them at once. A good way to get an initial value is to plot the function and see where it crosses the x-axis SOLVING AN EQUATION WITH ONE VARIABLE

12 In many applications you can estimate the domain of the solution Often when a function has more than one solution only one of the solutions will have a physical meaning SOLVING AN EQUATION WITH ONE VARIABLE

13 fzero finds zeros of a function only if they cross the x- axis, not just touch it If fzero can't find a solution, it returns NaN >> fzero( @(x)x^2, 0.1 ) % touches at x=0 Exiting fzero: aborting search for an interval containing a sign change because NaN or Inf function value encountered during search. (Function value at -1.37296e+154 is Inf.) Check function or try again with a different starting value. ans = NaN SOLVING AN EQUATION WITH ONE VARIABLE

14 fzero has many options. To see them all, type help fzero in Command Window or use Help Window One useful option [x fval] = fzero(function,x0) returns the value of function at x. You can get this value easily on your own but the above saves a little typing SOLVING AN EQUATION WITH ONE VARIABLE

15 Parabola with roots at 0 and 2 >> parabola = @(t)1 - (t-1)^2; Find a zero >> x = fzero( parabola, 4 ) x = 2 Get function value at the zero >> parabola( x ) ans = 0 Do both at once >> [x fval] = fzero( parabola, 4 ) x = 2 fval = 0

16 Example: Parabola with roots at 0 and 2 >> parabola = @(t)1 - (t-1)^2; % find a zero >> x = fzero(parabola, 4) x = 2 % function value at zero >> parabola(x) ans = 0

17 You can use fzero to find where a function has a particular value. Suppose you have f(x) = 0 and you want to find the x that makes f(x) = 3. You can rewrite this as f(x) - 3 = 0 and then, defining g(x) = f(x) – 3, use fzero to find the root (solution) of g(x). Example: x 3 +5x = 10 becomes x 3 + 5x – 10 = 0. Solving an Equation with One Variable >> g = 'x^3+5*x-10'; >> x = fzero(g, 1) x = 1.4233 fplot (g, [0,2]) grid on

18 Can also use fzero to find where a function has a particular value. Suppose given f(x)=0 and want to find the x that makes f(x)=3. Can rewrite this as f(x)-3=0, and, defining g(x) = f(x) – 3, use fzero to find root of g(x) SOLVING AN EQUATION WITH ONE VARIABLE

19 FINDING A MINIMUM OR A MAXIMUM OF A FUNCTION Often want to find the minimum or maximum of a function y = f(x) in a certain region of the horizontal axis. Can do that with [x fval] = fminbnd(function,x1,x2)

20 FINDING A MINIMUM OR A MAXIMUM OF A FUNCTION [x fval]= fminbnd(function,x1,x2) function can be a string or a handle function must accept a single scalar x function must return a single scalar – the value of the function at x x1 and x2 are the left and right boundaries of the region on the x-axis, i.e., x1 < x2 always

21 >> f=@(x)x^3 - 12*x^2 + 40.25*x - 36.5; >> [x fval] = fminbnd(f, 0, 8) x = 5.6073 fval = -11.8043 >> [x fval] = fminbnd(f, 0.5, 2) x = 0.5001 fval = -19.2482 Finding a Minimum of a Function

22 Function is entered as a string or a function handle (like fzero). quad uses the numerical technique called the "adaptive Simpson method". It calculates the integral with an absolute error smaller than 1.0e–6 The user must make sure that the function does not approach a vertical in the interval [a,b]. Numerical Integration - quad

23 q = quad( function, a, b ) Can enter function as string expression or function handle Same as with fzero in Section 9.1 quad calculates integral with absolute error smaller than 1.0e–6 Can change this number by adding optional tol argument to the command, i.e., q = quad(function,a,b,tol) tol defines maximum. Smaller value gives more accurate answer but takes longer to compute NUMERICAL INTEGRATION

24 Function is entered as a string or a function handle (like fzero). It is used exactly like quad. quadl uses the numerical technique called the "adaptive Lobatto method" which can be more efficient for high accuracies and smooth integrals. The user must make sure that the function does not approach a vertical in the interval [a,b]. Numerical Integration - quadl It is a lower case L

25 >> f = @(x)1./(x.^3-2*x-5); >> simpson = quad (f,0,2) simpson = -0.4605 >> lobatto = quadl (f,0,2) lobatto = -0.4605 Numerical Integration - Example

26 The trapz command: q = trapz( x, y ) Use when given (x,y) points, not function Often occurs with experimental data Uses trapezoidal method of numerical integration x and y must be same length the values in x must be in ascending order NUMERICAL INTEGRATION

27 Use trapz when you have coordinate (x,y) points, not a function (like experimental data). It uses the trapezoidal method of numerical integration. x and y must be of the same length. The values in x must be in ascending order. Numerical Integration - trapz >> x = [1 3 5 7 9]; >> y = [8 4 8 3 2]; >> q = trapz(x, y) q = 40

28 Three-dimensional (3-D) plots useful for presenting related 3-D points. Common cases are Scalar or vector function of two- independent variables Scalar or vector data measurements in 3-D space Movement over time 3-D space THREE-DIMENSIONAL PLOTS

29 LINE PLOTS A three-dimensional line plot is a plot obtained by connecting points in 3- D space. MATLAB command is x,y, and z must be same size Remaining arguments are same as in 2-D plots (Section 5.1)

30 Here is a first example of the plot3 command. Simple, linear but rather dull. x = [1 2 3 4]; y = [5 6 7 8]; z = [4 8 10 14]; plot3 (x,y,z) grid on 3D Line Plots

31 Let's look at a more beautiful plot. a = [0:0.001:4*pi]; b = sin (a); c = cos (a); plot3 (a,b,c) grid on 3D Line Plots

32 Let's look at this very pretty and colorful one. t = 0:0.1:6*pi; x = sqrt(t).* sin (2*t); y = sqrt(t).* cos (2*t); z1 = 0.5 * t; z2 = 0.5 * t; z1 = t; z3 = 0.25*t; plot3(x,y,z1,'b',x,y,z2,'r',x,y,z3,'g','linewidth',2) 3D Line Plots

33

34 3D MESH AND SURFACE PLOTS Mesh and surface plots are 3-D plots used to graph functions of the form z = f(x,y) x and y are independent variables, z is a dependent variable A mesh plot connects values of z with lines to form the outline of a surface A surface plot connects lines in a mesh plot with planes to show a solid representation of the surface

35 Three steps to making mesh or surface plot 1. Create grid in the x-y plane that contains points you're interested in 2. Calculate the value of z at every point of the grid 3. Make the plot MESH AND SURFACE PLOTS

36 Creating a grid in the x y plane (Cartesian coordinates): The grid is the set of points on which you want to evaluate z. For example MESH AND SURFACE PLOTS

37 Note that X is made of identical rows because each row of grid has the same x-coordinates Y is made of identical columns because each column of grid has same y-coordinates To make matrices, use MATLAB command

38 Z = X.*Y.^2./ (X.^2 + Y.^2) 3D Mesh and Surface Plots

39 The mesh command permits the creation of wireframe mesh colored graphs in 3D. The third dimension must be a matrix created from meshgrid and some other operation. Here is a simple example: x = [1 2 3 4]; y = [5 6 7 8]; z = [4 8 10 14]; [X,Y] = meshgrid (x,y); Z = X.*Y; mesh (X,Y,Z) 3D Mesh and Surface Plots Grid on by default. grid off to remove

40 The surf command permits the creation of surface-colored graphs in 3D. It works exactly like the mesh command. Here is a simple example: x = [1 2 3 4]; y = [5 6 7 8]; z = [4 8 10 14]; [X,Y] = meshgrid (x,y); Z = X.*Y; surf (X,Y,Z) 3D Mesh and Surface Plots

41

42 Again let's see more beautiful plots. c = [-pi:0.1:pi]; d = [-pi:0.1:pi]; [C,D] = meshgrid (c,d); E = sin (C).* sin (D); mesh (C,D,E) surf (C,D,E) 3D Mesh and Surface Plots

43 You can change the surface shading with the shading command (shading faceted is the default seen on the previous slide). shading flat shading interp 3D Mesh and Surface Plots

44 Additional comments on the mesh command: MATLAB colors surface plots with colors that vary with value of z Can make color constant by using the Plot Editor in the Figure Window or by using colormap command. ( See Help on colormap for details) By default, mesh draws a grid. Issue command grid off to prevent grid from appearing Can draw box around plot with box on MESH AND SURFACE PLOTS

45 You can also change the coloring of the graph with colormap. colormap (gray) colormap (cool) colormap (hot) 3D Mesh and Surface Plots

46 PLOTS WITH SPECIAL GRAPHICS Polar coordinates grid in the x y plane : To make 3-D plot of function z = f(r,θ) 1. Make grid of values of θ and r with meshgrid 2. Compute value of z at each grid point 3. Convert grid with polar coordinates to grid with Cartesian coordinates using MATLAB's pol2cart command 4. Make 3-D plot using values of z and the Cartesian coordinates

47

48 3D Plots for Special Graphs - Sphere It is extremely easy to plot a sphere. The sphere function returns the coordinates of a sphere with a specified number of faces (20 if the number is not specified). [X,Y,Z] = sphere (10); mesh (X,Y,Z) [X,Y,Z] = sphere (50); surf (X,Y,Z) shading interp colormap cool [X,Y,Z] = sphere (100); surf (X,Y,Z)

49 THE view COMMAND The view command controls direction from which you view plot. Command is view(az,el) or view([az el]) az – azimuth: angle (in degrees) in x y plane measured from negative y axis and positive in counterclockwise direction el – elevation: angle of elevation (in degrees) from x y plane. Positive in direction of positive z axis

50 Default view angles are az = -37.5o and el = 30o az = -37.5o and el = 30oaz = 20o and el = 35o THE view COMMAND

51 Can project 3-D curve onto 2-D plane by specific settings of azimuth and elevation See book examples of projections THE view COMMAND

52 END OF LESSON


Download ppt "Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem."

Similar presentations


Ads by Google