




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





6&7-
6&7-
Two posts of height hl and hr are connected by a rope of length L.
Find a point x (on the ground) where the length of the two segments of rope equals L.
6&7-
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-
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-
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-
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:
will plot “1:length(vector)” vs “vector”.
As with plot(x,y) you can optionally specify a color, linestyle and 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.
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)
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
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-
6&7-
match 1x7 7 logical array
6&7-
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.
when we don’t know f(x) explicitly.
Logical values can be generated by the relational operators or the logical function.