Discussion:
penalty function code
(too old to reply)
Ma Dely Esberto
2017-11-05 10:36:07 UTC
Permalink
Raw Message
I was doing this optimization using interior penalty function code with the objective function
f=(1/3).*(x1+1).^3+x2. the 1st barrier is g1(x1,x2)=(1-x1), and the second is: g2(x1,x2)=-x2.
after running the code, the screen displayed:
step r x1 x2 f(r)
---- ------ --------- ---------- ----------
0 0.10 Error using fprintf
Unable to convert 'sym' value to 'double'.

Error in mdpe_penaltyfunc2 (line 75)
fprintf('%3i %8.2f %12.6f %12.6f\n',count,r,x1,x2)
I don't know to fix this. I am newbie to Matlab. Here's the code:

function tab = mdpe_penaltyfunc2()
clc;
clear all;

syms x1 x2 X r

% objective function
f = input('Please enter the objective function: = ','s');
ff=eval(f);

% barriers
g1(x1,x2)=(1-x1); % penalty barrier
g2(x1,x2)=-x2; % penalty barrier
gj(X)=(g1(x1,x2)+g2(x1,x2));

% General equation for penalty function
funv_1=inv(g1(x1,x2));
funv_2=inv(g2(x1,x2));
fmin=(ff-r.*(funv_1+funv_2));


% f = @(x1,x2) (1/3).*(x1+1).^3+x2;
% f2 = @(X)f(x(1),x(2));

% initialization parameters
dx=1;
tol=0.000001;
count=0;
r=0.1;
err=0;
fprintf('step r x1 x2 f(r)\n')
fprintf('---- ------ --------- ---------- ----------\n')


while (err > tol)
count=count+1;

for j=1:2
gj(X)<=0;
r>=0;
k=1;

x1=x1+k;
x2=x2+k;

func = eval('f');
end

for r=1:1000
r=double(r+1*10);
x1=double(((r.^0.5)+1).^0.5);
x2=double((r).^0.5);
funch=eval('f');
fmin=double((ff-r.*(funv_1+funv_2)));
fminh=double(fmin);
ffh=double(funch);
err=abs(fminh-ffh)/0.00001;

end

end

% last step

% ffh = double(eval('f')); % determine new value of f(x)



if (count >200)
error('Slowly Converges. Change initial value.');
end
x1=x1;
x2=x2;
fprintf('%3i %8.2f %12.6f %12.6f\n',count,r,x1,x2)
%fopt = eval('f')

end
% sample problem
% f=(1/3).*(x1+1).^3+x2
Karan Gill
2017-11-06 13:11:08 UTC
Permalink
Raw Message
Post by Ma Dely Esberto
I was doing this optimization using interior penalty function code with the objective function
f=(1/3).*(x1+1).^3+x2. the 1st barrier is g1(x1,x2)=(1-x1), and the second is: g2(x1,x2)=-x2.
step r x1 x2 f(r)
---- ------ --------- ---------- ----------
0 0.10 Error using fprintf
Unable to convert 'sym' value to 'double'.
<SNIP>

The error message tells you the function where the error occurred. Here the function is "fprintf". The error says you cannot convert "sym" to "double". This means you have provide the "fprintf" function wtih symbolic values (sym) but "fprintf" needs "doubles". So you need to provide "fprintf" with an input it understands.

Convert "double" to "sym" by using the "double" function. See: https://www.mathworks.com/help/symbolic/double.html
Then refer to the "fprintf" help page for examples: https://www.mathworks.com/help/matlab/ref/fprintf.html
Loading...