1 view (last 30 days)

Show older comments

Rik
on 23 Jul 2021

Original post (in case Ria decides to edit it away again):

How do I find the maximum and minimum of a function in a given domain?

Rik
on 18 Jul 2021

You need a function like fminbnd:

y =@(x) (1*x.^4)/4+(4*x.^3)/3- 5*(x.^2)/2;

x_min = fminbnd(y,-3,3)

Let's confirm this with a plot:

fplot(y,[-3 3])

Image Analyst
on 18 Jul 2021

Try this:

x = linspace(-3, 3, 1000);

y = (1*x.^4)/4+(4*x.^3)/3- 5*(x.^2)/2;

plot(x, y, 'b-', 'LineWidth', 2);

grid on;

% Find where min is

[yMin, indexOfMin] = min(y);

fprintf('Min of y at x = %f, y = %f.\n', x(indexOfMin), min(y));

You get

Min of y at x = -3.000000, y = -38.250000.

Is that what you were looking for?

Walter Roberson
on 19 Jul 2021

syms x

y = (1*x.^4)/4+(4*x.^3)/3- 5*(x.^2)/2

LB = -3; UB = 3;

xcrit = solve(diff(y, x),x)

xcrit(xcrit < LB | xcrit > UB) = [];

xcrit = unique([xcrit; LB; UB])

ycrit = subs(y,x,xcrit)

[miny, minidx] = min(ycrit)

[maxy, maxidx] = max(ycrit)

fprintf('minimum is %g at %g\n', miny, xcrit(minidx))

fprintf('maximum is %g at %g\n', maxy, xcrit(maxidx))

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!