Problems by calculating zero points of a cubic function

조회 수: 6 (최근 30일)
Tim
Tim 2024년 8월 5일
댓글: Tim 2024년 8월 5일
Hey there,
I have the following problem: let´s say I want to calculate the zero points of the cubic function:
f(x) = -2 x^3 + x^2 + 0.5 x - 8
I already know the respective solution:
p = roots([-2 1 0.5 -8])
The answer p is a vector with all three possible solutions.
Now my problem:
I would like to vary the third coefficient - that is 0.5 - by several values 0.1, 0.2, ... , 0.5
That would be like
x=0.1:0.1:0.5;
p = roots([-2 1 x -8])
The problem is that the respective solutions are wrong.
What is my mistake?
How should I do it instead?
Thx a lot in advance!
Best regards,
Tim
  댓글 수: 2
Steven Lord
Steven Lord 2024년 8월 5일
Others have told you how to get what you wanted but they didn't say why what you tried didn't work. Let's look at the polynomial whose roots you tried to compute.
x=0.1:0.1:0.5;
p = [-2 1 x -8]
p = 1x8
-2.0000 1.0000 0.1000 0.2000 0.3000 0.4000 0.5000 -8.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The roots function is not vectorized to operate on multiple polynomials at once, and even if it did the code you wrote wouldn't create that array of polynomials. Instead of creating 5 polynomials:
O = ones(size(x.'));
p5 = [-2*O, 1*O, x.', -8*O]
p5 = 5x4
-2.0000 1.0000 0.1000 -8.0000 -2.0000 1.0000 0.2000 -8.0000 -2.0000 1.0000 0.3000 -8.0000 -2.0000 1.0000 0.4000 -8.0000 -2.0000 1.0000 0.5000 -8.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
what you wrote creates a different polynomial. The solutions you received were right but for a different polynomial than you were interested in.
PS = poly2sym(p)
PS = 
format longg
roots(p)
ans =
1.18464993681455 + 0.465306240585768i 1.18464993681455 - 0.465306240585768i 0.348063520833424 + 1.18091540917693i 0.348063520833424 - 1.18091540917693i -0.69984674988818 + 0.952746166367853i -0.69984674988818 - 0.952746166367853i -1.1657334155196 + 0i
vpasolve(PS)
ans = 
[The output of roots and vpasolve are in different orders, but they contain basically the same elements.]
Tim
Tim 2024년 8월 5일
Wow, thank you very much, Steven. That makes absolutely sense!!!
Now I learned a lot... thank you, guys!

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2024년 8월 5일
x=0.1:0.1:0.5;
p = arrayfun(@(i)roots([-2 1 x(i) -8]),1:numel(x),'UniformOutput',0);
p = cell2mat(p)
p =
0.9732 + 1.3484i 0.9779 + 1.3383i 0.9827 + 1.3282i 0.9874 + 1.3181i 0.9921 + 1.3080i 0.9732 - 1.3484i 0.9779 - 1.3383i 0.9827 - 1.3282i 0.9874 - 1.3181i 0.9921 - 1.3080i -1.4464 + 0.0000i -1.4559 + 0.0000i -1.4653 + 0.0000i -1.4748 + 0.0000i -1.4842 + 0.0000i

추가 답변 (1개)

John D'Errico
John D'Errico 2024년 8월 5일
편집: John D'Errico 2024년 8월 5일
Or, you can use analytical tools.
syms x a
f = -2*x^3 + x^2 + 0.5*x -8; % Remember to use * to multiply
fa = f + a;
Before you do ANYTHING, plot the basic function.
fplot(f,[-2,2])
grid on
yline(0)
So, when a is zero, the root real found will be near -1.5. You should understand that only when a is between roughly 7 and 8, are there three real roots. For all other values of a, there will be exactly 1 real root.
The value of a adjusts the constant term, and by doing so, it translates the entire curve up or down in y.
faroots = solve(fa,x,'maxdegree',3)
faroots = 
Ugh. That really is pretty messy looking. It is actually the second root you want, which is the real root when a==0.
vpa(subs(faroots(2),a,0))
ans = 
Well, there is a tiny imaginary part, but that is just floating point trash. We can plot the root found, as a function of a now.
fplot(real(faroots(2)),[-5,5])
xlabel 'a'
ylabel root
  댓글 수: 2
Tim
Tim 2024년 8월 5일
wow, very interesting and helpful!!
thax a lot !
John D'Errico
John D'Errico 2024년 8월 5일
편집: John D'Errico 2024년 8월 5일
Funny, in that I recall the time when as a high school student, I asked a similar question of my math teacher in my calc class. Except, I foolishly picked a day when my teacher was absent, so we had only a clueless sub for the day. It totally confused her. Lesson learned. Save the interesting questions for Dr. Krzeminski.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by