How to solve a cubic equation using Genetic Algorithms?

12 次查看(过去 30 天)
Hello there,
I want to write a code in matlab that finds a real solution for the equation x^3+x^2+x+7=0 , -5<x<5, using Genetic Algorithms.
How can I implement that with code?

采纳的回答

Stephan
Stephan 2021-4-20

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-4-20
编辑:Walter Roberson 2021-4-20
f = @(x) x.^4-2*x.^3+3*x.^2+x-4
f = function_handle with value:
@(x)x.^4-2*x.^3+3*x.^2+x-4
target = 5;
residue = @(x) (f(x)-target).^2
residue = function_handle with value:
@(x)(f(x)-target).^2
[bestx, fval] = ga(residue, 1)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
bestx = -1.1833
fval = 0.5017
roots([1, -2, 3, 1, -4-target])
ans =
0.7541 + 1.9406i 0.7541 - 1.9406i 1.7076 + 0.0000i -1.2159 + 0.0000i
  2 个评论
Nick
Nick 2021-4-20
So what fitness function should I use for the x^3+x^2+x+7=0 equation?
Walter Roberson
Walter Roberson 2021-4-20
One that computed the left hand side of that polynomial, with the target set to the constant that is the right hand side of the equation.
... you are obviously answering homework, so I deliberately showed you how to do it for a different polynomial, so that you would have to do at least minimal study of the technique instead of just copying what I posted.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by