1 view (last 30 days)

Show older comments

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)

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)

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)

They are not very useful in that form, so convert them to floating point values

groots = vpa(groots)

You can see tiny imaginary parts, but they are irrelevant. They are just floaitng point trash. Discard the imaginary parts.

groots = real(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))

gmax = max(localmaxima)

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.

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.

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

or, if you mean the maximum absolute value, use

disp(max(abs(gx(X))))

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

Start Hunting!