why my graph not matching this graph?
1 view (last 30 days)
Show older comments
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')
0 Comments
Accepted Answer
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
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).
More Answers (0)
See Also
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!