How to know results
1 次查看(过去 30 天)
显示 更早的评论
I have a polynomial and parameters in it,
a=2, c=1, d=2 , g=0.5, k=2 , f=2
e=[2,8,5,4]
b=[1,2,3,4]
p=[ -c.*k+ (f.^2)*(e.^2)/a+2*e.*g, d.*e-(f*b.*e)/a]
roots(p)
I calculate roots of polynomial given parameter values.
But e an b parameters have vector values.
I want to know if it obtains roots for every combination of the e and b values or
it simply gets roots when e=2, b=1 , then b=8,e=2,... ?
采纳的回答
Ameer Hamza
2020-6-2
编辑:Ameer Hamza
2020-6-2
Your current code does not solve the problem, as you described. You need to use for-loop
a=2, c=1, d=2 , g=0.5, k=2 , f=2
e=[2,8,5,4]
b=[1,2,3,4]
r = zeros(numel(e), 1) % polynomial is 1st order so there will be only one root
for i=1:numel(e)
p = [ -c.*k+(f.^2)*(e(i).^2)/a+2*e(i).*g, d.*e(i)-(f*b(i).*e(i))/a]
r(i) = roots(p)
end
5 个评论
Ameer Hamza
2020-6-3
Following code use cross-combination
a=2, c=1, d=2 , g=0.5, k=2 , f=2
e=[2,8,5,4]
b=[1,2,3,4]
[E, B] = ndgrid(e, b);
e = E(:);
b = B(:);
r = zeros(numel(e), 1) % polynomial is 1st order so there will be only one root
for i=1:numel(e)
p = [ -c.*k+(f.^2)*(e(i).^2)/a+2*e(i).*g, d.*e(i)-(f*b(i).*e(i))/a]
r(i) = roots(p)
end
sol = [e, b, r]
1st column of 'sol' is 'e' value, 2nd is 'b' values, and 3rd is the corresponding root.
更多回答(1 个)
Steven Lord
2020-6-2
As written your code solves for the roots of the 7th order polynomial whose coefficients are:
p =
8 134 53 34 2 0 -5 -8
This is:
8*x^7 + 134*x^6 + 53*x^5 + 34*x^4 + 2*x^3 - 5*x - 8
If instead you want to find the roots of the four first order polynomials created by specifying each element of e and b in the expression for p, use a for loop instead.
>> for q = 1:numel(e)
p=[ -c.*k+ (f.^2)*(e(q).^2)/a+2*e(q).*g, d.*e(q)-(f*b(q).*e(q))/a]
end
p =
8 2
p =
134 0
p =
53 -5
p =
34 -8
If you want to find the roots of the sixteen first order polynomials with elements taken from e and from b (and not necessarily the same element of each vector) the easiest way is to use a double for loop.
2 个评论
Steven Lord
2020-6-2
It is not first order as you've written it. Use one or two for loops to construct and solve the four or sixteen first order polynomials or use the explicit formula for the solution of a linear equation.
% if q*x = w then x = w./q
a=2, c=1, d=2 , g=0.5, k=2 , f=2
e=[2,8,5,4]
b=[1,2,3,4]
q = -c.*k+ (f.^2)*(e.^2)/a+2*e.*g;
w = d.*e-(f*b.*e)/a;
x = w./q % Using ./ instead of / because w and q are vectors
check = q.*x - w % Should be all 0's or close to it
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!