Using of symprod fuction

6 次查看(过去 30 天)
HSUAN
HSUAN 2023-7-30
评论: HSUAN 2023-8-1
I want to calculate the following formula
So I wrote out the following code.(r=7)
syms Nb Nv K n Tb_1 Hb t Sb F Sv Hv D P Cj r q i z j
Nv(i)=[0 5 1 17 4 8 9];
Nb(i)=[1 ;1 ;1 ;1 ;1 ;1 ;1];
Sb(i)=[25 ;28 ;32 ;36 ;35 ;38 ;0];
for i=1:6
z(i)=symprod((Nv(j))*(Nb(i)*Sb(i)),j,2,(i+1));
disp(z(i))
end
Error using *
Dimensions do not match.
It display error using of * .
I tried modifying sb(i)=[25 28 32 36 35 38 0]; but still got the same result.
So I guess maybe it's not possible to multiply three matrices using.
syms Nb Nv K n Tb_1 Hb t Sb F Sv Hv D P Cj r q i z j
Nv(i)=[0 5 1 17 4 8 9];
Nb(i)=[1 ;1 ;1 ;1 ;1 ;1 ;1];
Sb(i)=[25 ;28 ;32 ;36 ;35 ;38 ;0];
for i=1:6
z(i)=symprod((Nv(j))*(Nb(i)),j,2,(i+1));
disp(z(i))
end
I try to multiply two matrices using.
The result is
44
1936
85184
3748096
164916224
7256313856
While it works, the result doesn't seem to match the formula I wrote above.
Please help me fix the code so that I can calculate the correct result, thank you for taking the time to answer.
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-7-30
编辑:Dyuman Joshi 2023-7-30
Why are you using symprod for that? A simple for loop will be good enough. You might even be able to vectorize it.
Also, this is just bad
Nv(i)=[0 5 1 17 4 8 9];
i is by default assigned as the imaginary constant in MATLAB, see below
i
ans = 0.0000 + 1.0000i
However, since you have defined i as a symbolic variable, the above line of code works. If you had not done that, and simply use that line of code, you would have gotten error.
If you want to index a vector or an array, you will have to define the indices before using them.
If you want to define an array simply assign the array to a variable name -
Nv = [0 5 1 17 4 8 9]
Nv = 1×7
0 5 1 17 4 8 9
Walter Roberson
Walter Roberson 2023-7-30
It is not possible to use any of the following as an array index:
  • symbolic variables
  • symbolic expressions
  • symfun
  • symmatrix
So in the context of
symprod((Nv(j))*(Nb(i)),j,2,(i+1))
where j is a symbolic variable (otherwise you could not symprod() over it) then Nv and Nb could not be arrays and would have to be symfun instead. For example,
Nv(i) = piecewise(i == 1, 0, i == 2, 5, i == 3, 1, i == 4, 17, i == 5, 4, i == 6, 8, i == 7, 9);

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2023-7-30
编辑:Torsten 2023-7-30
Nv=[0 5 1 17 4 8 9];
Nb=[1 1 1 1 1 1 1];
Sb=[25 28 32 36 35 38 0];
n = numel(Nv);
% Easy to follow
Z = 0;
for i = 1:n-1
p = 1;
for j = 2:i+1
p = p*Nv(j)*Nb(i)*Sb(i);
end
Z = Z + p;
end
Z
Z = 7.3851e+13
% Harder to follow
Z = sum((Nb(1:n-1).*Sb(1:n-1)).^(1:n-1).*cumprod(Nv(2:n)))
Z = 7.3851e+13

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by