script for looping function

2 次查看(过去 30 天)
Gregory Power
Gregory Power 2019-3-4
编辑: Stephan 2019-3-4
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 个评论
Gregory Power
Gregory Power 2019-3-4
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

请先登录,再进行评论。

回答(2 个)

Stephan
Stephan 2019-3-4
编辑:Stephan 2019-3-4
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 2019-3-4
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by