Maximal values of polynomial in Matlab
26 views (last 30 days)
Show older comments
Given J(x)=(p(x)^2)/q(x) where both p and q are polynomials defined in the workspace by horizontal coefficient arrays, and q(x) > 0 for all values of x.
Create a Matlab script that will compute and display the real and finite value of x for which J(x) is maximized, and also display the maximal value of J achieved at this x value (using only numerical calculations and not symbolic Matlab calculations).
The script should find the maxima using dJ(x)/dx=0 .
I am fairly new working with Matlab and I am unsure on how to approach this problem. I have tried:
P=conv(p,p);
Q=q;
[a,b]=deconv(P,Q) %J(x)
[c,d]=deconv((conv(Q,polyder(P))+conv(P,polyder(Q))),conv(Q,Q)) %dJ(x)/dx
r=roots([c,d])
J=max(abs(r))
But I am not getting the correct answer. For p(x)=2x+5 (i.e. [2 5]) and q(x)=x^4+x^2+2x+2 (i.e. [1 0 1 2 2]), the maximal of J should be approximately 12.9 occurring at x=-0.27. Your help is greatly appreciated.
0 Comments
Answers (1)
David Hill
on 22 Jul 2022
p=[2 5];
q=[1 0 1 2 2];
x=-1:.0001:1;
J=@(x)polyval(p,x).^2./polyval(q,x);
[~,idx]=min(abs(diff(J(x))));
Xmax=x(idx);
Jmax=J(Xmax);
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!