This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Homework 6 Solutions
1. f(x) = x1/3+0.5x22=0 x0 = 2 1/3 term g2(x) is for the 0.5x2 term g1(x) will be for the x
X” = 0.5x2 2 a g1(x) = (0.5x2  2)3
0.5x2 = x“3 + 2 9 x2 = 2x“3 + 4 9 g2(x) = (2x1/3 + 4)“2
Earle)
g1’(x) = 3(0.5x2 — 2)2(x) = 3x(o.5x2  2)z
81'(X) = 3*(5*4  2)2(2) = 0
While this is less than 1, it doesn’t work. My guess is it is a singularity and something different happens at this point. The derivative is continuous. I don’t know why this doesn’t work. «wag ; «51X to new toolbar buttons: data brushing 8; linked glots g Q Play video
t v, ..» a £5: d i . , . g ‘ . . , A .,..;~.;M s
. 1 .‘ an .mk‘uz g2'(x) = yzile/3 + 4)‘1/2(2/3*x'2/3) a (1/3*x'2/3)(2x1/3 + 4).”2
g2’(x) = (1/3*2'2/3)(2*21/3 + 4)“2 = 0.08224
ThereforeI we should use ggix) for our iterations.
Pint—bl
g1(x) = (0.5x2 — 2)3
‘ 82(X) = (2x1/3 + 4?”
Starting with gﬂ
I gziz) = (2*21/3 + 4)“2 = 2.5534
g2(2.5534) = (2*255341/3 + 4)“2 = 2.5949
g2(2.5949) = (2*2.59491/3 + 4)“2 = 2.5978
g2(2.5978) = (2*259781/3 + 4)”2 = 2.59795
g2(2.59795) = (2*2597951/3 + 4)“2 = 2.59796 This is converged
Now using gig);
g1(2) = (0.5*22 — 2)3 = o
g1(0) = (05"‘02—2)3 = 8
g1(—8) = (o.5*(8)2  2)3 = 27000
g1(27000) = (o.5*270002 — 2)3 = 4.8428E25 g1(4.8428E25) = (O.5*4.8428E252 — 2)3 = 1.6124E153 This will not converge 2. xL = 2 xU = 3 f(x) = x1/3+0.5x2—2=0
False Position Method:
ck = [f(bk)ak — f(ak)bk]/[f(bk)  f(ak)] where ak = XL and bk = xu
f(bk) = (3)1/3 + 0.5*32 — 2 = 1.05775 f(ak) = (2)“3 + 0.5*22 — 2 = 1.2599
ck = (1.05775*2 — (1.2599*3))/(1.05775(42599)) = 2.5436 f(ck) = (2.5436)1/3 + 0.5*2.54362 .— 2 = O.130067 Since this is the same sign as ak, ck becomes ak. cm = (1.05775*2.5436 — (0.130067*3))/(1.05775(0.130067)) = 2.59359
f(ck+1) = (2.59359)“3 + 0.5*2.593592 — 2 = 0010584
This is still the same sign as ak; therefore, cm becomes am. cm = (1.05775*2.59359 — (0.010584*3))/(1.05775(0.010584)) = 2.59762 Secant Method: Xk+1 = Xk ' “kale " Xk1)/(f(xk) — f(xk1))l xk = XL xk.1 = xu flxk) = f(ak) = 1.2599 f(xk_1) = f(bk) = 1.05775 xk+1 = 2 — (1.2599)[(2  3)/(1.2599 — 105775)] = 2.5436 f(xk.1) = (2.5436)“3 + o.5*2.54362 — 2 = 0.130067 x...2 = 2.5436 — (0.130067)[(2.5436 — 2)/(o.130067 — (1.2599))] = 2.6062 f(xk+2) = —(2.6062)1/3 + O.5*2.60622 — 2 = 0.019967 Xk+3 = 2.6062 — (0.019967)[(2.6062 — 2.5436)/(0.019967  (0.130067))] = 2.59787 f(x) = x1/3+0.5x22=0 f’ (x) = 1/3*x'2/3 + x x0 = 2
Xk+1 = Xk " f(xk)/f’(xk)
f(2) = (2)1/3 + 0.5*22 — 2 = 1.2599
f’(2) = 1/3*2"2/3 + 2 = 1.79001
xk.1 = 2 — (1.2599/1.79oo1) = w % relative error = 2.70386 — 2 [/2 *100 = w
f(2.70386) = (2.70386)1/3 + o.5*2.703862 — 2 = 0.262293
f’(2.70386) = 1/3*2.70386‘2/3 + 2.70386 = 2.53211
xk+2 = 2.70386 — (0.262293/2.53211) = mg; % relative error = [2.60027 — 2.70386 l/2.70386 = 3.83% f(2.60027) = —(2.60027)1/3 + 0.5*2.600272 — 2 = 0.005597
f’(2.60027) = 91/3*2.60027'2/3 + 2.60027 = 2.424
xk+3 = 2.60027 — (0.005597/2.424) = 2.59797 % relative error = [2.59797 — 2.60027 l/2.60027 = 0.0888% 10/17/08 1:52 PM C:\Documents and Settings\gtj223\Desktop\Grad\...\myrootfinder.m 1 of 3
function [xroot, froot] = myrootfinder(fun,x1,x2,method)
% myrootfinder Root solver for any given function
%
% Synopsis: [xroot,froot] = myrootfinder(func,x1,x2,method)
% Input: fun = (string) name of m~file that returns f(x) and f'(x)
% x1 = An initial guess. Set as the lower bound.
% x2 = A second initial guess. Set as the upper bound.
% method 2 1 — bisection, 2—false position, 3—secant, 4—Newtons
% Output: xroot = The x value where the function is zero
% froot = The actual value of the function at xroot should be ~ 0
tol = 1E—8; %Tolerance value
zeta = 2*tol; %Establish zeta as greater than the tolerance so that
%you enter the while loop V
% Bisection method
if method ==
a = x1; b = x2;
fa = feval(fun,a); %Calling a function that evaluates a given function.
%dfdx isn't needed until Newton’s method so there
fb = feval(fun,b); %is no need to reassign it. It will simply exist.
%There are multiple ways to check here. You can do fa*fb and see if it
%is negative (that implies that only one is negative). You can say (fa<0
%& fb<0) (fa>0 & fb>0). I used the sign function in matlab. It is used
%in the book as well.
if sign(fa) == sign(fb) %Checking to make sure our signs are opposite for
%the function values. Intermediate Value
%Theorem check.
fprintf(‘Root is not bracketed on interval [%f,%f]',x1,x2)
xroot = 'No root found on interval‘;
froot = 'No function value because no root“;
else
while zeta > tol %Just a randomly low value for our convergence
xm = a + .5*(b—a); %Equation for bisection method
fxm = feval(fun,xm); %Function call again but this one is
%used to evaluate our new value for
%xm.
if sign(fxm) == sign(fa) %Sign check to make sure we have opposite signs.
a = xm; %Reassign xm to a and then we can iterate again
zeta = abs(a — b)/b;
xroot = a; %We will claim a new root every time through. The
%last time through will be the final xroot.
froot = fxm; %Same for froot.
else %Same as the if statement only it exists in case %fxm isn't the same sign as fa.
b = xm;
zeta = abs(b  a)/a; i
l
l
i
E
.
r
i
l
i 10/17/08 1:52 PM C:\Documents and Settings\gtj223\Desktop\Grad\...\myrootfinder.m 2 of 3
W xroot = b;
froot = fxm;
end
end
end
%False Position Method :
elseif method == i
xL=x1; xU=x2; g
fo = feval(fun,xL); %Same function call. g
fo = feval(fun,xU); E
if sign(fo) == sign(fo) %Same check ,
fprintf('Root is not bracketed on interval [%f,%f]',x1,x2)
xroot = 'No root on interval';
froot = 'No function value because no root';
else I
while zeta > tol
fo = feval(fun,xL); %We have to call this function again
%because in this method we use different
fo = feval(fun!xU); %function values where the bisection
%only changes the root values.
ck = (fo*xL  fo*xU)/(fo — fo); %False method equation
fck = feval(fun,ck);
if sign(fck) == sign(fo)
xL = ck;
zeta = abs(ck — xU)/xU;
xroot = xL;
froot = fck;
elseif sign(fck) == sign(fo)
xU = ck;
zeta = abs(ck — xL)/xL;
xroot = xU;
froot = fck;
end
end
end %Secant Method %Same as the false postition method but we have a slightly different ck
%formula. Also, there is no need for sign checking since we are simply
%incrementing not replacing if you will.
elseif method == 3 xL = x1; xU = x2; %This while loop is different. Before, we would find an X value and
%then we would plug it back in to solve for another x value but we
%had to have two separate signs because of the IMV theorem. Now we
%don't. We aren't trying to converge in the same manner. Sign is
%now irrelevant as long as we know that a root exists in our
%interval (that is why we do the check earlier).
while zeta > tol fo: feval(fun,xL); 10/17/08 1:52 PM C:\Documents and Settings\gtj223\Desktop\Grad\...\myrootfinder.m 3 of 3 W“ fo= feval(fun,xU);
xkl = xL — fo*(xL  xU)/(fo — fo); %Different iteration formula.
ka1 = feval(fun,xkl);
zeta = abs(xk1 — xU)/xU;
XL = XU;
xU = xkl;
xroot = xkl;
froot = ka1;
end %Newton's Method
elseif method == 4
XL = x1; xU = x2; %Note no Sign checking. This is similar to the secant method but it
%uses a derivative for f as well.
while zeta > tol [f,dfdx] = feval(fun,xL); fo = f; dfdxxL = dfdx; xkl = xL — fo/dfdxxL; kal = feval(fun,xkl); zeta = abs(xk1 — xL)/xL; xL xkl; xroot = xkl; froot = ka1;
end %If someone typed in a method 5, we don’t have one that corresponds to it
%so we add an error message in case. else
fprintf(’Method chosen, %f, is not specified for this function.\n Chose 1 fort bisection,\n 2 for false position,\n 3 for secant,\n 4 for Newton.\n’,method)
xroot = 'No xroot can be found with this method';
froot = ’No function value because no xroot'; end 10/17/08 1:52 PM C:\Documents and Settings\gtj223\Desktop\Gra...\mynonlineareqn.m 1 of 1 W function [f,dfdx] = mynonlineareqn(x) o z fx3n Evaluate f(x)= .5*x‘2 — X‘(1/3) — 2 and dfdx %We can manually change our function in this line and then the rootfinder
%will use the new function.
f = .5*(x)‘2 — x‘(1/3) — 2;
dfdx = x — (1/3)*x‘(—2/3); vWWWMW._.M«—_—___mmmm.Mh‘wwmwwmw.»_44« 4 10/17/08 1:54 PM MATLAB Command Window >> [xroot, froot] = myrootfinder('mynonlineareqn',2,3,1)
xroot = 2.5980 froot = 7.6594e—009
>> [xroot, froot] = myrootfinder('mynonlineareqn',2,3,2)
xroot = 2.5980 froot = 4.4409e—016
>> [xroot, froot] = myrootfinder('mynonlineareqn',2,3,3)
xroot = 2.5980 froot = 4.4409e—016
>> [xroot, froot] = myrootfinder('mynonlineareqn',2,3,4)
xroot = 2.5980 froot =
4.4409e—016 >> [xroot, froot] = myrootfinder('mynonlineareqn',2,3,5) Method chosen, 5.000000, is not specified for this function. Chose 1 for bisection,
2 for false position,
3 for secant,
4 for Newton. XI‘OOt = 1 of 2 WW“— ',‘1' 10/17/08 1:54 PM MATLAB Command Window 2 of 2
MW No xroot can be found with this method froot =
No function value because no xroot
>> [xroot, froot] = myrootfinder('mynonlineareqn',2.7,3.5,1) Root is not bracketed on interval [2.700000,3.500000]
xroot = No root found on interval a
E
froot = t
E
No function value because no root 3 >> ...
View
Full Document
 Fall '06
 Klaus

Click to edit the document details