Pooryaa
Pooryaa

Reputation: 54

How to calculate the summation of a function over some values, in MAT LAB?

What MATLAB command should be used to evaluate the sum of f(i)s, where, for example, i=1..15? I see numeric::sum(f(i), i=a..b) in the help, but I get the error:

Error: Unexpected MAT LAB operator

by entering the command: numeric::sum(i,i=1..100)

Upvotes: 1

Views: 363

Answers (3)

Hadi
Hadi

Reputation: 1213

1)define your variable ex:

var=1:1:1000;

or

var=35:3:200;

etc.

2)convert it to a column vector

var=var';

3)use these functions: a)sum ,b)arrayfun

res=sum(arrayfun(@your_desired_function,var));

4)this way you could sum over any arbitrary function which you defined before(even with more than one input argument);

5)another way is to use for loop

var=3:5:4500;
s=0;
for i=1:numel(var)
    //do your calculation on v(i) and stor the result in res for example
    res=v(i)^2+3*(v(i))+5;
    s=s+res;
end

Upvotes: 1

Pooryaa
Pooryaa

Reputation: 54

The function symsum will do it. For example the commands:

syms k
symsum(k^2, 0, 3)

compute:

(0)^2 + (1)^2 + (2)^2 + (3)^2

which equals 14

Upvotes: 2

Shai
Shai

Reputation: 114786

The command numeric::sum is not a Matlab command but rather a MuPAD command.

In order to sum entries in Matlab you should use sum function:

sum( 1:100 )

Evaluates to

5050

Upvotes: 2

Related Questions