Biggest sum of series in Matlab

6 views (last 30 days)
Trana Andrei
Trana Andrei on 17 Nov 2020
Answered: Esen Ozbay on 20 Sep 2022
Hello.I have a problem to solve in matlab and i don't know how to take it.I ve just started to learn matlab, so i don't know the codes very good.Could you help me with this problem?
EXERCISE:Which is the biggest value of ,,n'' with the property that S(n)=(sum of (k^2) from k=1 to n)<L , where L is given?Solve it using summing and using the formula wich gives us S(n).
Can you help me please to solve this in matlab? I tried to write that sum using the formula:
syms k n
F1 = symsum(k^2,k,1,n).
I don't know how to continue.

Answers (1)

Esen Ozbay
Esen Ozbay on 20 Sep 2022
For this question, I think you should use a for loop and check each number one by one. If you use 'n' in your expression, the result is in terms of 'n'. If you use a number instead of 'n', it will give you the result.
Note: using 'syms' and 'symsum' is not the best way to do this, but I wanted to help you with a code that included the one you already wrote.
Try this:
L = 50; % define L, I chose 50 randomly.
syms k
largest_n = 0; % the largest one we know so far is 0.
for n = 1:sqrt(L)
% we are trying numbers one by one (going from 1 to sqrt(L)),
% I am going only up to sqrt(L) because values greater than this
% cannot satisfy our condition by definition. You can start from
% L as well, but it will slow down your code since it is performing
% extra calculations for nothing.
F1 = symsum(k^2, k, 1, n); % F1 is a number, but the variable type is 'symbol'
F1 = double(F1); % change the type of F1 to 'double' so that we can do comparisons.
if F1 < L % if the calculated number is smaller than the desired number
largest_n = n;
% the largest n satisfying our condition that we know of is n.
% if we find a larger n later in the loop, we will change this.





Community Treasure Hunt

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

Start Hunting!