please review matlab code and tell me why i receive errors
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Image Analyst
am 21 Jul. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!