I have a matrix of numeric data;
(let's say 4*4 )
how can I add a syms expression (e.g 3*s) in some elements of it???
I've tried to add it but I get this Error:
The following error occurred converting
from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot
convert the input expression into a
double array.
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)
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!
I want to insert 'double' type values in a matrix. For that I am creating a matrix with following lines of Matlab code:
dpitchcnt=(N/256); %N is total number of byte
pitchvec(1:int64(dpitchcnt)); %creating a matrix 'pitchvec' with 1 row and int64(dpitchcnt)' columns
size(pitchvec) %Trying to display the size.
I am getting the following error while carrying out the above operation:
Undefined function or method '_colonobj' for input arguments of type
'int64'. Error in ==> sample at 31 pitchevec(1:int64(dpitchcnt));
What am I doing wrong?
The syntax varName(1:10) will get the first 10 values of varName, not create the variable varName;
To create a matrix you can use
pitchvec = zeros(1,int64(dpitchcnt)); %A zero-matrix
matrixSize = size(pitchvec);
You can also use ones(n,m);%Create a n times m matrix with 1 all over.