script for looping function

2 views (last 30 days)
Gregory Power
Gregory Power on 4 Mar 2019
Edited: Stephan on 4 Mar 2019
I'm trying to write a script that defines a function, 5*sin((2*pi).*f1.*t)+10*cos((2*pi).*f2.*t), that loops through the function for the variable t from 0 to 100 in 0.1 increments. f1 and f2 are random numbers between 0.1 and 5. I can't seem to get past the function definition when I try to run my code. I think my loop is off as well, but I can't get to a point to check it. Below is my code. in the end I need to plot it, but I know how to do that, it's just getting the function to work and populate the q array. if you could just point out the problem with the function, that would be great. I do want to write the program within a single script file.
t=0:.1:100; %generates array of time from 0 to 100 in 0.1 increments
a=0.1; %variables to generate f1 and f2
b=5; %variables to generate f1 and f2
f1=a+(b-a).*rand(1, length(t)); %creates an array of random numbers between .1 and 5
f2=a+(b-a).*rand(1, length(t)); %creats an array of random numbers between .1 and 5
q=zeros(1, length(t)); %creates space to store outputs of function for plotting
g(t, f1, f2) = @(t, f1, f2) 5*sin((2*pi).*f1.*t)+10*cos((2*pi).*f2.*t); %function for loop
for i= 1:length(t) %supposed to loop function through arrays of t, f1, and f2
q(1:i)=g(t, f1, f2); % supposed to fun the loop and store outputs in array q
end
disp(q) %shows array q to see if it works
  1 Comment
Gregory Power
Gregory Power on 4 Mar 2019
p.s. this is the error that I am getting.
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Ex5a (line 10)
g(t, f1, f2) = @(t, f1, f2) 5*sin((2*pi).*f1.*t)+10*cos((2*pi).*f2.*t); %function for loop

Sign in to comment.

Answers (2)

Stephan
Stephan on 4 Mar 2019
Edited: Stephan on 4 Mar 2019
Hi,
try:
t=0:.1:100; %generates array of time from 0 to 100 in 0.1 increments
a=0.1; %variables to generate f1 and f2
b=5; %variables to generate f1 and f2
f1=a+(b-a).*rand(1, length(t)); %creates an array of random numbers between .1 and 5
f2=a+(b-a).*rand(1, length(t)); %creats an array of random numbers between .1 and 5
q=zeros(1, length(t)); %creates space to store outputs of function for plotting
g = @(t, f1, f2) 5*sin((2*pi).*f1.*t)+10*cos((2*pi).*f2.*t); %function for loop
for i= 1:length(t) %supposed to loop function through arrays of t, f1, and f2
q(:)=g(t, f1, f2); % supposed to fun the loop and store outputs in array q
end
disp(q) %shows array q to see if it works
Note that you can get the same result without a loop:
t=0:.1:100; %generates array of time from 0 to 100 in 0.1 increments
a=0.1; %variables to generate f1 and f2
b=5; %variables to generate f1 and f2
f1=a+(b-a).*rand(1, length(t)); %creates an array of random numbers between .1 and 5
f2=a+(b-a).*rand(1, length(t)); %creats an array of random numbers between .1 and 5
q=zeros(1, length(t)); %creates space to store outputs of function for plotting
g = @(t, f1, f2) 5*sin((2*pi).*f1.*t)+10*cos((2*pi).*f2.*t); %function for loop
res = g(t,f1,f2);
This is because your function handle is vectorized and therefore accepts vectors as input.
Best regards
Stephan

Dennis
Dennis on 4 Mar 2019
You just need to get rid of the (t,f1,f2) before the '=' when defining your function. You need to take a look at your loop aswell, t is a vector and the result of your calculation will not fit in q. Also q(1:i) should most likely be q(i).
Please check if this provides the desired results:
t=0:.1:100; %generates array of time from 0 to 100 in 0.1 increments
a=0.1; %variables to generate f1 and f2
b=5; %variables to generate f1 and f2
f1=a+(b-a).*rand(1, length(t)); %creates an array of random numbers between .1 and 5
f2=a+(b-a).*rand(1, length(t)); %creats an array of random numbers between .1 and 5
q=zeros(1, length(t)); %creates space to store outputs of function for plotting
g = @(t, f1, f2) 5*sin((2*pi).*f1.*t)+10*cos((2*pi).*f2.*t); %function for loop
%supposed to loop function through arrays of t, f1, and f2
q(:)=g(t, f1, f2); % supposed to fun the loop and store outputs in array q
disp(q) %shows array q to see if it works

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!