Help my code won't run for finding the minimum and maximum of a function
1 view (last 30 days)
Show older comments
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 Comments
Matt J
on 20 Sep 2014
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.
Accepted Answer
Mischa Kim
on 20 Sep 2014
Edited: Mischa Kim
on 20 Sep 2014
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 Comments
Mischa Kim
on 20 Sep 2014
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.
More Answers (1)
Matt J
on 20 Sep 2014
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 Comments
See Also
Categories
Find more on Signal Generation and Preprocessing 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!