# evaluate

## 语法

``val = evaluate(expr,pt)``
``val = evaluate(prob,pts)``

## 说明

` `val = evaluate(expr,pt)` 返回在值 `pt` 上的优化表达式 `expr` 的值。`

``val = evaluate(prob,pts)` 返回 `prob` 中的目标函数和约束函数在 `pts` 中的点处的值。`

## 示例

```x = optimvar("x",3,2); y = optimvar("y",1,2); expr = sum(x,1) - 2*y;```

```xmat = [3,-1; 0,1; 2,6]; sol.x = xmat; sol.y = [4,-3]; val = evaluate(expr,sol)```
```val = 1×2 -3 12 ```

```x = optimvar('x'); y = optimvar('y'); prob = optimproblem; prob.Objective = -x -y/3; prob.Constraints.cons1 = x + y <= 2; prob.Constraints.cons2 = x + y/4 <= 1; prob.Constraints.cons3 = x - y <= 2; prob.Constraints.cons4 = x/4 + y >= -1; prob.Constraints.cons5 = x + y >= 1; prob.Constraints.cons6 = -x + y <= 2; sol = solve(prob)```
```Solving problem using linprog. Optimal solution found. ```
```sol = struct with fields: x: 0.6667 y: 1.3333 ```

`val = evaluate(prob.Objective,sol)`
```val = -1.1111 ```

```x = optimvar("x"); y = optimvar("y"); obj = (10*(y - x^2))^2 + (1 - x)^2; cons1 = x^2 + y^2 <= 1; cons2 = x + y >= 0; cons3 = y <= sin(x); cons4 = 2*x + 3*y <= 2.5; prob = optimproblem(Objective=obj); prob.Constraints.cons1 = cons1; prob.Constraints.cons2 = cons2; prob.Constraints.cons3 = cons3; prob.Constraints.cons4 = cons4;```

```rng default % For reproducibility xvals = randn(1,100); yvals = randn(1,100);```

`pts = optimvalues(prob,x=xvals,y=yvals);`

`val = evaluate(prob,pts);`

```figure plot3(xvals,yvals,log(1 + val.Objective),"bo")```

```neg1 = val.cons1 <= 0; pos1 = val.cons1 > 0; neg4 = val.cons4 <= 0; pos4 = val.cons4 > 0; figure plot3(xvals(neg1),yvals(neg1),val.cons1(neg1),"bo") hold on plot3(xvals(pos1),yvals(pos1),val.cons1(pos1),"rx") plot3(xvals(neg4),yvals(neg4),val.cons4(neg4),"ko") plot3(xvals(pos4),yvals(pos4),val.cons4(pos4),"gx") hold off```

## 输入参数

• `expr` 中的所有变量都必须与 `pt` 中的字段名称匹配。

• 与字段名称匹配的值必须为数值。

• `pt` 中字段的大小必须与 `expr` 中对应变量的大小匹配。

• `pts` 中的字段名称必须与 `prob` 中的目标和约束表达式中的对应变量名称匹配。

• `pts` 中的值必须是与 `prob` 中对应变量大小相同的数值数组。

## 详细信息

### 约束表达式值

• 如果约束为 `L <= R`，则约束值为 `evaluate(L,pt)` - `evaluate(R,pt)`

• 如果约束为 `L >= R`，则约束值为 `evaluate(R,pt)` - `evaluate(L,pt)`

• 如果约束为 `L == R`，则约束值为 `abs(evaluate(L,pt) – evaluate(R,pt))`