Prev: How to read serial port with real time windows target
Next: Which LAPACK routines are _actually_ used (for A\B and B/A)
From: Rolando on 14 Jun 2010 09:42 I'm trying to differentiate sin(x) based on the iteration loop. But evertime i try to i get this error 'DOUBLE cannot convert the input expression into a double array' y=[]; syms x; f=sin(x); for i=1:7 % Taylor series of sin(x) y(i,:)=(-1)^i*(diff(f,i))/(2*i+1); end
From: Steven Lord on 14 Jun 2010 10:12
"Rolando " <rchacon(a)handson.net> wrote in message news:hv5bje$1c5$1(a)fred.mathworks.com... > I'm trying to differentiate sin(x) based on the iteration loop. But > evertime i try to i get this error 'DOUBLE cannot convert the input > expression into a double > array' > y=[]; > syms x; > f=sin(x); > for i=1:7 > % Taylor series of sin(x) > y(i,:)=(-1)^i*(diff(f,i))/(2*i+1); In order to store a sym object (which is what the right-hand side is) into a double array (which is what the subscripted assignment on the left is attempting to do) MATLAB must convert the sym object into a double array. Since your right-hand side contains a symbolic variable and you haven't given that symbolic variable a numeric value, MATLAB cannot perform the conversion and so (correctly) errors. Two solutions: 1) Make y a vector of sym objects. If you do this, no conversion will be needed as the left and right-hand sides will be the same type. y = sym(zeros(7, 1)); syms x; f=sin(x); for i=1:7 % Taylor series of sin(x) y(i)=(-1)^i*(diff(f,i))/(2*i+1); end 2) Make your y a cell array. Cell arrays can store any data type, so when you assign the sym object into the _contents_ of the ith cell in the cell array, no conversion is needed. [If you attempted to assign into the ith cell itself using y(i) = ... then MATLAB would attempt to convert the sym object into a cell itself, which would fail for different reasons.] y = cell(7, 1); syms x; f=sin(x); for i=1:7 % Taylor series of sin(x) y{i}=(-1)^i*(diff(f,i))/(2*i+1); end -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com |