Help with for loop

6 views (last 30 days)
benny
benny on 19 Nov 2014
Edited: Andrei Bobrov on 19 Nov 2014
I need help or direction on for loops given the question
Use a loop to create a vector vout that contains the 5‐value moving average of vin.  Since it takes 5 values of input to create the first value in the output, there will be a slight delay in the output.  In other words, the first non‐zero value of vout will occur at the 5th position of vout and will be the average of vin(1) through vin(5).
given information
f1 = 100; % The lower frequency in cycles per second
f2 = 400; % The higher frequency in cycles per second
w1 = 2*pi*f1; % The lower frequency in radians per second
w2 = 2*pi*f2; % The higher frequency in radians per second
T1 = 1/f1; % The period of the lower frequency
T2 = 1/f2; % The period of the higher frequency
ns = 20; % Number of samples per period of high freq. component
% Plot the signal over 4 periods of the lower frequency component. The
% increment of the composite signal will be the period of the higher
% frequency component divided by ns.
t = (0:T2/ns:4*T1);
vin = cos(w1.*t) + cos(w2.*t);
vout(k) = (vin(k) + vin(k-1) + vin(k-2) + vin(k-3) + vin(k-4) ) / 5
This is what i tried
for k= 5:90;
vout(k) = (vin(k) + vin(k-1) + vin(k-2) + vin(k-3) + vin(k-4) ) / 5
end
plot(k,vin)
plot(k,vout)
  3 Comments
benny
benny on 19 Nov 2014
I dont get anything on the graph, my values for vin were guessed
Guillaume
Guillaume on 19 Nov 2014
plot(vin);
plot(5:numel(vout), vout);
should solve that (assuming hold all). But again, that has nothing to do with the loop. Since your question is about the loop, again, what problem are you having with it?

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 19 Nov 2014
Edited: Andrei Bobrov on 19 Nov 2014
f1 = 100;
f2 = 400;
w1 = 2*pi*f1;
w2 = 2*pi*f2;
T1 = 1/f1;
T2 = 1/f2;
ns = 20;
t = (0:T2/ns:4*T1);
vin = cos(w1.*t) + cos(w2.*t);
vin = vin(:);
out = conv2(vin,ones(5,1)/5,'valid');
with loop
out = zeros(numel(vin)-4,1);
for jj = 1:numel(vin)-4
out(jj) = mean(vin(jj:jj+4));
end
plot:
plot([vin(5:end), out])

More Answers (1)

MA
MA on 19 Nov 2014
you have common mistakes,here your correct code:
clear all
close all
clc;
f1 = 100; % The lower frequency in cycles per second
f2 = 400; % The higher frequency in cycles per second
w1 = 2*pi*f1; % The lower frequency in radians per second
w2 = 2*pi*f2; % The higher frequency in radians per second
T1 = 1/f1; % The period of the lower frequency
T2 = 1/f2; % The period of the higher frequency
ns = 20; % Number of samples per period of high freq. component
% Plot the signal over 4 periods of the lower frequency component. The
% increment of the composite signal will be the period of the higher
% frequency component divided by ns.
t = (0:T2/ns:4*T1);
vin = cos(w1.*t) + cos(w2.*t);
for k=5:90
vout(k) = (vin(k) + vin(k-1) + vin(k-2) + vin(k-3) + vin(k-4) ) / 5;
end
k=5:90;
voutn(1,:)=vout(5:90);
subplot(2,1,1)
plot(t,vin)
title('vin')
subplot(2,1,2)
plot(k,voutn)
title('vout')

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!