Why is matlab not evaluating equation correctly

1 Ansicht (letzte 30 Tage)
Josh McCaffrey
Josh McCaffrey am 28 Mär. 2015
Kommentiert: Star Strider am 28 Mär. 2015
x1=sym('x1');
x2=sym('x2');
a=sym('a');
fx0(x1,x2) = 10*((x1)+3)^2+(x2)^2;
sp=[-1 , 1];
fx0d(x1,x2) = gradient(fx0);
dsd = -1*fx0d(sp(1,1),sp(1,2));
dsd1 = fx0(sp(1,1)+a*dsd(1,1),sp(1,2)+a*dsd(2,1))
When I run this code the variable dsd1 is not correct. The correct answer is 10(-40a+2)^2+(1-2a)^2 but matlab returns (2*a - 1)^2 + 10*(40*a - 2)^2 and I have no idea why.

Akzeptierte Antwort

Star Strider
Star Strider am 28 Mär. 2015
When I use your code, and then also add at the end of that code:
Q = 10*(-40*a+2)^2+(1-2*a)^2;
Q = simplify(Q, 'steps', 10)
and run it, I get as the result:
dsd1 =
(2*a - 1)^2 + 10*(40*a - 2)^2
Q =
(2*a - 1)^2 + 10*(40*a - 2)^2
Unless I’m missing something, those look a lot alike!
  2 Kommentare
Josh McCaffrey
Josh McCaffrey am 28 Mär. 2015
Thanks! I think I need to take a break.. lol
Star Strider
Star Strider am 28 Mär. 2015
My pleasure!
I know the feeling.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by