(a) m-file for function d(x):
function y = d(x)
y = sqrt(x.^2 + (x.^2 - 2*x -1).^2);Matlab program:
fplot(@(x) d(x),[-2 4],'b')
[xmin, dmin] = fminbnd(@d,-2,4)
ymin = xmin^2 - 2*xmin - 1
figure
fplot(@(x) x.^2-2*x-1,[-2,4],'b')
hold on
plot(xmin,ymin,'bo')Output
xmin =
-0.3660
dmin =
0.3898
ymin =
-0.1340(b)
>> x1 = fzero(@(x) d(x)-4,-2)
y1 = x1^2 - 2*x1 - 1
x2 = fzero(@(x) d(x)-4,4)
y2 = x2^2 - 2*x2 - 1
x1 =
-1.3975
y1 =
3.7479
x2 =
3.1216
y2 =
2.5011So points (-1.3975,3.7479) and (3.1216, 2.5011) are at distance 4 from the origin