Solve a cubic equation using MATLAB code
53 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Matt Fig
2011 年 2 月 28 日
From your comments, it looks like you want this instead:
function [] = solve_cubic()
w=10:2:30;
a=4*((9*w)-7);
b=(-2)*((18*w)-16).*(w+1);
c=3*((w+1).^2).*((3*w)-1);
d=(-3)*((1+w).^3);
f = zeros(1,length(w));
for ii = 1:numel(w)
g = roots([a(ii),b(ii),c(ii),d(ii)]);
f(ii) = g(g<1);
end
ptr=2./(1+w);
u=(1+w)./2;
l=(u.*(1-ptr.*f).^3)./3;
plot(w,l);
5 件のコメント
Matt Fig
2011 年 2 月 28 日
You keep posting the same erroneous code, but did you try to copy what I wrote and see if it gives you the values you expect? I ran it and it got no errors. If you don't want it as a function, simply delete the first line; it isn't hard to do!
Ubong
2014 年 9 月 11 日
Seen the final response by Matt, that was just so nice . Still have questions for Matt though
1. what is the consequence of not adding the f = zeros(1,length(w)); line? I found that I can still get my row vector, f, without adding it
2. What do I do, if g had returned both real and complex numbers for every ii, and I need to get only real numbers from g
その他の回答 (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 件のコメント
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 件のコメント
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 件のコメント
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.
Awa Kologo
2020 年 9 月 13 日
- 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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!