Azzera filtri
Azzera filtri

line search minimization problem, error sym is not convertible to double

3 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

Walter Roberson
Walter Roberson il 18 Feb 2024
f = 316.2*(X1^0.5) + 34.3*X2 + (10^8)*(X1^(-0.5))*(1/X2) %symbolic expression
%...
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)
f is a function of sqrt(X1)/X2 . The derivative of f with respect to X1 or X2 involves the other variable. Substituting for one of the variables leaves the other in the expression. Therefore A1 and A2 are going to be symbolic expressions involving X2 and X1.
A11(i) = A1
A22(i) = A2
A11 and A22 are defined as numeric zero. You cannot asssign a symbolic expression involving unresolved variables to a numeric location.
Probable solution:
A1 = double(subs(A, [X1, X2], [X1g, X2g]));
A2 = double(subs(B, [X1, X2], [X1g, X2g]));

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by