在 Matlab 中使用循环从表达式中查找最大值

Finding the maximum value from an expression using a loop in Matlab

x 介于 01 之间时,我想使用表达式的二阶导数求最大值。换句话说,我对 cox(x^2) 求导两次,得到二阶导数 - 2*sin(x^2) - 4*x^2*cos(x^2),然后我想计算 x = 0x = 1 的二阶导数,然后显示填充值的 maximum 值。

我有:

syms x
f = cos(x^2);
secondD = diff(diff(f));

for i = 0:1
y = max(secondD(i))
end

有人可以帮忙吗?

您可以通过 subsdouble 轻松完成:

syms x
f = cos(x^2);
secondD = diff(diff(f));

% instead of the for loop
epsilon = 0.01;
specified_range = 0:epsilon:1;
[max_val, max_ind] = max(double(subs(secondD, specified_range)));

请注意,这是一种求最大值的数值方法,返回的答案并不总是完全正确。但是,通过增加 epsilon,您通常可以获得更好的结果(同样在某些情况下它并不完全正确)。