MATLAB: Int(exp,var) gives different results for same symbolic expression

intMATLABsymbolic

Hi everyone,
can someone explain to me why the two symbolic expressions below yield different results when being integrated over x?
syms x y a
E1 = a*(x+y);
E2 = a*x + a*y;
int(E1,x)
ans = (a*(x + y)^2)/2
int(E2,x)
ans = (a*x*(x + 2*y))/2
Where clearly the first integral is not what I'd expect it to be. It seems for E1 int() is integrating x+y and not just x.
Thank you
Rob

Best Answer

  • Both solutions are equivalent and differ by a constant factor. The ambiguity can be resolved by using a condition (like the value of the function at initial point).
    syms x y a
    E1 = a*(x+y);
    E2 = a*x + a*y;
    y1 = expand(int(E1,x))
    y2 = expand(int(E2,x))
    Result
    y1 =
    (a*x^2)/2 + a*x*y + (a*y^2)/2
    y2 =
    (a*x^2)/2 + a*y*x
    differes by a term containing 'y'. If you take the derivative of both functions w.r.t. 'x', you will get the original function back.