How to find absolute maximum value of (x*(x-.25)*(x-.5)*(x-1))

17 views (last 30 days)
How could I find the absolute maximum value of g(x) on the interval [0,1]?
My code:
% gx = (x-0) (x-.25) (x - 0.5) (x-1)
nodes = [0 .25 .5 1];
%function of g(x)
gx = @(x) ((x.^4) -(1.75*x.^3)+(0.875*x.^2)-(0.125*x));
% derivative of g(x)
dx_gx = @(x) ((4*x.^3)-(5.25*x.^2)+(1.75*x)-0.125);
val_x = zeros(3,1);
val_x = feval(symengine,'numeric::solve',dx_gx,'AllRealRoots')
val_gx = zeros(3,1);
val_gx = gx(val_x);
abs_val_gx = abs(val_gx)
%abs_gx_val = zeros(3,1);
%abs_gx_val = abs(val_gx)
max_gx = max(abs_val_gx)
%val_x = solve(dx_gx)
It shows the error messege below:
val_x =
[ [x == 0.098186995281737181724488452044525], [x == 0.38272663887880462974987816017091], [x == 0.83158636583945818852563338778456]]
Error using mupadmex
Error in MuPAD command: An arithmetical expression is expected. [abs]
Error in sym/privUnaryOp (line 1699)
Csym = mupadmex(op,args{1}.s,varargin{:});
Error in sym/abs (line 416)
Y = privUnaryOp(X, 'symobj::map', 'abs');
Error in Task_2_b_error_gx (line 16)
abs_val_gx = abs(val_gx)
  1 Comment
Torsten
Torsten on 26 May 2021
Maybe
abs_val_gx = abs(double(val_gx));
instead of
abs_val_gx = abs(val_gx);
?

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 26 May 2021
You are mixing numerical functions with symbolic tools. The two do not mix that well.
So just pick ONE, and stay with it. For example...
syms x
gx = ((x.^4) -(1.75*x.^3)+(0.875*x.^2)-(0.125*x));
dgdx = diff(gx)
dgdx = 
Now we can find the roots of the derivative function. The local maxima of the absolute value will lie at those roots.
groots = solve(dgdx,x,'maxdegree',3)
groots = 
They are not very useful in that form, so convert them to floating point values
groots = vpa(groots)
groots = 
You can see tiny imaginary parts, but they are irrelevant. They are just floaitng point trash. Discard the imaginary parts.
groots = real(groots)
groots = 
Now, evaluate the function at those points, take the absolute value, and find the largest of the local maxima.
localmaxima = abs(subs(gx,x,groots))
localmaxima = 
gmax = max(localmaxima)
gmax = 
0.027008190581118152484802537282069
Is this consistent with what we see in a plot?
gfun = matlabFunction(gx);
fplot(@(x) abs(gfun(x)),[0,1])
yline(double(gmax),'r');
xline(double(groots),'g');
axis([0 1 0 0.03])
I never moved away from symbolic computations until the very end, when I wanted to plot it all.
Could you have solved this using tools like fminbnd? YES. But then you would work entirely in terms of numberical, double precision functions.
  2 Comments
Jahan N
Jahan N on 27 May 2021
Thanks for your effort.I am also grateful to you.
When I run your code ,this shows me the error below:
Error using solve>processString (line 354)
' 3 ' is not a valid expression or equation.
Error in solve>getEqns (line 284)
eqns = processString(eqns, v, vc);
Error in solve (line 160)
[eqns,vars,options] = getEqns(varargin{:});
Error in Maxima_Abs_3 (line 4)
groots = solve(dgdx,x,'maxdegree',3)
It also shows the error:
Undefined function 'max' for input arguments of type 'sym'
Error in Maxima_Abs_3 (line 8)
gmax = max(localmaxima)
That means 'max' function shows undefined for symbolic input argument.
It also shows the error:
Undefined function 'yline' for input arguments of type 'double'.
yline(double(gmax),'r');
How can I resolve the above error?
The other parts of your code are running smoothly.
John D'Errico
John D'Errico on 27 May 2021
Edited: John D'Errico on 27 May 2021
The code I wrote was entirely valid. However, you are using an old release of MATLAB. What release, I have no clue.
Whay can I say this? Because yline was introduced in R2018b. So your release is older than that. The other errors you mention are also consistent with having an old release.
You can probably just use
groots = vpasolve(dgdx,x)
instead of the line I gave.
And the call to yline is only to show a horizontal line in the plot.
Or, you can do as I also suggested, and completely avoid use of symbolic tools. That is your choice.

Sign in to comment.

More Answers (1)

Alan Stevens
Alan Stevens on 26 May 2021
You could just do something like
gx = @(x) ((x.^4) -(1.75*x.^3)+(0.875*x.^2)-(0.125*x));
X = roots([4, -5.25, 1.75, -0.125]);
disp(max(gx(X)))
0.0037
or, if you mean the maximum absolute value, use
disp(max(abs(gx(X))))

Tags

Community Treasure Hunt

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

Start Hunting!