Help my code won't run for finding the minimum and maximum of a function
조회 수: 1 (최근 30일)
이전 댓글 표시
The function is p(x) and when you take the derivative, I get the quadratic formula and I'm trying to use it to find critical points. the quadratic I have is a saved code(not in this code I saved it as a function):
%The quadratic formula as a function
function X=quadratic(A,B,C)
crit_pt1=(-B+sqrt(B^2-4*A*C))/(2*A)
crit_pt2=(-B-sqrt(B^2-4*A*C))/(2*A)
This is where my program starts:
a = -1.0;
b = 2.0;
c = 1.0;
d = 1.0;
e = 1.0;
f = 1.0;
p = @(x) c * x.^3 + d * x.^2 + e * x + f;
quadratic(3 * c, 2 * d, e);
if isreal(crit_pt1)
disp('Crit Point 1:');
disp(crit_pt1);
disp('p(Crit Point 1):');
disp(p(crit_pt1));
disp('Crit Point 2:');
disp(crit_pt2);
disp('p(Crit Point 2):');
disp(p(crit_pt2));
else
disp('The critical points are complex.');
end
disp('Left Endpoint:');
disp(a);
disp('p(Left Endpoint):');
disp(p(a));
disp('Right Endpoint:');
disp(b);
disp('p(Right Endpoint):');
disp(p(b));
delta = (b - a) / 100;
x = a:delta:b;
y = p(x);
plot(x, y)
It says error to many outputs for quadratic and also that critpt_1 and crit_pt_2 not defined even though I have it defined in the saved function for quadratic. Any help?
댓글 수: 2
Matt J
2014년 9월 20일
Notice how your code is now in a more readable font, distinct from your text. I did that with this button,
and hope you will do the same from now on.
채택된 답변
Mischa Kim
2014년 9월 20일
편집: Mischa Kim
2014년 9월 20일
Cakey, critpt_1 and crit_pt_2 are only defined locally, in function quadratic. Also, the function does not seem to be complete, since there are no values assigned to X. Update the function to
function X = quadratic(A,B,C)
crit_pt1 = (-B+sqrt(B^2-4*A*C))/(2*A);
crit_pt2 = (-B-sqrt(B^2-4*A*C))/(2*A);
X = [crit_pt1; crit_pt2];
end
and use in the calling function or script something like
CP = quadratic(3 * c, 2 * d, e);
crit_pt1 = CP(1);
crit_pt2 = CP(2);
That way the critical points are assigned within quadratic to the variable X. In the calling function you can then access the values for the critical points.
댓글 수: 2
Mischa Kim
2014년 9월 20일
Think of the function quadratic as a sort of a black box (from the perspective of the calling function). The only thing the calling function knows is that when quadratic is called with three input arguments, it returns a 2-by-1 vector whose first ( CP(1) ) and second ( CP(2) ) elements are crit_pt1 and crit_pt2, respectively.
추가 답변 (1개)
Matt J
2014년 9월 20일
I assume you really meant this,
function [crit_pt1, crit_pt2]=quadratic(A,B,C)
crit_pt1=(-B+sqrt(B^2-4*A*C))/(2*A)
crit_pt2=(-B-sqrt(B^2-4*A*C))/(2*A)
and later
[crit_pt1, crit_pt2]=quadratic(3 * c, 2 * d, e)
A few more miscellaneous tips.
- Instead of x = a:delta:b, do instead x=linspace(a,b,101)
- Instead of p = @(x) c * x.^3 + d * x.^2 + e * x + f do polyval([c d e f],x)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Signal Generation and Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!