Roots of a 4th degree polynomial and plotting a graph
6 views (last 30 days)
Show older comments
I have a 4th degree polynomial which has a variable, say k for one of its coefficients, how can I find roots (r) for this equation while describing that variable within a range of values
Is it possible to plot a graph using these values of the roots 'r' and the corresponding values of 'k'
0 Comments
Answers (3)
Dyuman Joshi
on 27 Jan 2024
Edited: Dyuman Joshi
on 27 Jan 2024
Define the polynomial as a Function Handle of the variable 'k' and use roots for different values of 'k'.
Yes, you can plot the values of roots but note that if the equation has complex roots, the complex parts will be ignored if you use plot() directly.
In that case, you can plot the real and imaginary values separately.
2 Comments
Torsten
on 27 Jan 2024
In this case, I suggest you first take a 2-hours introductory course free of cost to learn the basics of MATLAB:
Image Analyst
on 27 Jan 2024
Edited: Image Analyst
on 27 Jan 2024
Not for some arbitrary k, unless there is something in the symbolic toolbox you can use. Otherwise put k in with the other coefficients and call roots and then plot for example
k = -5;
% For coefficients [1,2,k,3,-1500]
% y = 1*x.^4 + 2 * x.^3 + k*x.^2 + 3*x.^1 + 1500
coefficients = [1,2,k,3,-1500];
theRoots = roots(coefficients)
x = linspace(-8.3, 7, 512);
y = 1*x.^4 + 2 * x.^3 + k*x.^2 + 3*x.^1 - 1500;
plot(x, y, 'LineWidth', 2)
grid on
xlabel('x');
ylabel('y');
caption = sprintf('Plot for k = %f', k);
title(caption);
yline(0, 'r-') % Red line along y=0 axis.
% Plot any purely real roots as red circles on the y axis.
for k = 1 : numel(theRoots)
if imag(theRoots(k)) == 0
hold on
plot(theRoots(k), 0, 'ro', 'LineWidth', 2)
end
end
0 Comments
John D'Errico
on 27 Jan 2024
Edited: John D'Errico
on 27 Jan 2024
Effectively, you are telling us that one of the coefficients of the polynomial is determined to within some interval. And now you want to characterize how the roots will behave, as a function of that parameter. I might call this a problem of interval arithmetic. But the thing is, those roots will (sometimes) be highly nonlinear functions of that parameter. And sometimes, the roots might be well-behaved, not at all that nonlinear. I would suggest the first case is more likely however, especially for a degree 4 polynomial.
No, there is no simple code to do what you ask. And there is nothing in the symbolic toolbox that can solve the problem. You will also need to deal with things like bifurcations, etc.
What can you do? Well, the simple thing would be to generate the roots for many values of k. Then combine the roots into one array, and call plot, plotting them as a function of k. An issue here is the roots will often change order. They do not get returned from a tool like roots in any meaningful order. And that means your plot will sometimes appear non-sensical. One solution to that is to convert the polynomial function into its companion matrix form, then use a tool like my eigenshuffle, as found on the file exchange. It will actively attempt to resequence the roots it finds.
Sigh. I'm sorry. But a problem like this is not something with a trivial solution. And if you have no clue even what a function handle is, you need to spend some time learning MATLAB first. Start with the tutorials as suggested.
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!