I want to minimize a function, but it gives an error message "Conversion to logical from sym is not possible.". I would like to understand the reason behind it.

3 Ansichten (letzte 30 Tage)
At 1st I define te function and then I find the minimum value. The code is as follows:
function q = Bauer_var(v)
syms a m n b r s phi E D ri ro u;
ri = 50;
ro = 200;
E = 210000;
s = 1;
u = 0.3;
D = (E*s^3)/(12*(1-u^2));
a = v(1);
m = v(2);
n = v(3);
b = v(4);
assume(r >= ri);
assume(r <= ro);
z = exp(a*r)*((r-ri)^2)*((r-ro)^2)*cos(n*(tan(b)*log(ri/r) + phi))/(r^m);
Mkrit = (int(int((D*((diff(z,r,2) + (1/r)*diff(z/r) + (1/r^2)*diff(z,phi,2))^2 + 2*(1-u)*(((1/r)*diff(diff(z,r),phi) - (1/r^2)*diff(z,phi))^2 - diff(z,r,2)*((1/r)*diff(z,r) + (1/r^2)*diff(z,phi,2)))))*2,r,[ri ro]),phi,[0 2*pi]))/((-1)*int(int(((1/r^3)*diff(z,r)*diff(z,phi))*r,r,[ri ro]),phi,[0 2*pi]));
q = Mkrit;
end
After defining the function as above, I minimize it as follows:
v = [10, 10, 5, 10];
var_min = fminsearch(@Bauer_var,v);
a = var_min(1)
m = var_min(2)
n = var_min(3)
b = var_min(4)
The above minimisation should be giving me the minimum values, but it always gives the following error:
{{
Conversion to logical from sym is not possible.
Error in fminsearch (line 327)
if fxr < fv(:,1)
Error in BauerVarExec (line 2)
var_min = fminsearch(@Bauer_var,v);
}}
I would really appreciate any help. And I am new to this, so would like to get an explanation which a beginner can unerstand.

Akzeptierte Antwort

Steven Lord
Steven Lord am 29 Jun. 2021
The fminsearch function requires the function handle that you pass into it as the first input to return a numeric array, not a symbolic array. Convert the symbolic value Mkrit into double immediately before you return it from your function.
Basically what you're doing is returning sqrt2Symbolic. You need to return sqrt2Double instead.
two = sym(2);
sqrt2Symbolic = sqrt(two)
sqrt2Symbolic = 
sqrt2Double = double(sqrt2Symbolic)
sqrt2Double = 1.4142
  20 Kommentare
Walter Roberson
Walter Roberson am 15 Jul. 2021
I created a version for testing, in which I converted to using vpaintegral() at each stage. I invoke it inside of ga() since ga() is able to handle integer constraints while it minimizes.
Unfortunately, the version with vpaintegral() is wretchedly slow. It is able to create the symbolic function in a quite decent time, in terms of a series of vpaintegral() calls, but the resulting function is too slow to get anywhere. I have had it executing for about 3 hours but it has yet to finish a single evaluation.
Yes, it is true that in the previous version, it eventually slowed down to about that speed, but this is the very first evaluation, which only took about 3 minutes in the previous version.
I would firmly recommend that you upgrade to a modern MATLAB; the methods needed for your older release are not feasible.
Sarbhanu Saha
Sarbhanu Saha am 31 Jul. 2021
I have been working on it. And i now found the minimisation values of the function by getting the absolute value of Mkrit by abs(Mkrit). From the help of my PhD guides, I also changed the initial vues for the iteration. Here is the execution:
syms V [1 4]
formula = Bauer_var(V);
fcn(V) = vpa(formula);
fun = @(V) double(fcn(V(1),V(2),V(3)));
options = optimset(@fminsearch);
options = optimset(options, 'Display', 'iter');
V0 = [0.005, 2, 1, pi/4];
[bestv, fval] = fminsearch(fun, V0, options);
a = bestv(1)
m = bestv(2)
n = bestv(3)
b = bestv(4)
Below is the function, which will be minimised:
function q = Bauer_var(v)
tic
syms a m n b r s phi E D ri ro u;
Q = @(v) sym(v);
ri = Q(10);
ro = Q(80);
E = Q(210000);
s = Q(1);
u = Q(3)./10;
D = (E.*s.^3)./(Q(12).*(1-u.^2));
a = v(1);
m = v(2);
n = v(3);
b = v(4);
Pi = sym(pi);
assume(r >= ri);
assume(r <= ro);
z = exp(a.*r).*((r-ri).^2).*((r-ro).^2).*cos(n.*(tan(b).*log(ri./r) + phi))./(r.^m);
dzr = diff(z,r);
dzrr = diff(dzr,r);
dzp = diff(z,phi);
dzpp = diff(dzp,phi);
dzrp = diff(dzr,phi);
q2inside = ((1./r.^3).*dzr.*dzp).*r;
q2inint = vpaintegral(q2inside,phi,[0 2.*Pi]);
q2 = (-1).*vpaintegral(q2inint,r,[ri ro]);
q1inside = (D.*((dzrr + (1./r).*dzr + (1./r.^2).*dzpp).^2 + 2.*(1-u).*(((1./r).*dzrp - (1./r.^2).*dzp).^2 - dzrr.*((1./r).*dzr + (1./r.^2).*dzpp)))).*r;
q1inint = vpaintegral(q1inside,r,[ri ro]);
q1 = Pi.*vpaintegral(q1inint,phi,[0, 2.*Pi]);
Mkrit = q1./q2;
q = abs(Mkrit);
toc
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by