Quoting shwaip
close.
matlab uses 1-based indexing, so the first element of the array is x(1).
i'd do something like this:
Code:
k=5
x=0.5
est=0;
for n=0:k
est = est + x^n;
end
THANKS!! I think I got it!
My friend and I have one more we can't figure out, if you are willing to help. I'll give you the problem, original code template, and what I have so far.
- An m-file, for_sines.m, with minimal documentation is in the classwork folder – add code to generate and plot the 3 sine waves from Lab 4 using"for" loops. Recall that we plotted sin(2pft) vs. t for f = 6, 8, and 10 Hz and t running from 0 to 2.5 sec in 0.01 sec increments. Lab 4 Assignment is in Lecture 4 in the "classwork" folder. The t array and sine array are initialized in for_sines.m . Other than this initialization, don't use any vector initialization; that is, generate both the final time and frequency vectors and the 3 sine waves using loops. Generate the 3 subplots using a loop. Your code will yield the same graph as before; however, now include frequency is in the title.
Code:
Template:
% for_sines.m
% yourname and lab section
fprintf('\nyourname labsection\n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
% initialize f to its proper values using a "for" loop
;
% initialize t to its proper values using a "for" loop
;
% calculate the sine and plot using a "for" loop
figure;
What I have thus far:
Code:
% for_sines.m
% yourname and lab section
fprintf('\n Gretchen Paden, Th 3:00-5:00 \n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
sin_t(1,:) = sin(2*pi*f(1).*t)
t = 0:.01:2.5
f = 6:2:10
% initialize f to its proper values using a "for" loop
for f=0:.01:2.5
disp(f)
end
% initialize t to its proper values using a "for" loop
for t = 0:.01:2.5
disp(f)
end
% calculate the sine and plot using a "for" loop
figure;
for ii
y(ii,;)=sin(2*pi*f(ii).*t);
subplot(3,1,ii),plot(x,y(1,:),'k');ylabel('y');xlabel('x');title(Frequency);grid on
end
Let me know if you can help. Thanks a million!!