When using ismember for small symbolic arrays I get the expected result.
For example:
syms a b c d e f
ismember([a,b,c],[e,f,a])
ans =
1 0 0
However for larger arrays I get an error
ismember([a,b,c,d,e,f],[f,e,b,b,c,a])
Undefined function 'cumsum' for input arguments of type 'sym'.
Error in unique>uniquelegacy (line 411)
pos = cumsum([1;d]); % Lists position, starting at 1.
Error in unique (line 95)
[varargout{1:nlhs}] = uniquelegacy(varargin{1});
Error in ismember>ismemberlegacy (line 377)
[au,~,an] = unique(a(:));
Error in ismember (line 76)
[varargout{1:nlhs}] = ismemberlegacy(varargin{:});
Can you explain why this is? Is there a way around this for the larger symbolic arrays? Thanks!
Related
I want to fill the rows of the matrix with symbolic expressions using for-loop.
The code is given below.
for r=1:N
%dL/dfidot
frst(r)=diff(L,fidot(r));
%d/dt*dL/dfidot
dfrst(r)=diff(frst(r),fi(r))*fidot(r)+diff(frst(r),fidot(r))*fiddot(r);
%dL/dfi
scnd(r)=diff(L,fi(r));
%EQ of Motion
EqofMotion(r)=dfrst(r)-scnd(r)==0;
acc(r)=solve(EqofMotion(r),fiddot(r));
C=zeros(N,1);
C(r,1)=acc(r);
end
acc is symbolic array, C is matrix. The idea is to fill r-th row of C matrix with acc(r) using loop. Program gives me an error as following:
The following error occurred converting from sym to double:
Error using symengine (line 58)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in Trying (line 56)
C(r,1)=acc(r);
How can I fix this problem?
You are trying to assign a symbolic value to a double array element. This is illegal because symbolic objects are not implicitly convertible to double.
To solve this problem, you can make the C array an array of symbolic objects:
C = sym(zeros(N,1)); % now C is an array containing the symbolic expressions 'zero'
C(r,1) = acc(r);
I have a vector v and would like to define a function x using following formular:
Now I try to define this in Matlab, but for some reason I can't access an array within a symsum.
v = [1 2 3 4];
syms k
x = #(t) symsum(v(k) * exp(2*pi*i*k*t/size(v)), k, 1, size(x_n));
When I try to evaluate this Function x(4) I get an exception:
Error using sym/subsindex (line 796)
Invalid indexing or function definition. When defining a function, ensure that the arguments are
symbolic variables and the body of the function is a SYM expression. When indexing, the input
must be numeric, logical, or ':'.
Do you know, why? Do you have a solution or workaround?
I want to define a vector,X, with five elements:
syms a
X = zeros(1,5)
X(1) = 1;
for k=1:4
X(k+1)=X(k)+a^2;
end
Actually I need to have the vector X, that its elements should be based on variable a. But I face an error in Matlab, when I write the code above:
The following error occurred converting from sym to double:
Error using symengine (line 58)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in Code2 (line 5)
X(k+1)=X(k)*a^2;
How to fix this?
You are mixing between symbolic variables to doubles,
Doubles holds a value, you can use the loop that you wrote only if a is a double and already holds a value (the value can be the input of a function) .
for example :
function ret=testFunc(a)
X = zeros(1,5)
X(1) = 1;
for k=1:4
X(k+1)=X(k)+a^2;
end
ret=X
end
if you want to work with syms (for other symbolic analysis) you can define x as a symbol too, for example :
syms a x
x(1)=1;
for i=2:5
x(i)=x(i-1)+a.^2;
end
now, x is a function of a, and if you print x you will get :
[ 1, a^2 + 1, 2*a^2 + 1, 3*a^2 + 1, 4*a^2 + 1]
to evaluate the value of x, you will still need to input a value for a.
Matlab help suggest subs function for replacing a in wanted value as follows:
y = subs(x,a,4)
in that point y is still a symbol and you will need to make it a double by using
double(y)
Is it possible to do a summation series in Octave?
In matlab there is symsum function for it, however I didn't found anything similar for octave.
For example, I want to find the following sum
Addendum:
Whether it's possible to sum something like this
f = #(x) nchoosek(5,x)*0.1.^x*0.9.^(5-x)
sum(f([0:5]))
Failed with error
error: called from:
error: /usr/share/octave/3.6.4/m/help/print_usage.m at line 87, column 5
error: /usr/share/octave/3.6.4/m/specfun/nchoosek.m at line 95, column 5
error: at line -1, column -1
error: evaluating argument list element number 1
If you don't need an analitical solution, then you don't need symsum. For example if you want to calculate
the you can simply use sum
sum([1:5])
Here is another example:
f = #(x) exp(-x)
sum(f([1:5]))
And another one with factorial function:
g = #(n) 1 ./ factorial(n)
sum(g([0:5]))
the same, but without anonymous function:
sum(1 ./ factorial([0:5]))
Update
As for your last example, nchoosek allows only scalar arguments. So, you'll need additional arrayfun call:
f = #(x) nchoosek(5,x)*0.1.^x*0.9.^(5-x)
sum(arrayfun(f,[0:5]))
I want to put a function in feval(f,x) that has a vector as input
e.g.
function [ ret ] = f (x)
ret = x(1)^2 - x(2)^2;
end
and
x = [1,2]
but octave always gives an error code:
`x' undefined near line 6 column 18
evaluating argument list element number 1
evaluating argument list element number 1
It seems feval can only evaluate numbers and not vectors. Is there any way to do this?
Create a handle to your function and then call feval on the handle, passing it your vector as its argument:
h = #(x)myfun(x);
x = [1, 2];
y = feval(h, x);
I tried this out in Octave on your function and it seems to work.