Not sure how to fix this error please assist
t=0:0.1:1000
ao=0.504
for n=1:1000
an=0.504*(2/(1+16*(n^2)));
bn=0.504*((8*n)/(1+16*(n^2)));
f(t)=ao + (an*cos(2*n*t))+(bn*sin(2*n*t))
end
plot(t,f(t));
here is my error
Subscript indices must either be real positive integers or logicals.
Error in asda (line 6)
f(t)=ao + (an*cos(2*n*t))+(bn*sin(2*n*t))

답변 (1개)

Star Strider
Star Strider 2018년 4월 12일

0 개 추천

First, MATLAB requires that subscripts (and indices) be integers greater than zero, so using ‘t’ as an index variable will not work.
Second, your ‘f’ calculation is vectorised at each iteration, producing a (1x10001) row vector. If you want to calculate and store all of them for each iteration of ‘n’, you have to account for that in your indexing.
Try this:
f(n,:)=ao + (an*cos(2*n*t))+(bn*sin(2*n*t));
with the plot call then being simply:
plot(t,f)

댓글 수: 4

Christopher Carey
Christopher Carey 2018년 4월 12일
this doesn't seem to give me the desired signal
Star Strider
Star Strider 2018년 4월 12일
I only addressed the problem you asked for help with.
This appears to be an inverse Fourier transform. Consider changing the ‘f’ calculation by not adding ‘ao’ at each step, then calculating the sum of the ‘f’ matrix at the end, and adding ‘ao’ to that result.
Check the expression for the inverse Fourier transform, since you may have omitted some necessary details in your code.
Christopher Carey
Christopher Carey 2018년 4월 12일
I can show you the desired signal if wish to look and see if my code corresponds to that signal or now
You did not define ‘t’ correctly.
Try this:
t = linspace(0, pi, 250);
as well as my earlier suggestions. You might want to extend ‘t’ for a few cycles to see that the waveform repeats periodically, as it should. I will let you experiment with that.

댓글을 달려면 로그인하십시오.

질문:

2018년 4월 12일

댓글:

2018년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by