Filter löschen
Filter löschen

Syntax error which I cannot find ( find the minimum of a function using Quadratic Approximation Method )

2 Ansichten (letzte 30 Tage)
Hey there!
Im trying to find the minimum of a function using quadratic method and I'm getting some errors which I cannot find.
And maybe there are mistakes in my code also. Hope your help to find out my mistakes.
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;
x1=a; x2=0.5; x3=b;
line=x3 - x1;
figure; X = [a,b]; Y = [0,0];
plot(X,Y,'black','linewidth',4);
hold on;
grid on;
n = 0;
while (line>e)
n=n+1;
f1 = f(x1);
f2 = f(x2);
f3 = f(x3);
p = ( f1*(x2.^2-x3.^2) + f2*(x3.^2-x1.^2) + f3*(x1.^2x2.^2) ) / (2*(f1*(x2-x3)+f2*(x3-x1)+f3*(x1-x2)));
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
if p >= x2
x1 = x2;
x2 = p;
else
x3 = x2;
x2 = p;
end
fprintf('------%.10f\n',x1,x2,x3);
line = x3-x1;
fprintf('line=%.10f\n',line);
X = [x1,x3]; Y = Y +n;
plot(X,Y,'black','linewidth',4);
end
fprintf('The number of iterations is %d\n',n);
fprintf('Number of calculated values is %d\n',n+3);
fprintf('The point of minimum is %.4f\n',(x1+x3)/2);
fprintf('Minimum is %.4f\n',f((x1+x3)/2));

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Nov. 2022
p = ( f1*(x2.^2-x3.^2) + f2*(x3.^2-x1.^2) + f3*(x1.^2x2.^2) ) / (2*(f1*(x2-x3)+f2*(x3-x1)+f3*(x1-x2)));
^^^^^^^^^^
Taking something ^2x2 is not valid syntax. Looking at the other clauses you probably forgot a - and wanted x1.^2-x2.^2

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by