line search minimization problem, error sym is not convertible to double
Show older comments
I am trying to write a code to minimize a function. it worked well for minimizing the fucntion f=1+(X1^2)+2*(X2^2). Now I replace that function with f = 316.2*(X1^0.5) + 34.3*X2 + (10^8)*(X1^(-0.5))*(1/X2).
Now I get this error below
Unable to perform assignment because value of type 'sym' is not convertible to 'double'. Error in untitled2 (line 39)
A11(i) = A1
Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to
substitute values for variables.
Below is my code
clear
clc
syms X1
syms X2
%f = 1+(X1^2)+2*(X2^2)
f = 316.2*(X1^0.5) + 34.3*X2 + (10^8)*(X1^(-0.5))*(1/X2) %symbolic expression
A1 = 1
A2 = 1
t = 0
X1g = 1
X2g = 2
%f1g=1+(X1g^2)+2*(X2g^2)
f1g= 316.2*(X1g^0.5) + 34.3*X2g + (10^8)*(X1g^(-0.5))*(1/X2g)
while ((A1^2)+(A2^2))>=0.0000001 && t<3
t=t+1
A=diff(f,X1)
B=diff(f,X2)
%A1 and A2 are gradient with repect to X1g and X2g
A1=subs(A,X1,X1g)
A2=subs(B,X2,X2g)
%fg=1+(X1g^2)+2*(X2g^2)
%making vector of scalars
a=0:.1:5;
b=length(a);
alpha = zeros(1,b)
%making vectors of zeros
A11 = zeros(1,b)
A22= zeros(1,b)
X11g= zeros(1,b)
X22g= zeros(1,b)
X1new= zeros(1,b)
X2new= zeros(1,b)
f1new= zeros(1,b)
for i=1:b
alpha(i) = a(1,i)
A11(i) = A1
A22(i) = A2
X11g(i) = X1g
X22g(i) = X2g
%calculating new X1 and X2
X1new(i) = X11g(i)-(alpha(i))*(A11(i))
X2new(i) = X22g(i)-(alpha(i))*(A22(i))
%f1new(i) = 1+(X1new(i)^2)+2*(X2new(i)^2)
f1new(i)= 316.2*(X1new(i)^0.5) + 34.3*X2new(i) + (10^8)*(X1new(i)^(-0.5))*(1/X2new(i))
end
[minf1new, Colnumf1new] = min(f1new)
a = Colnumf1new
X1g = X1new(1,a)
X2g = X2new(1,a)
%f1new = 1+(X1g^2)+2*(X2g^2)
f1new= 316.2*(X1g^0.5) + 34.3*X2g + (10^8)*(X1g^(-0.5))*(1/X2g)
A1
A2
E=(A1^2)+(A2^2)
end
Z= zeros(1,8)
fprintf('norm of gradient is %4.9f \n',E);
fprintf('min fuction value is %4.9f \n',f1new);
fprintf('Value of X1 is %4.9f \n',X1g);
fprintf('Value of X2 is %4.9f \n',X2g);
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time 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!