Getting infinite looping and NaN error ( find the minimum of a function using Quadratic Approximation method )
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey!
Im trying to find the minimum of the function using quadratic approximation method. But here Im getting NaN error in my matrices. I cannot find why. As well as there is something wrong with loop.
thanks.
clc
clear
f = @(x) exp((x.^4 + x.^2 -x + sqrt(5))./5) + sinh((x.^3 + 21.*x + 9)./(21.*x + 6)) -3 ;
%QUADRATIC APPROXIMATION METHOD
a = 0;
b = 1;
e = 0.01; % ε=0.0001 % ε=0,0000001 % ε=10^-17
x1=a; x2=(a+b)/2; x3=b;
figure; X = [a,b]; Y = [0,0];
plot(X,Y,'red','linewidth',8);
xlabel('intervals of uncertainty');
ylabel('iteration number');
title('ε = 0.01');
hold on;
grid on;
n = 0;
while (b-a>=e)
f1 = f(x1);
f2 = f(x2);
f3 = f(x3);
V = [x1^2,x1,1 ; x2^2,x2,1 ; x3^2,x3,1] ;
B = [f1 ; f2 ; f3] ;
P = linsolve(V,B) ;
xk = -P(2)/(2*P(1));
if xk>= x2
x1 = x2;
x2 = xk;
f1 = f2;
f2 = f(x2);
else
x3 = x2;
x2 = xk;
f3 = f2;
f2 = f(x2);
end
n=n+1;
X = [x1,x3]; Y = Y + n;
plot(X,Y,'red','linewidth',8);
end
xmin = (a+b)./2;
fprintf('The number of iterations = %d\n',n);
fprintf('Number of calculated values = %d\n',n+3);
fprintf('The point of minimum = %.4f\n',(xmin);
fprintf('Minimum of the function = %.4f\n',f((xmin)));
Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!