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?

采纳的回答

Mischa Kim
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 个评论
cakey
cakey 2014-9-20
编辑:cakey 2014-9-20
Wow, i think your suggestions worked! I don't really understand why the syntax I used was wrong. I understand that my quadratic function was incomplete. But the second part about crit_pt is confusing to me. Thank you. More specifically, i would like to know why CP = quadratic(3 * c, 2 * d, e); crit_pt1 = CP(1); crit_pt2 = CP(2); needs the CP= stuff.
Mischa Kim
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
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.
  1. Instead of x = a:delta:b, do instead x=linspace(a,b,101)
  2. Instead of p = @(x) c * x.^3 + d * x.^2 + e * x + f do polyval([c d e f],x)

类别

Help CenterFile Exchange 中查找有关 Signal Generation and Preprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by