MATLAB: How to expand a function to a polynomial expression

expandpolynomial

I want to expand the equation (1) to (2). I tried as follows, but the result goes back to the equation (1).
clear
clc
syms x k;
assume(-1< x/4 <1);
expand((1-x/2)*symsum((x/4)^k,k,0,inf));
simplify(ans);
pretty(ans)
ans= 2 (x – 2) / ( x – 4 )

Best Answer

First, even though people think it is nice to write the shorthand in mathematics of
-1< x/4 <1
this is NOT a valid MATLAB condition in general. Yes, YOU know what it is intended to mean. But MATLAB typically does not see things like that.
Luckily, the symbolic toolbox is able to understand what you wrote.
syms x
assume(-1< x/4 <1)
assumptions
ans =
[ -1 < x/4, x/4 < 1]
In normal MATLAB expressions however, what you wrote will fail.
x= -5:5;
-1< x/4 <1
ans =
1 1 0 0 0 0 0 0 0 0 0
Whereas we would have expected a different result.
(-1 < x/4) & (x/4 < 1)
ans =
0 0 1 1 1 1 1 1 1 0 0
So always be careful when you make an assumption of how MATLAB will treat your favorite mathematical shorthand.
Regardless, to solve your problem, simplest is just:
taylor(x/(2*(x/4 - 1)) - 1/(x/4 - 1),'order',10)
ans =
- x^9/262144 - x^8/65536 - x^7/16384 - x^6/4096 - x^5/1024 - x^4/256 - x^3/64 - x^2/16 - x/4 + 1