Chapter 8 #11: (polyval, polyder, roots)

advertisement
Chapter 8
#11: (polyval, polyder, roots)
(a) Set up the polynomial by hand. We need to find h with respect to x first. Notice that the
total length for h’s is 20*12-4*x-4*(x+15) = 20*12-8*x-60. So each h is
(20×12−8𝑥−60)
4
.
Thus we have
𝑉 = 𝑥(𝑥 + 15)
(20×12−8𝑥−60)
4
= 𝑥(𝑥 + 15)(45 − 2𝑥) = −2𝑥 3 + 15𝑥 2 + 675𝑥.
(b) Use polyval to plot (p262-263):
1) Define x as a vector (x=0: 0.1: 20).
2) Define p as a vector with the coefficients of the polynomial in part (a), i.e.
p=[-2 15 675 0];
3) Define V as a vector using the command polyval, V=ployval (p,x)
4) Plot the vectors x and V using the command plot.
(c) Use the command polyder and roots (p266-267) to find the critical values (using the
graph to determine which is the maximum):
1) Find the vector k=polyder(p). Here k is a vector with the coefficients of the
polynomial that is the derivative.
2) Find the roots of the derivative: r=roots(k). Here r is the column vector with the
roots of the polynomial.
3) Use the graph to determine which the maximum is. Find the maximum value at that
point using the command polyval.
#12 (polyval, polyder, roots)
(a) It’s easy to find the volume, which is 𝑉 = 𝑥(40 − 2𝑥)(22 − 2𝑥) = 4𝑥 3 − 124𝑥 2 + 880𝑥.
(b) Do the same as #11 part (b):
1) Define x as a vector (x=0: 0.1: 11).
2) Define p as a vector with the coefficients of the polynomial in part (a), i.e.
p=[4 -124 880 0];
3) Define V as a vector using the command polyval, V=ployval (p,x)
4) Plot the vectors x and V using the command plot.
(c) We need to solve 4𝑥 3 − 124𝑥 2 + 880𝑥 − 1000 = 0 for x. So define a new vector
p1=[4 -124 880 -1000], which represents the polynomial on the left hand side. Then use
the command root to solve, i.e. xroot=roots(p1).
(d) Use the command polyder and roots (p266-267) to find the critical values (using the
graph to determine which is the maximum):
1) Find the vector k=polyder(p). Here k is a vector with the coefficients of the polynomial
that is the derivative.
2) Find the roots of the derivative: r=roots(k). Here r is the column vector with the roots of
the polynomial.
3) Use the graph to determine which the maximum is. Find the maximum value at that
point using the command polyval.
#23 (polyfit, polyval, interp1) See the example after the hint.
Notice that you only need to do part a).
(a) Use the command polyfit (p267-270) to find the third order polynomial.
(b) Interpolation is explained on pp274-277. Define your data, then create a plottable vector
(xi in the example on p277), and use the "interp1" (ends with the number one) to create
the y vector. Since there are two graphs, use the "figure" command to separate the plots
(do NOT plot them on the same graph!).
Example for #23: Suppose we have the following data:
x
0
1
2
3
4
5
y
0.2
10
16
60
109
210
(a) Curve-fit the data with a third-order polynomial. Use the polynomial to estimate the
value of y when x=4.5.
(b) Fit the data with linear and spline interpolations and use each interpolation to
estimate the value of y when x=4.5.
We have script file:
% Define the data as vectors x and y
x=[0:5];
y=[0.2 10 16 60 109 210];
%Part(a)
disp('(a)')
%Create vector xp with points for the curve-fit
xp=linspace(0,5,100);
%Use the command polyfit to find a third-order polynomial
%Here x and y are the data given in the problem. 3 is the degree of
the
%polynomial. p is the vector of the coefficients of the polynomial
that
%fits the data.
p=polyfit(x,y,3);
%Find the vector yp corresponding to the third-order polynomial
yp=polyval(p,xp);
%Plot the data points with 'o' and the curve.
plot(x,y,'o',xp,yp)
%Part(b)
disp('(b)')
%Create vector xp with points for interpolation
xp=linspace(0,5,100);
%Calculate y points from linear interpolation.
ypLin=interp1(x,y,xp,'linear');
%Calculate y points from spline interpolation
ypSpl=interp1(x,y,xp,'spline');
figure
%Plot the data and the linear interpolation
plot(x,y,'o',xp,ypLin)
xlabel('x')
ylabel('y')
legend('Data','Linear Interpolation')
figure
%Plot the data and the spline interpolation
plot(x,y,'o',xp,ypSpl)
xlabel('x')
ylabel('y')
legend('Data','SplineInterpolation')
%Estimate the corresponding y value of the interpolations
y_Linear=interp1(x,y,4.5,'linear')
y_Spline=interp1(x,y,4.5,'spline')
We have three figures:
In the command window, we have:
(a)
yvalue =
155.6107
(b)
y_Linear =
159.5000
y_Spline =
148.4175
#25 (plot, semilogx, semilogy, loglog, polyfit polyval)
(a) Define the vector h=0:3000:33000; Then define the vector D to be the corresponding
data from the table. Then make four plots using the following four commands (Check
page 149 for details). Use the "figure" command to separate the plots (do NOT plot them
on the same graph!):
1) plot (h, D, ‘o’)
2) semilogx (h, D, ‘o’)
3) semilogy (h, D, ‘o’)
4) loglog (h,D ‘o’)
Here semilogx plots y versus x with a log (base 10) scale for the x axis and linear scale for
the y axis. And semilogy plots y versus x with a log (base 10) scale for the y axis and
linear scale for the x axis. Finally, loglog plots y versus x with a log (base 10) scale for
both axes.
(b) After you get the three plots, you will find that in the 3rd plot the points appear to be a
straight line. So we fit the data with exponential function. Thus the function is
𝑦 = 𝑏𝑒 𝑚𝑥
We need to find the constant b and m in this question. Please read carefully page 271273 about how to solve this. Especially the example on page 271. Otherwise it’s hard to
understand why the following steps work. Indeed, you just need to use the code in the
Example on page 273 with changed data:
1) Use the ployfit function with h and log(D), i.e. p=polyfit(h, log(D), 1). Here p is the
vector of the coefficients. The first element, p(1), is the constant m. The second
2)
3)
4)
5)
element, p(2), is b for the logarithmic function. Thus p(2)=ln(b), then 𝑏 = 𝑒 (𝑝(2)) .
Find the constant b=exp(p(2)), and the constant m=p(1).
Define the vector hp for the plot: hp=[0:0.1:33000];
Find the corresponding for the function: Dp=b*exp(m*hp);
Plot the data and the curve for Dp on the same graph using the command plot.
#26 (polyfit, powerfit, plot)
1. Write a user-defined function with the first line: function [b, m] = powerfit (x y)
Check page 272 for details. Inside the function we need to find the constant b and m
for the function 𝑦 = 𝑏𝑥 𝑚 . Thus we use the command p=polyfit(log(x),log(y),1) to find
the vector p. Then we have m=p(1) and b=exp(p(2)).
2. Define x and y using the data. Then apply the function to find the constant b and m.
Then define a vector xp to plot, i.e. xp=[0.5:0.1:7.8]. Find the corresponding yp by
𝑦𝑝 = 𝑏𝑥𝑝𝑚 . Then plot the data (x,y) and the curve(xp,yp) on the same graph using
the command plot.
Chapter 9
#4 (fplot, fzero) See the example after the hint.
1. Define FA as an anonymous function(p231-233)
2. Plot the function using the command fplot.
3. Use the command fzero (p297) to find the roots.
Example : Determine the two roots of the equation
3𝑥𝑒 −𝑥 − 0.4 = 0
We have script file:
%Creating an anonymous function. Note that you need move all terms to one
%side.
FA=@ (x) 3*x*exp(-x)-0.4;
%Use fplot to plot the function
fplot(FA,[0 6])
xlabel('x')
ylabel('f(x)')
%After ploting the figure, we could use the graph to find the estimated
%points where the curve crosses the x-axis.
%Use the command fzero to find two roots near x=0.2 and x=3
x1=fzero(FA,0.2)
x2=fzero(FA,3)
In the command window, we have:
x1 =
0.1558
x2 =
3.1680
We have the graph of f:
#12(fminbnd)
1) First we find h. Notice that ℎ = 2√𝑅2 − 𝑟 2 . So the volume of the cylinder is 𝑉 = 𝜋𝑟 2 ℎ.
Thus in your script file, we first define the length of R. Then define the vector r=0:0.1:14.
2) Define the corresponding vectors h and D using the two formulas we derived above.
3) Plot the vectors r and V using the command plot.
4) Notice that the graph has a maximum value, but the command fminbnd gives the local
minimum point inside some interval. So in order to use this command, we need first find
the minimum point for the inverse of the volume.
Define the inverse of the volume using anonymous function (p231-233): VA=@ (x) pi*x^2*2*sqrt(R^2-x^2); Please pay particular attention to the negative sign.
5) Use the command fminbnd(page 299) to find the local minimum point inside the
interval (11, 13), i.e. rmax=fminbnd(VA, 11, 13).
6) Find the corresponding hmax using ℎ𝑚𝑎𝑥 = 2√𝑅 2 − 𝑟𝑚𝑎𝑥 2
# 27(quad)
1. Define the values for a and b. Then calculate k using 𝑘 =
√𝑎2 −𝑏 2
𝑎
.
2. Define an anonymous function f (page 231-233), where 𝑓 = √1 − 𝑘 2 𝑠𝑖𝑛2 𝜃.
𝜋
3. Find the integral 𝑖𝑛𝑡 = ∫02 √1 − 𝑘 2 𝑠𝑖𝑛2 𝜃 𝑑𝜃, using the command int=quad(f,0,pi/2).
4. Calculate P=4*a*int.
𝑃
5. Calculate the average speed: 𝐴𝑣𝑒 = 24×265×248.
# 32 (ode45)
Differential equations are extremely practical in engineering, and most cannot be solved
explicitly. The best that can be done is to numerically approximate the solutions (you will learn
how this works in MATH 308). The steps for doing so in MATLAB are on pp303-307 and
summarized here:
1. Make sure the problem is in the form dy/dx = f(x,y)
2. Define an anonymous function f (page 231-233), here we have two independent
variables x and y, so the anonymous function is f=@(x,y) followed by the equation in the
problem.
3. We will stick with ode45 as the "solver_name". In general, we have the command
line: [x,y] = ode45(ODEfun,xspan,y0);, where ODEfun is the function we defined in step 2 and
xspan is the vector x with the range (in this problem xspan is [1:0.1:5]) and y0 is the initial value
of y (in this problem y0 is 1 since y(0)=1). The output [x, y] is the solution for the ODE, where
x, y are column vectors. We don’t need to see the vectors, so use a semicolon here.
4. Plot the vectors x and use the command plot.
#39 (ode 45) Same process as #32
If you understand what’s going on in the previous problem, you will figure out this one.
Download