필터 지우기
필터 지우기

please review matlab code and tell me why i receive errors

조회 수: 1 (최근 30일)
lateef
lateef 2023년 7월 21일
답변: Image Analyst 2023년 7월 21일
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

답변 (1개)

Image Analyst
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.

카테고리

Help CenterFile 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!

Translated by