Find optimized x1, x2, x3 for a given y.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Easir Papon
am 2 Dez. 2017
Kommentiert: Easir Papon
am 2 Dez. 2017
Hi,
I have a function like below:
y=x1^2+2*x1*x2+x3^2;
I want to find the values for x1, x2 and x3 for y=2.
How can I do that? I tried with genetic algorithm using 'ga' function but looks like that's not the right one. Can anyone please help me how should I approach or is there any built in function to get that? Thanks in advance.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 2 Dez. 2017
y = 2;
fun = @(x) (x(1).^2 + 2.*x(1).*x(2) + x(3).^2 - y).^2
bestx = ga(fun, 3);
x1 = bestx(1); x2 = bestx(2); x3 = bestx(3);
Note: there are an infinite number of solutions. It does not start to get interesting until you put on constraints.
3 Kommentare
Walter Roberson
am 2 Dez. 2017
No, the ^2 I used is needed. ga is a minimizer. If you were to use
fun = @(x) x(1).^2 + 2.*x(1).*x(2) + x(3).^2 - y;
then it would happily minimize to negative infinity by making x(2) negative (if it was unconstrained). You are not asking the question "which x gives x(1).^2 + 2.*x(1).*x(2) + x(3).^2 most less than y", you are asking the question "which x gives x(1).^2 + 2.*x(1).*x(2) + x(3).^2 equal to y". The way to use a minimizer to get one (real) value equal to another is to minimize the square of the difference: the difference will be 0 when they are the same, and the square of the difference increases as they get further apart.
"And if my x2 is bounded by 2<x2<5 and x1=2, and I want to find optimized result for y=2. That means I am looking for the values of x2 and x3"
y = 2;
fun = @(x) (x(1).^2 + 2.*x(1).*x(2) + x(3).^2 - y).^2;
nvars = 3;
A = []; b = [];
Aeq = []; beq = [];
lb = [2 2*(1+eps) -inf];
ub = [2 5*(1-eps) inf];
bestx = ga(fun, nvars, A, b, Aeq, beq, lb, ub);
x1 = bestx(1); x2 = bestx(2); x3 = bestx(3);
For such a simple system you could also consider fmincon:
y = 2;
fun = @(x) (x(1).^2 + 2.*x(1).*x(2) + x(3).^2 - y).^2;
x0 = [2 3.5 20];
A = []; b = [];
Aeq = []; beq = [];
lb = [2 2*(1+eps) -inf];
ub = [2 5*(1-eps) inf];
bestx = fmincon(fun, x0, A, b, Aeq, beq, lb, ub);
x1 = bestx(1); x2 = bestx(2); x3 = bestx(3);
Weitere Antworten (1)
Akira Agata
am 2 Dez. 2017
Another solution would be to use fsolve function, like:
fun = @(x) x(1)^2+2*x(1)*x(2)+x(3)^2 - 2;
options = optimoptions(@fsolve,'Algorithm','Levenberg-Marquardt');
% Find the solution from the initial point (x1,x2,x3) = (1,2,3)
fsolve(fun,[1,2,3],options)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Genetic Algorithm finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!