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

 채택된 답변

Star Strider
Star Strider 2014년 10월 4일

0 개 추천

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

Jorge
Jorge 2014년 10월 4일
편집: Star Strider 2014년 10월 4일
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 2014년 10월 4일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2014년 10월 4일

댓글:

2014년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by