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

1 次查看(过去 30 天)
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));

采纳的回答

Walter Roberson
Walter Roberson 2022-11-9
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by