Friday, April 27, 2012

Homework #11

13.7
For this problem I first graphed he equation to see if there was a maximum for that range of x, and there was so I implemented the Golden-Section Search and reached the convergence of the true value of 0.8408 at x= -0.3472


Matlab:

%Graph that shows theres a maximum between -2:1
x = (-2:0.1:1);
f = @(x) -x.^4 - 2*x.^3 - 8*x.^2 - 5*x;
plot(x,f(x));

%plot(x,f_x(x));

xl = -2;
xu = 1;
 d = ((sqrt(5) - 1)/2)*(xu-xl);
 x1 = xl + d;
 x2 = xu - d;

 sol= [xl f(xl) x2 f(x2) x1 f(x1) xu f(xu) d]
 for iter = 1:13
 if f(x2) > f(x1)
    xu = x1;
    x1 = x2;
    d = ((sqrt(5) - 1)/2)*(xu-xl);
    x2 = xu - d;
 end
 if f(x1) > f(x2)
    xl = x2;
    x2 = x1;
    d = ((sqrt(5) - 1)/2)*(xu-xl);
    x1 = xl + d; 
 end

 sol= [xl f(xl) x2 f(x2) x1 f(x1) xu f(xu) ]

 end
 hold on;
 plot(x2,f(x2),'*r')




13.8

(a) for this problem I used the some code you provided us and altered it to work with this problem. 

Output:
GoldenRatio = -0.3472    0.8408
Parabolic = -0.3473    0.8408
Newton =  -0.3473    0.8408




Matlab:

theta0 = 50;

x = [-2:0.01:1];
y = @(x) -x.^4 - 2*x.^3 - 8*x.^2 - 5*x;
figure( 1);
plot(x,y(x));
hold on;
% Golden Ration Search
xl= -2;
xu = 1;
for ii = 0:26,
d = (xu-xl)*(sqrt(5)-1)/2;
x1 = xl+d;
x2 = xu-d;
if(y(x1)>y(x2))
xl = x2;
else
xu = x1;
end;
plot(xu,y(xu),'*r')
plot(xl,y(xl),'*r')
end
plot(xu,y(xu),'*g')
GoldenRatio = [xu y(xu)]
figure(2)
x = [0:0.01:20];
plot(x,y(x));
hold on;
x0 = -2;
x1 = -1;
x2 = 1;
% parabolic interpolation
for ii = 0:6,
x3 = y(x0)*(x1^2-x2^2) + y(x1)*(x2^2-x0^2)+y(x2)*(x0^2-x1^2);
x3 = x3/(2*y(x0)*(x1-x2)+2*y(x1)*(x2-x0)+2*y(x2)*(x0-x1));
if(abs(x3-x1)<1.0e-6) break; end
if(y(x3)>y(x1))
x0 = x1;
x1 = x3;
% x2 = x2;
else
x2 = x1;
x1 = x3;
% x0 = x0; 
end;
plot(x3,y(x3),'*r')
end
plot(x3,y(x3),'*g')
Parabolic = [x3 y(x3)]
% Newtons
figure(3)
y = @(x) -x.^4 - 2*x.^3 - 8*x.^2 - 5*x;
yp = @(x) -4*x.^3 -6*x.^2 -16*x -5;
ypp = @(x) -4*(3*x.^2 + 3*x +4);
x = [0:0.01:20];
plot(x,y(x),x,yp(x))
hold
x = 0;
for ii = 0:6,
x = x-yp(x)/ypp(x);
plot(x,y(x),'*r')
end
Newton = [x y(x)]
plot(x,y(x),'*g')







13.21

For this problem I implemented the golden section search to calculate the time and altitude of peak elevation
Output:
GoldenRatio = 3.8699(time)  192.8537(elevation)



Matlab:
g = 9.81;
z0 = 100;
v0 = 55;
m = 80;
c= 15;
t = (0:0.01:10);
f = @(t) z0 + (m/c)*(v0+((m*g)/c))*(1-exp(-((c/m)*t))) - ((m*g)/c)*t;
figure(1);
plot(t,f(t));
hold on;
% Golden Ration Search
xl= 0;
xu = 10;
for ii = 0:10,
d = (xu-xl)*(sqrt(5)-1)/2;
x1 = xl+d;
x2 = xu-d;
if(f(x1) > f(x2))
xl = x2;
else
xu = x1;
end;
plot(xu,f(xu),'*r')
plot(xl,f(xl),'*r')
end
plot(xu,f(xu),'*g')
GoldenRatio = [xu f(xu)]





13.22







No comments:

Post a Comment