why my graph not matching this graph?

1 view (last 30 days)
ok so I'm assigned to match this graph. the problem looks straight forwards, but for some reason max and min are not spitting the right values for the highest point and lowest point of a function. why is this? they are spitting out the highest index and lowest index.
f = @(x) x.^3-6.*x;
x = linspace(-3,3,100);
[f0,i0] = min(f(x))
x0 = x(i0)
[f1,i1] = max(g)
x1 = x(i1)
figure
plot(x,f(x),'b-',x0,f0,'rs',x1,f1,'ro')

Accepted Answer

Star Strider
Star Strider on 4 Oct 2014
Wrong max and min. You’re actually looking for the inflection points:
f = @(x) x.^3-6.*x; % Function
x = linspace(-3,3,100); % Domain
ip = @(x) (f(x+1E-8)-f(x))./1E-8; % Derivative
x0 = fzero(ip,-1); % x0: Inflection Point near x = -1
f0 = f(x0); % f0 = f(x0)
x1 = fzero(ip,+1); % x1: Inflection Point near x = +1
f1 = f(x1); % f1 = f(x1)
figure
plot(x,f(x),'b-',x0,f0,'rs',x1,f1,'ro')
produces:
Which matches quite nicely the plot in your screenshot.
  2 Comments
Jorge
Jorge on 4 Oct 2014
Edited: Star Strider on 4 Oct 2014
is just weird how for this it worked? when i do the 2 graphs separately with intervals of [-3,0] and [0,3]
do you know why?
f = @(x) x.^3-6.*x;
x = linspace(0,3,100);
[f0,i0] = min(f(x));
x0 = x(i0);
figure
plot(x,f(x),'b-',x0,f0,'ro');
legend('f(x) = x^3-6x','(xmin,fmin)')
ylabel('f(x)')
xlabel('x')
clear variables
%%=========================================================================
% problem 3b
f = @(x) x.^3-6.*x;
x = linspace(-3,0,100);
[f1,i1] = max(f(x));
x1 = x(i1);
figure;
plot(x,f(x),'b-',x1,f1,'ro')
ylabel('f(x)')
xlabel('x')
title('maximum of function x^3-6x')
legend('f(x)=x^3-6x','xmax,fmax','location','SE')
Star Strider
Star Strider on 4 Oct 2014
Not weird at all! To do it separately, the min and max approach would work, because for the (-3,0) interval the max would be where you plotted the square, and for (0,3) the min would be where you plotted the circle. Over the full interval, the only way to find the inflection points would be to do the derivative. I did it numerically, but it is not difficult to do it analytically. Analytically, the inflection points are ±sqrt(2).

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!