필터 지우기
필터 지우기

script for looping function

조회 수: 1 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by