Reputation:
I need to calculate with Matlab symbolic the following expression :
"l" is a vector of symbolic variables and "C_l" also (same length than "l", that is to say, l_max -l_min = 3000-10=2990
).
To calculate this, I did :
clear
syms l_min l_max fsky Np var1D varO1
l_min = 10
l_max = 3000
l = sym('l_',[1 (l_max - l_min)])
C_l = sym('C_l_',[1 (l_max -l_min)])
% First observable
var1D = symsum(2/((2*l+1)*fsky*Np^2), l, l_min, l_max)
varO1 = var1D/symsum(C_l, l_min, l_max)^2
Error using symengine
Invalid operands.
Error in sym/privBinaryOp (line 1030)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in / (line 373)
X = privBinaryOp(A, B, 'symobj::mrdivide');
Error in inequality (line 9)
var1D = symsum(2/((2*l+1)*fsky*Np^2), l, l_min, l_max)
I don't know to proceed to compute the expression of sigma_o,1^2 just above.
The first variance sigma_{o,1}^2 has been correctly coded. Now, I have difficulties with the second one but I have done some progress in investigation to compute this second variance (sigma_{o,2}^2) :
Now, I did :
clear
syms l_min l_max fsky Np var1D var2D varO1 varO2
l_min = 10
l_max = 3000
syms l
%l = sym('l_',[1 (l_max - l_min)])
C_l = sym('C_l_',[1 (l_max -l_min)])
% First observable
var1D = symsum(2/((2*l+1)*fsky*Np^2), l, l_min, l_max)
varO1 = var1D/sum(C_l)^2
% Second observable
var2D = symsum(((2*l+1)*fsky*Np^2), l, l_min, l_max)
varO2 = var2D/symsum((2*l+1)*C_l, l, l_min, l_max)^2
But I still get an error at the last line :
var2D =
9005901*Np^2*fsky
Error using symengine
Not a square matrix.
Error in sym/privBinaryOp (line 1030)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in ^ (line 330)
B = privBinaryOp(A, p, 'symobj::mpower');
Error in inequality (line 15)
varO2 = var2D/symsum((2*l+1)*C_l, l, l_min, l_max)^2
The issue is that I don't know how to mix the classical "sum
" function and the "symsum
" symbolic function because I have the following expression in divider :
varO2 = var2D/symsum((2*l+1)*C_l, l, l_min, l_max)^2
Is a workaround available here?
@CrisLuengo
:
Following your suggestion, I tried :
clear
syms l_min l_max fsky Np var1D var2D varO1 varO2
l_min = 10
l_max = 15
syms l
%l = sym('l_',[1 (l_max - l_min)])
C_l = sym('C_l_',[1 (l_max -l_min)])
assume(C_l > 0)
% First observable
var1D = symsum(2/((2*l+1)*fsky*Np^2), l, l_min, l_max)
varO1 = var1D/sum(C_l)^2
% Second observable
var2D = symsum(((2*l+1)*fsky*Np^2), l, l_min, l_max)
varO2 = var2D/(symsum(((2*l+1)*C_l), l, l_min, l_max)).^2
and get the error :
varO1 =
45894056/(97698825*Np^2*fsky*(C_l_1 + C_l_2 + C_l_3 + C_l_4 + C_l_5)^2)
var2D =
156*Np^2*fsky
Error using symengine
Invalid operands.
Error in sym/privBinaryOp (line 1030)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in / (line 373)
X = privBinaryOp(A, B, 'symobj::mrdivide');
Error in inequality (line 16)
varO2 = var2D/(symsum(((2*l+1)*C_l), l, l_min, l_max)).^2
I just want to express symbolically the following quantity (I have a set of data of couples (l, C_l), that is to say, I have for each "l" value an associated "C_l" value.
Where might the error come from ?
There may be a confusion about the expression of second quantity above. I have i=1:N couples of values (l_i, C_l_i), and the quantity appears like this :
Unfortunately, when I am doing :
% Second observable
var2D = symsum(((2*l+1)*fsky*Np^2), l, l_min, l_max)
varO2 = var2D./(sum(symsum(((2*l+1).*C_l), l, l_min, l_max))).^2
I get on terminal Matlab :
As you can see, I have the factor 156
which appears for each C_l and this is wrong since I would like to have as real values the factor (2*l+1)
for each C_l : computation of (2*l+1)
must be done with a real value, not as symbolic coefficients.
I have badly expressed my issue : with the solution suggested in answer by @drakon101
, I have always the same weighting values.
What I would like to get is the following symbolic expression for the divider :
How to make change the variable "l" to make it replaced by real values (3, then 5, 7, ...., 5999) ?
Upvotes: 1
Views: 610
Reputation: 20487
(Posted solution in behalf of the question author to move it to the answer space).
I have found the following solution by coding :
clc
clear all
syms fsky Np l
l_min = 10
l_max = 15
C_l = sym('C_l_',[1 (l_max)]);
assume(C_l > 0);
% Second observable
var2D = symsum(((2*l+1)*fsky*Np^2), l, l_min, l_max)
below=0;
for l=l_min:l_max
below= below + (2*l+1)*C_l(l);
end
varO2= var2D/(below)^2
Upvotes: 0
Reputation: 536
I don't know what output you're looking for, but according to the equations you have displayed above, your second observable lines should be:
% Second observable
var2D = 2*symsum(((2*l+1)), l, l_min, l_max)./(fsky.*Np.^2)
varO2 = var2D./(symsum(((2.*l+1).*C_l), l, l_min, l_max)).^2
Upvotes: 2