Help my code won't run for finding the minimum and maximum of a function
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
Matt J
il 20 Set 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)
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
