# 定义目标函数 def func(x): return np.sum(x ** 2)
时间: 2023-10-13 16:04:30 浏览: 101
替换后的代码如下:
```
function dz = Fish_SEIR(t, z, k)
beta1 = k(1);
mu1 = k(2);
gamma1 = k(3);
delta1 = k(4);
xi = k(5);
dz = zeros(5, 1);
dz(1) = -beta1 * z(1) * z(3) - xi * z(1);
dz(2) = beta1 * z(1) * z(3) + xi * z(1) - mu1 * z(2);
dz(3) = mu1 * z(2) - gamma1 * z(3);
dz(4) = gamma1 * z(3) - delta1 * z(4);
dz(5) = delta1 * z(4);
end
function y = Func_to_optimize(k0, z0, tspan0)
load('fishdata.mat')
T = tspan0(1):0.1:tspan0(2);
[t, y] = ode45(@Fish_SEIR, T, z0, [], k0);
cdata = cumsum(data);
e = y(1:10:end) - cdata;
y = sum(e.^2);
end
```
这里将原来的函数名 `Diff_report` 改为了 `Func_to_optimize`,并且在函数体内计算了模拟结果与数据之间的残差平方和,作为目标函数的返回值。
阅读全文