How to solve a cubic equation using Genetic Algorithms?

조회 수: 7 (최근 30일)
Nick
Nick 2021년 4월 20일
댓글: Walter Roberson 2021년 4월 20일
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