[英]Finding the maximum value from an expression using a loop in Matlab
I want to find the maximum value using the second derivative of the the expression when x
is between 0
and 1
.当
x
介于0
和1
之间时,我想使用表达式的二阶导数找到最大值。 In other words I am taking the derivative of cox(x^2)
twice to get the second derivative resulting in - 2*sin(x^2) - 4*x^2*cos(x^2)
, then I want to evaluate this second derivative at x = 0
to x = 1
, and display the maximum
value of the populated values.换句话说,我两次取
cox(x^2)
的导数得到二阶导数,结果是- 2*sin(x^2) - 4*x^2*cos(x^2)
,然后我想在x = 0
到x = 1
处计算这个二阶导数,并显示填充值的maximum
。
I have:我有:
syms x
f = cos(x^2);
secondD = diff(diff(f));
for i = 0:1
y = max(secondD(i))
end
Can someone help?有人可以帮忙吗?
You can do it easily by subs
and double
:您可以通过
subs
和double
轻松完成:
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)));
Please note that it is a numerical approach to find the maximum and the returned answer is not completely correct all the time.请注意,这是一种寻找最大值的数值方法,并且返回的答案并非始终完全正确。 However, by increasing the
epsilon
, you can expect a better result in general (again in some cases it is not completely correct).但是,通过增加
epsilon
,您通常可以期待更好的结果(同样在某些情况下它并不完全正确)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.