简体   繁体   中英

Matlab symbolic : calculate expression with vector of symbolic variables

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.

Update 1

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?

Update 2

@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?

Update 3

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.

Update 4

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:

不同的权重值:2*l+1

How to make change the variable "l" to make it replaced by real values (3, then 5, 7, ...., 5999)?

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

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM