please review matlab code and tell me why i receive errors
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Image Analyst
2023-7-21
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!