Multiple Sum of Series in a Variable - Syntax Problem
조회 수: 7 (최근 30일)
이전 댓글 표시
Hello,
I got a problem expressing the equations bellow, in matlab. There is a dat variable which is a 7x2 table and has values in it. I must create a function wich will have "dat" as an input and "a" as an output. So if we go to the first "symsum" of SSxy equation I want to express "for j=1 to P do the sum of series of Xj*Yj" as it is shown bellow in the code. The problem is that I got errors and I must have the syntax of symsum wrong. Can someone help me understand my mistake? Thanks for your time, appreciate your help!
dat - variable
1000 79
3000 71
9000 59
50000 49
93000 39
223000 33
510000 24
function [a]=Example(dat)
x=log10(dat(:,1));
y=dat(:,2);
P=length(dat);
syms x y
SSxy=(symsum(x(j)*y(j),j,1,P))-(((symsum(x(j),j,1,P))*(symsum(y(j),j,1,P)))/P);
SSxx=(symsum(x(j).^2,j,1,P))-(((symsum(x(j),j,1,P))^2)/P);
a=SSxy/SSxx;
end
댓글 수: 0
채택된 답변
Jan
2021년 3월 24일
편집: Jan
2021년 3월 24일
Why do you try a symbolic sum, when your input is numerical? Here a numerical summation:
function a = Example(dat)
x = log10(dat(:, 1));
y = dat(:, 2);
P = size(dat, 1); % Safer than length(dat)
% X.' * y is the dot product, same as sum(x .* y)
SSxy = x.' * y - sum(x) * sumy(y) / P;
SSxx = x.' * x - sum(x)^2 / P;
a = SSxy / SSxx;
end
댓글 수: 2
Jan
2021년 3월 25일
symsum creates a symbolic formula. You could create it and evaluate the result for numeric input. But this is ways slower than the direct numeric summation.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!