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

조회 수: 77 (최근 30일)
Ria Singh
Ria Singh 2021년 7월 18일
댓글: Rik 2021년 7월 23일
I'm trying to find the max and min of a function over a function, but I can't seem to figure out how. My equation is y = (1*x^4)/4+(4*x^3)/3- 5*(x^2)/2 over -3<=x<=3. I tried doing min(y) and max(y) but it's not working. Does anybody know how to find the max and min???
  댓글 수: 2
Image Analyst
Image Analyst 2021년 7월 23일
Other than editing away most of your question, what else have you done? Did you like any of the Answers below?
Rik
Rik 2021년 7월 23일
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?
I'm trying to find the max and min of a function over a function, but I can't seem to figure out how. My equation is y = (1*x^4)/4+(4*x^3)/3- 5*(x^2)/2 over -3<=x<=3. I tried doing min(y) and max(y) but it's not working. Does anybody know how to find the max and min???

댓글을 달려면 로그인하십시오.

답변 (3개)

Rik
Rik 2021년 7월 18일
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)
x_min = -2.9999
Let's confirm this with a plot:
fplot(y,[-3 3])

Image Analyst
Image Analyst 2021년 7월 18일
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
Walter Roberson 2021년 7월 19일
syms x
y = (1*x.^4)/4+(4*x.^3)/3- 5*(x.^2)/2
y = 
LB = -3; UB = 3;
xcrit = solve(diff(y, x),x)
xcrit = 
xcrit(xcrit < LB | xcrit > UB) = [];
xcrit = unique([xcrit; LB; UB])
xcrit = 
ycrit = subs(y,x,xcrit)
ycrit = 
[miny, minidx] = min(ycrit)
miny = 
minidx = 1
[maxy, maxidx] = max(ycrit)
maxy = 
maxidx = 4
fprintf('minimum is %g at %g\n', miny, xcrit(minidx))
minimum is -38.25 at -3
fprintf('maximum is %g at %g\n', maxy, xcrit(maxidx))
maximum is 33.75 at 3

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by