PS =
Problems by calculating zero points of a cubic function
이전 댓글 표시
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
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]
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]
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)
format longg
roots(p)
vpasolve(PS)
[The output of roots and vpasolve are in different orders, but they contain basically the same elements.]
Tim
2024년 8월 5일
채택된 답변
추가 답변 (1개)
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)
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))
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
2024년 8월 5일
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.
카테고리
도움말 센터 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



