Multiple Sum of Series in a Variable - Syntax Problem

6 views (last 30 days)
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

Accepted Answer

Jan
Jan on 24 Mar 2021
Edited: Jan on 24 Mar 2021
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 Comments
Christos Tsallis
Christos Tsallis on 24 Mar 2021
My thoughts were more complex than they should be I guess... Can I do the same with the use of symsum? Really thanks for your reply, appreciate it!
Jan
Jan on 25 Mar 2021
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.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!