numerical analysis 3d plots. a numerical method is a technique for computing a numerical...

52
Numerical Analysis 3D Plots

Upload: tabitha-hood

Post on 17-Jan-2016

235 views

Category:

Documents


5 download

TRANSCRIPT

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

Numerical Analysis3D Plots

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

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

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

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 zeroMaking the equation be true is called satisfying

the equation

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

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

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

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

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

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

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

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

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

• 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 sameThe procedure lets the user define

"almost the same", e.g., absolute value of the difference is less than 10-4

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

• 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.

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

•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

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

• 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

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

• 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

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

• 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

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

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

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

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

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

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

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

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: x3+5x = 10 becomes x3 + 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

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

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

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

• 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)

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

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

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

>> 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

0 1 2 3 4 5 6 7 8-40

-30

-20

-10

0

10

20

30

x

f(x)

Finding a Minimum of a Function

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

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

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

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

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

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

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

>> 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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Z = X.*Y.^2 ./ (X.^2 + Y.^2)

3D Mesh and Surface Plots

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

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

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

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

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

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

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

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

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

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

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

You can also change the coloring of the graph with colormap.colormap (gray)

colormap (cool)

colormap (hot)

3D Mesh and Surface Plots

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

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

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

3D Plots for Special Graphs - SphereIt 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 interpcolormap cool

[X,Y,Z] = sphere (100);surf (X,Y,Z)

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

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

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

Default view angles are az = -37.5o and el = 30o

az = -37.5o and el = 30o

az = 20o and el = 35o

THE view COMMAND

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

Can project 3-D curve onto 2-D plane by specific settings of azimuth and elevation

See book examples of projections

THE view COMMAND

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

END OF LESSON