Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: penalty function code
Replies: 1   Last Post: Nov 6, 2017 8:11 AM

 Messages: [ Previous | Next ]
 Ma Dely Esberto Posts: 1 Registered: 11/5/17
penalty function code
Posted: Nov 5, 2017 5:36 AM

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

Date Subject Author
11/5/17 Ma Dely Esberto
11/6/17 Karan Gill