write a matlab/octave script that finds the sum of following series. 1 – 1/3 + 2 – 1/5 + 3 – 1/7 + 4 – 1/9 + … + 98 – 1/197 + 99 -1/199 in matlab

You can write a MATLAB/Octave script to find the sum of the given series using a loop. Here's an example script:

% Initialize variables
n = 99; % number of terms in the series
sum = 0;
sign = 1;

% Loop through the series
for i = 1:n
    % Calculate the term
    term = sign * i - 1 / (2*i+1);

    % Update the sum
    sum = sum + term;
    
    % Toggle the sign
    sign = -sign;
end

% Display the sum
disp(sum);
304 chars
20 lines

This script initializes the sum to 0 and sign to 1. It then iterates from 1 to 99, calculating each term in the series by alternating the sign and using the formula term = sign * i - 1 / (2*i+1). The sum is updated with each term in each iteration. Finally, the sum is displayed using the disp function.

related categories

gistlibby LogSnag