please review matlab code and tell me why i receive errors
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
function [x_max, J_max] = maxperf(p, q)
% Define the polynomials p(x) and q(x)
P = polyval(p, x);
Q = polyval(q, x);
% Compute the objective function J(x)
J = P^2*Q;
% Find the roots of the derivative of J(x)
x_roots = roots(polyder(J));
% Filter out roots that are not finite and real
x_roots = x_roots(isfinite(x_roots) & isreal(x_roots));
% Evaluate J(x) at each root
J_vals = polyval(J, x_roots);
% Find the index of the root that produces the maximum J(x)
[J_max, index] = max(J_vals);
% Find the corresponding x that maximizes J(x)
x_max = x_roots(index);
% Display the result
disp(['The maximum value of x is: ', num2str(x_max)]);
disp(['The corresponding maximal value of J is: ', num2str(J_max)]);
end
i defined functions p and q and still get an error in the code
0 commentaires
Réponses (1)
Image Analyst
le 21 Juil 2023
p and q are not functions. They are input arguments. What did you assign for them, and how did you call maxperf()? For example did you do
p = polyfit(x1, y1, 2);
q = polyfit(x2, y2, 2);
[x_max, J_max] = maxperf(p, q)
And since you use x inside maxperf() it needs to be assigned. Please show us the missing code where you assigned x, either by passing in x, or assigning x to some array in maxperf.
0 commentaires
Voir également
Catégories
En savoir plus sur Scope Variables and Generate Names dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!