I am using MATLAB for part of my final year project. I am solving a geometric series such as the sum of x^j, starting from j=0 up to n-1. I have the following code so far:
$Variable dictionary
%N Number of terms to sum
%alpha Sum of series
%x Vector of constants
%n Loop counter
N = input('Enter the number of terms to sum: ');
alpha = 0;
x = [0.9 0.99 0.999 0.9999 0.99999 0.999999];
for n = 0:N-1
alpha = alpha + (x.^(n));
end
format long
alpha
I want to be able to put the value of n
in myself and to be able to input more than value of n
. Is it possible to do a loop inside a loop for this? For example declare N
as a vector then use the for loop I currently have inside it?
This code would work with string input -
N = input('Enter the number of terms to sum: ');
N = str2num(N);
Rest of the code stays unchanged.
Enter input as a string, ie as an example -
Enter the number of terms to sum: '2 5 8 11 14 17'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.