0

I have a function which is f=x^4-8x^3+24^2-32x.

I need to find the turning points (i.e. the maximum or minimum of the curve) using the inbuilt command 'fsolve'.

Now, I know that the turning point is x=2 because I worked it out manually by finding the first derivative of f and making it equal to zero, but when I tried doing it on matlab using fsolve I didn't manage it. I did this:

x=sym('x'); 
f=x^4-8*x^3+24*x^2-32*x;
f1=diff(f,x,1) 

and I don't know how to continue from there (my approach is most probably wrong as I've been viewing the problem from a mathematical point of view)

Anyone know how I can write the code please?

2 Answers2

1

fsolve is for solving an equation numerically. So you first need to create a matlab function from the symbolic expression:

syms x
f=x^4-8*x^3+24*x^2-32*x;
f1=matlabFunction(diff(f,x,1))
result = fsolve(f1, 0)
flawr
  • 10,814
  • 3
  • 41
  • 71
1

Your equation seems to be almost flat near x=2. So fsolve can do the job, but the precision won't be great.

Luckily, finding the roots (all the roots !) of a polynomial is something that we can do mathematically:

sym x
% Get the coefficient of the polynome
c      = sym2poly(diff(x^4-8*x^3+24*x^2-32*x))
% Create the Frobenius companion matrix
l        = length(c);
A        = diag(ones(l-2,1),-1)
A(:,end) = -c(l:-1:2)./c(1)
% Get the roots
roots  = eig(A)

% roots =
% 
%    2.0000 + 0.0000i
%    2.0000 - 0.0000i
%    2.0000 +      0i

Why does it works ? Check the wikipedia article about companion matrix

obchardon
  • 10,614
  • 1
  • 17
  • 33
  • So, this is an alternative to the fsolve? – amateurcoder Mar 01 '21 at 16:49
  • For numeric solutions to polynomial roots, just use the MATLAB roots( ) function, which uses the eig( ) technique in the background. No need to manually create the companion matrix and call eig( ) since roots( ) already does this for you. – James Tursa Mar 04 '21 at 18:14
  • @JamesTursa, ho ! you're right ! I had forgotten that this function already existed, thank you. – obchardon Mar 05 '21 at 08:18