Docsity

MatLab functions - diffL, fzero, roots, fplot, quadl, trapz, logical | CS 101, Study notes of Computer Science

Toggle sidebar
bg1
6&7-2
Another example of the use of fzero.
How does plot(X) work with only one argument?
What is a script file?
Numeric Integration using trapz and quad/quadl
functions.
Readings: Matlab by Pratap Chapter 4.1,5.4
6&7-3
1. Problem Definition
Write Matlab code to solve the problem posed on the next slide.
Use the fzerofunction. Show how fzero “calls” a function
to find a root many tim es. We can “see” how the fzero function
works by saving the his tory of input values in global variables.
Two posts of height hl and hr are
connected by a rope of length L.
hl hr
xr
0x
Find a point x (on the ground) where the length of the
two segments of rope equals L.
pf3
pf4
pf5
pf8

Partial preview of the text

Download MatLab functions - diffL, fzero, roots, fplot, quadl, trapz, logical | CS 101 and more Study notes Computer Science in PDF only on Docsity!

6&7-

Another example of the use of fzero.

How does plot(X) work with only one argument?

What is a script file?

Numeric Integration using trapz and quad/quadl

functions.

Readings: Matlab by Pratap Chapter 4.1,5.

6&7-

  1. Problem Definition Write Matlab code to solve the problem posed on the next slide. Use the fzero function. Show how fzero “calls” a function to find a root many times. We can “see” how the fzero function works by saving the history of input values in global variables.

Two posts of height hl and hr are connected by a rope of length L.

hl hr

xr

0 x

Find a point x (on the ground) where the length of the two segments of rope equals L.

6&7-

  1. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)

Assume hl, xr, hr and L are given. The problem is to solve the following equation for x. L = length of left segment + length of right segment or L = sqrt((x-0)^2 + (0-hl)^2 ) + sqrt((xr-x) 2 + (hr-0)^2 )

or re-writing the above L - (sqrt((x-0) 2 + (0-hl)^2 ) + sqrt((xr-x)^2 + (hr-0) 2 )) = 0 now the above lhs expression is a function of x only so we will give it the name diffL. So the problem can be written as diffL(x) = 0 Find a root of the function diffL

6&7-

  1. Develop Algorithm (processing steps to solve problem)

Natural-Language Algorithm

In a previous lecture we wrote code for the function called xy_length. Even though this is not a built-in Matlab function we can still call this function in our program.

The input arguments to xy_length are two vectors containing the x-coordinates and y-coordinates of the 3 points.

6&7-

  1. Develop Algorithm (processing steps to solve problem)

Natural-Language Algorithm

We can save the history of values (x’s) passed to the function diffL by using the code,

global XDATA

XDATA = [XDATA,x]

and similarly since we compute the new lengths each time we can save the history of these values by,

global LDATA

LDATA = [LDATA,Lnew]

6&7-

  1. Write the “Function" (Code)

Use the Matlab editor to create a file diffL.m.

function dL = diffL(x) % function dL = diffL(x) % Function to compute the difference L - (length of the sum of % the two line segments formed by the points (0,hl) to (x,0) % and %(x,0) to (xr,hr)). % “x” is a scalar. % Programmer: Tom Gambill % Date: 2/13/ % data.m script file assigns two row vectors xvec = [0,xr] , % yvec = [hl,hr] % and a scalar L.

(continued on next slide)

6&7-

Since the variables LDATA and XDATA contain the history of Lnew and x values (on each call that fzero made to diffL) we can “see” these values by using the global command and then plotting them. Note that the plot function plots only one vector.

6&7-

The command:

plot(vector)

will plot “1:length(vector)” vs “vector”.

As with plot(x,y) you can optionally specify a color, linestyle and marker.

plot(vector’color_linestyle_marker’);

See Lecture 2 slides 7,8 for a list of some options.

6&7-

Most integrals arising from solutions of problems in engineering and the physical sciences cannot be represented in "closed form"- they must be evaluated numerically.

  • For a function of a single variable, we seek an approximation to the area "under" the curve:

a b

af^ (^ x^ )^ dx =^ Sum of the “signed” areas

b ∫

x

f(x)

Definite Integral

The Matlab functions: quad (quad8) and trapz only apply to continuous functions f(x) of one variable - x ( a scalar).

6&7-

f(x)

x 0 = a x 1 x 2 x 3... Xn-1 xn = b

In this case, we approximate the integral by adding the areas of a set of approximating rectangles.

y

x

Approximation of f(x) by using constant functions on the intervals [x (^) k-1 , x (^) k ] (not a good approximation).

y 1

y 2

y 3

yn

f ( x ) ≈ f ( x k ) x k − 1 ≤ x ≤ x k , k = 1, 2 , " , n

6&7-

In this case, we approximate the integral by adding the areas of a set of approximating trapezoids.

Note that we should use more points in the partition of [a.b] since the approximation at x (^) n-1 shown above is not good.

f(x)

x =a 0 x (^1) x 2... xn-1 x =bn

y 0

y 1

y 2

Approximation of f(x) by using a linear function on the intervals [x (^) k-1 , x (^) k ] (a better approximation).

y

x

Trapezoidal Rule

6&7-

  1. Problem Definition Write a Matlab function named count_axles to compute the number of 4 axle vehicles traveling past a specific location on a road over a fixed time interval.
  2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) The input to count_axles will be a vector containing the number of axles for each vehicle that passes a specific location. A second input will be the number of axels for which you want to obtain a count. In our example this value is 4.

6&7-

  1. Develop Algorithm (processing steps to solve problem) We first identify each vehicle with the desired number of axles. For example if, >> data = [2 3 4 4 2 4 4]; then the vector identifying the 4 axle vehicles is, match = [0 0 1 1 0 1 1] Use the relational operator == to find matches >> match = data == 4 match = [0 0 1 1 0 1 1] Next, use the sum function to add all the 1’s. Note that match is a logical valued vector >> whos match Name Size Bytes Class

match 1x7 7 logical array

6&7-

4. Write the “Function" (Code)

function count = count_axles(data, target) % function count = count_axles(data, target)

match = data == target ; count = sum( match);

6&7-

The relational operators are: < <= > >= == ~=

Relational operators generate logical values.

match = [2 3 4 4 2 4 4] ~= 4 match = 1 1 0 0 1 0 0

6&7-

Example: Extract the bad data from the data vector. Suppose negative values are bad data.

data = [2 -5 3 4 -1 4 2 4 4 -9]; match = data > 0 match = 1 0 1 1 0 1 1 1 1 0

data = data(match) data = 2 3 4 4 2 4 4

6&7-

Problems subscripting with logicals. Rather than using a relational operator to generate a logical array we can use the logical function.

data = [2 -5 3 4 -1 4 2 4 4 -9]; match = logical([1 0 1 1 0 1 1 1 1 0]); data = data(match) data = 2 3 4 4 2 4 4 Note that the following does NOT work! match = [1 0 1 1 0 1 1 1 1 0]; data = data(match)

??? Subscript indices must either be real positive integers or logicals.

6&7-

We can use the Matlab functions fzero to find zeros or roots of a function of one variable. The function fzero is generic but finds only one root, the function roots applies only to polynomials but finds all roots.

The function fplot is used when you have an explicit function, (user defined or built-in). The plot function can be used for both explicitly defined functions and raw data for which we don’t know the functional relationship.

For numeric integration we can use quadl when we

know the function f(x). The function trapz is used

when we don’t know f(x) explicitly.

Logical values can be generated by the relational operators or the logical function.