FALL SEM - (2020 - 21) MAT2003: Submitted By: Sriharshitha Deepala REG NO: 19BCD7246 Lab No: 10 Slot: L6
FALL SEM - (2020 - 21) MAT2003: Submitted By: Sriharshitha Deepala REG NO: 19BCD7246 Lab No: 10 Slot: L6
MAT2003
CODE:
clc;
clear all;
n=input('No of iterations : ');
a=input('Lower limit : ');
b=input('Upper limit : ');
fo=1;
fn=1;
func = @(x)(x^2+54/x);
for i=1:n+1
if i==1 || i==2
f(i)=1;
continue;
end
f(i)=fo+fn;
fo=fn;
fn=f(i);
end
L2=(b-a)*f(n+1-2)/f(n+1);
j=2;
while j<n+1
L1=(b-a);
if L2>L1/2
anew=b-L2;
bnew=a+L2;
else if L2<=L1/2
anew=a+L2;
bnew=b-L2;
end
end
k1=func(anew);
k2=func(bnew);
if k2>k1
b=bnew;
L2=f(n+1-j)*L1/f(n+1-j+2);
else if k2<k1
a=anew;
L2=f(n+1-j)*L1/f(n+1-(j-2));
else if k2==k1
b=bnew;
L2=f(n+1-j)*[b-a]/f(n+1-(j-2));
j=j+1;
end
end
end
j=j+1;
end
fprintf('[a,b] is reduced to the range of [%.3f,%.3f]',a,b);
OUTPUT:
2. Minimize the function f(x) = x2 + 54/x using Golden search
method. ([a,b] = [0,5]).
CODE:
clc;
clear all;
a=input('Lower limit : ');
b=input('Upper limit : ');
e = input("Enter any small number :");
f = @(x)(x^2+54/x);
k = 2;
a_new = a;
b_new = b;
Lw = b - a;
aw = (a_new - a)/(b - a);
bw = (b_new - a)/(b - a);
Lw = bw - aw;
w1 = aw + 0.618*Lw;
w2 = bw - 0.618*Lw;
y1 = feval(f,(w1));
y2 = feval(f,(w2));
while Lw >e
aw = (a_new - a)/(b - a);
bw = (b_new - a)/(b - a);
Lw = bw - aw;
w1 = aw + 0.618*Lw;
w2 = bw - 0.618*Lw;
if mod(Lw,2)==0
y1 = feval(f,(w1));
else
y2 = feval(f,(w2));
end
if y1>y2
a_new = w1;
else
b_new = w2;
end
c = 0.5*(a+b);
k=k+1;
end
fprintf('The minimum value is %f\n',c)
OUTPUT:
3.
CODE:
function lab10_2
i = input('Enter the value of i:');
p0 = input('Enter the initial value:');
N = input('No.of iterations:');
e = input('Enter any smallest value:');
syms 'x'
f(x) = 2*x^2 + 16/x
df = diff(f)
ddf = diff(df)
while i <= N
p = p0 - (df(p0)/ddf(p0));
if (abs(p - p0)/abs(p)) < e
fprintf('Solution is %f and obtained in %d itertations\n',
double(p),i)
return
end
i = i + 1;
p0 = p;
end
end
OUTPUT: