【问题标题】:Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power错误使用 ^ 一个参数必须是方阵,另一个必须是标量。使用 POWER (.^) 获取元素幂
【发布时间】:2017-09-22 13:48:50
【问题描述】:

我正在尝试从这个函数中编写最小化函数,

(4-2.1*x1^2+x1^4/3)*x1^2+x1*x2+(-4+4*x2^2)*x2^2

在哪里, -10

这是我写的,对吗?

min = -10;
max = 10;
x1 = min+max*rand();
x2 = min+max*rand();
T = 1.0;
Tmin = 0.0000001;
refresh = 0.9;
finalMin = (4-2.1*x1^2+x1^4/3)*x1^2+x1*x2+(-4+4*x2^2)*x2^2;
finalCoordinate=[x1,x2];
while(T>Tmin)
    i=0;
    while(i<=300)
        x1= min+max*rand(1,1);
        x2= min+max*rand(1,1);
        newMin = (4-2.1*x1^2+x1^4/3)*x1^2+x1*x2+(-4+4*x2^2)*x2^2;
        delta1 = finalMin-newMin;
        if(newMin < finalMin)
            finalMin=newMin;
            finalCoordinate=[x1,x2];
        elseif(2.71828^(delta/T))>(0+(rand(1)*1))
            finalCoordinate=[x1,x2];
        end;
        i=i+1;
    end;
    T = T*refresh;
end;

但是为什么,在

elseif(2.71828^(delta/T))>(0+(rand(1)*1))




Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power.

以及如何从该坐标获得 100% 的准确度?

【问题讨论】:

  • 据我所知,您实际上并没有在任何地方定义delta。您确实定义了类似名称的delta1
  • 不要将 HTML sn-ps 用于非 HTML 的内容。所做的只是生成各种按钮和其他无论如何都不起作用的无意义的屏幕填充。

标签: matlab matlab-figure minimization simulated-annealing


【解决方案1】:

嗯,根据错误消息,delta 可能不是标量。您没有在此处的代码中定义它。

一般来说,您的问题是非线性约束优化问题,可以使用优化工具箱中的fmincon 快速解决:

fun = @(x) (4-2.1*x(1)^2+x(1)^4/3)*x(1)^2+x(1)*x(2)+(-4+4*x(2)^2)*x(2)^2;
x0 = rand(2,1)*20-10;
[x,fval] = fmincon(fun, x0, [],[], [],[], [-10;-10], [10;10])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2020-05-10
    • 2020-11-29
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多