Fil
Fil

Reputation: 471

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. 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.

I have:

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

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

Can someone help?

Upvotes: 0

Views: 143

Answers (1)

OmG
OmG

Reputation: 18838

You can do it easily by subs and 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).

Upvotes: 1

Related Questions