Solve a cubic equation using MATLAB code
显示 更早的评论
I have a cubic equation whose coefficients are varying according to a parameter say w in the following manner:
a=2/w;
b=(3/w+3);
c=(4/(w-9))^3;
d=(5/(w+6))^2;
a*(x^3)+b*(x^2)+c*x+d=0
I want to solve the above equation using a m-file not in the command window. Is it possible ? I am fairly new to MATLAB, so help would be appreciated.
采纳的回答
更多回答(4 个)
Bruno Luong
2011-2-26
编辑:Image Analyst
2018-11-16
Please download this FEX,
w = linspace(-3,3,10);
a = 2./w;
b = (3./w+3);
c = (4./(w-9)).^3;
d = (5./(w+6)).^2;
X = CardanRoots([a(:) b(:) c(:) d(:)])
Each row of X contains three solutions of the respective cubic equation. Filter out the complex solutions if you don't need them.
3 个评论
Ann Ytterberg
2018-11-16
Where is this CardonRoots.m?
Image Analyst
2018-11-16
It's the first hit from Google: CardanRoots
Bruno Luong
2018-11-16
Click on the link it should work
Walter Roberson
2011-2-26
EDIT: corrected missing syms, added more detail
function sols = solve_cubic(a, b, c, d)
syms x
sols = solve(a*x^3 + b*x^2 + c*x + d);
end
The inputs to this can include symbolic expressions.
The outputs of this will be symbolic numeric radicals if the inputs are all numeric, but might include symbolic RootOf expressions if any of the arguments are symbolic. To convert symbolic numbers to double precision floating point numbers, use double() on the output.
5 个评论
Bhagat
2011-2-26
编辑:Walter Roberson
2020-9-13
Bhagat
2011-2-26
Walter Roberson
2011-2-26
Sorry I missed a statement before.
Walter Roberson
2011-2-26
Don't use the apostrophes, use the syms command.
Bhagat
2011-2-26
Walter Roberson
2011-2-26
Provided that the input values are scalar numeric:
function sols = solve_cubic(a, b, c, d)
sols = roots([a,b,c,d]);
end
EDIT: provide more detail
The solutions to this will be a column vector of double precision floating point numbers (possibly complex.)
13 个评论
Bhagat
2011-2-26
Walter Roberson
2011-2-26
编辑:Walter Roberson
2020-9-13
Wvals = 1:exp(1):pi^3; %chose appropriate vals
nW = length(Wvals);
out1 = nan(1,nW);
out2 = nan(1,nW);
out3 = nan(1,nW);
for K = 1 : nW
w = Wvals(K);
a=2/w; b=(3/w+3); c=(4/(w-9))^3; d=(5/(w+6))^2;
these_sols = solve_cubic(a,b,c,d);
these_sols = these_sols(imag(these_sols) == 0);
L = length(these_sols);
if L > 0
out1(K) = these_sols(1);
end
if L > 1
out2(K) = these_sols(2);
end
if L > 2
out3(K) = these_sols(3);
end
end
plot(Wvals, out1, '.', Wvals, out2, '.', Wvals, out3, '.');
This will plot up to 3 points per w value, as there can be up to 3 real solutions in general. (I would have to look further to see whether 3 real solutions are actually possible.)
Walter Roberson
2011-2-26
There is an algebraic theorem that any cubic in real coefficients has either one or three real roots, never 0 or 2. You can use that theorem to simplify the above code slightly.
I tried some values myself, and found that indeed for most values of w, there is only one real root. There is a region, though, that is somewhere within -10 to +10, in which there are 3 real roots for each value of w.
Bhagat
2011-2-26
Walter Roberson
2011-2-26
find(Matrix < ParticularNumber)
Bhagat
2011-2-28
Bhagat
2011-2-28
Walter Roberson
2011-2-28
Matrix(Matrix < ParticularNumber)
Bhagat
2011-2-28
Bhagat
2011-2-28
编辑:Walter Roberson
2020-9-13
Bhagat
2011-2-28
Walter Roberson
2011-2-28
编辑:Walter Roberson
2020-9-13
You _are_ putting in w as a constant value: it's value is the entire list 10:2:30 . Using a list like that is not varying w.
Although it is possible to slightly change your a, b, c, and d so that values for each of the w would be calculated, the roots() call will not accept a vector of values, and must be called for each individual a, b, c, d combination.
w0 = 10:2:30;
for K = 1 : length(w0)
w = w0(K);
%calculate a, b, c, d here, and calculate the roots and select those in the proper range and continue on through to the calculation of "l". Do _not_ do the plot() at this time though.
L{K} = l;
end
Now you can plot of w0(K) against L{K}, but keep in mind that you might have no real roots or one real root or three real roots for any individual w, so you will have to build appropriate plotting code.
Bhagat
2011-3-1
Awa Kologo
2020-9-13
0 个投票
- Solving for the roots of a third order polynomial (i.e. finding a value that will make the expression equal zero) may require tedious algebra to do by hand but can be solved easily by a computer using an iterative approach. For example, finding the roots of the expression: , (ie. a value of x so that the equation is satisfied) is time consuming to do by hand. However, plugging in a guess for and then modifying that guess until a tolerance is met gives . Write a MATLAB script that solves exponential equations of the form where are constants that the user inputs in a 1 x 4 row vector: [a b c d]. The program will first check whether or not the input is a 1 x 4 matrix of numerical entries throw an error if it is not. The program will solve for one root of the polynomial iteratively to 6 decimal places and print out the value. You can take this to mean that your program should keep running for as long as Your program will need to make an initial guess for in order to solve iteratively. In most cases, the will give a reasonable first guess. You can use the roots() function to check your answer.
1 个评论
Walter Roberson
2020-9-13
This does not appear to be an Answer to the Question that was asked.
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!