Please tell me what I am doing wrong here.

1 view (last 30 days)
We want to compute the following:
where a is such that 0 <a<1 . Note that the frequency of each term is o kf , an integer times some “fundamental” frequency of . The phases will be random and uniformly distributed between −π and π .
Note that since 0 1 < < a , the exponential k a gets smaller as integer k grows large so eventually the terms will be negligible for large enough k. When we have computed K terms already define
Suppose we wish to terminate our computation after hitting k K= such that for some user specified value δ > 0 . When that condition is met we terminate so our result includes only K terms. Obviously, the smaller we set δ the more terms will be included and the more accurate will be our final computation. Also, if we set δ too small we will have an excessive number of terms to compute and the program may run excessively long – so the user might want to be able to set a maximum number of terms. Also, if you set the value of δ too high you might not include any terms at all, which should be avoided – you should be able to determine a limit on δ to prevent that from happening.
Write a MATLAB function called Sines_Sum_Inf.m.m to compute a sum of infinitely many sinusoids as specified in the equation (2) above. Note that the function can only compute finitely many terms so it must determine when it is sufficient to stop the computation!
Function Format:
• Inputs should not be via prompts – rather use input arguments
• Outputs should not printed out – rather use output arguments
Function Inputs:
• Scalar a, whose value must be 0 < a < 1
• Scalar fo, whose value must be non-negative
• Vector t, whose elements hold the start and stop time for the range of time over which the function is to be computed
• Scalar delta, used to specify stopping criteria as specified
• Scalar N, used to specify a maximum number of terms – as a safety to prevent the program from looping for an excessively long time.
Function Outputs:
• Vector y that holds the computed values of the function
• Vector t that holds the values of time at which the function is computed
• Scalar K, that indicates how many terms were included in the result
Other Function Requirements:
• The phases should be computed using the rand function
• Check inputs to ensure their sizes are valid… Give user an error if not
• Check to be sure that the a value is between 0 and 1
• Check to be sure that the N value is positive integer
• Check that delta is positive and is not too big
• Program should provide a warning when the loop reaches its maximum number of iterations.
Function Testing:
• Perform tests to verify all error and warning checks are working as desired
• Verify by hand the computed solution for a small size problem
• Plot the result of y vs t for a few scenarios chosen to explore the range of behavior
------------------------------------------------------------------------------------------------------------------
This is what I have done up until now
function [y,t,K] = Sines_Sum_Inf(a,fo,t,delta,N)
i=0;
if ((a<0) | (a>1))
error("Please make sure that amplitude a is 0<a<1, and re-run the function")
elseif (fo < 0)
error("Please make sure the frequency fo is non-negative")
elseif (N < 0)
error("Please make sure the N is positive")
elseif ((delta < 0) | (delta > 10))
error("Please make sure delta is not negative or too big")
elseif ((length(a)==N) & (fo > 0) & (length(fo)==N) & (length(t)==N))
phi = rand(1,N);
Ak = sum(a);
for j = a(1):a(length(a))
if (delta > Ak/a(i))
K=N;
continue
else
error("a does not satisfy the condition delta > Ak/a")
end
end
while (i <= N)
y = a(i)^K * sin(2*pi*K*fo(i)*t(i) + phi(i));
end
else
disp("Please make sure the sizes of a, fo, and t are equal to N and fo > 0 ")
end
end
This gives me the error
>> Sines_Sum_Inf(a,fo,t,d,N)
Array indices must be positive integers or logical values.
Error in Sines_Sum_Inf (line 16)
if (delta > Ak/a(i))
The input is :
a=1:10;
fo = 0.5:0.5:5;
t=0:2:19
delta = 6;
N=10;
  9 Comments
Travis Heckler
Travis Heckler on 27 Jul 2019
'Is a a scalar or a vector?'
It is a vector.
Walter Roberson
Walter Roberson on 27 Jul 2019
In MATLAB if v is a vector then
if v < 1
means the same thing as
if all(v < 1)
Therefore your range test would only reject the input if every entry was either negative or greater than 1, and if even one entry was in range then it would accept the input as valid. Are you certain that is what you want to test?

Sign in to comment.

Answers (1)

Travis Heckler
Travis Heckler on 27 Jul 2019
Edited: Travis Heckler on 27 Jul 2019
function [y,t,K] = Sines_Sum_Inf(a,fo,t,delta,N)
i=1;
j=1;
if ((a<0) | (a>1))
error("Please make sure that amplitude a is 0<a<1, and re-run the function")
elseif (fo < 0)
error("Please make sure the frequency fo is non-negative")
elseif (N < 0)
error("Please make sure the N is positive")
elseif ((delta < 0) | (delta > 10))
error("Please make sure delta is not negative or too big")
elseif ((length(a)==N) & (fo > 0) & (length(fo)==N) & (length(t)==N))
phi = rand(1,N);
Ak = sum(a);
while j < length(a)
if (a(j)/Ak < delta)
K=N;
j = j + diff(a);
else
error("a does not satisfy the condition delta > a/Ak")
end
end
while (i <= N)
y = a(i)^K * sin(2*pi*K*fo(i)*t(i) + phi(i));
i=i+ diff(N);
end
else
disp("Please make sure the sizes of a, fo, and t are equal to N and fo > 0 ")
end
end
This works

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!