How to solve "Parse error at METHODS: usage might be invalid MATLAB syntax."?
4 views (last 30 days)
Show older comments
%% Apply Penalty Method( equality constraints)
alpha_range = [(0:0.1:0.5) 1 3 5];
for counter=1:lenght(alpha_range)
alpha=alpha_range(counter);
%capture the current value of alpha in an anonymous function
anonymousFunctiononHandle=...
@x cost_function_augumented_equality_only(x,alpha) ;
%solve unconstrained optimization problem using fminsearch
x0 = zeros(2,1);
[xhatstar,f0]= fminsearch(anonymousFuctionHandle,x0,...
optimset('To1x',1e-10,'MaxfunEvals',10000,'MaxIter',10000));
%plot optimal solution for the current value of alpha
plot3(xhatstar(1),xhatstar(2),0,'m+','Linewidth',2,'Markersize',15)
title(['\alpha',num2star(alpha)])
x(-8,8)
pause(1)
end
and here my fuction
function [f0_hat]= cost_function_augumented_equality_only(x,alpha)
H = [1 1
1 2];
g = [1 2];
r = 45;
f0 = 0.5*x'*H*x + g'*x +r;
f1 = x(1)+x(2)-4;
f0_hat= f0+alpha*f1^2;
end
0 Comments
Answers (1)
Steven Lord
on 18 Jun 2021
anonymousFunctiononHandle=...
@x cost_function_augumented_equality_only(x,alpha) ;
This isn't valid MATLAB syntax. You're missing the parentheses around the input argument.
anonymousFunctiononHandle=...
@(x) cost_function_augumented_equality_only(x,alpha) ;
You also appear to have a couple other issues in your code, including several typos.
optimset('To1x',1e-10,'MaxfunEvals',10000,'MaxIter',10000));
You've used a 1 instead of a lower-case L in the first option's name.
%plot optimal solution for the current value of alpha
plot3(xhatstar(1),xhatstar(2),0,'m+','Linewidth',2,'Markersize',15)
title(['\alpha',num2star(alpha)])
x(-8,8)
There's no x variable or function defined in the code segment you posted here.
0 Comments
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!