Please tell me what I am doing wrong here.
Mostrar comentarios más antiguos
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.
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 comentarios
John D'Errico
el 27 de Jul. de 2019
What does this mean:
0 1 < < a
????
Is it some jargon of yours that suggests a must lie between 0 and 1? That seems to be the case, since you test for a<0 or a>1.
Travis Heckler
el 27 de Jul. de 2019
dpb
el 27 de Jul. de 2019
Removing the error checking code to reduce clutter, one has--
...
i=0;
...
phi = rand(1,N);
Ak = sum(a);
for j = a(1):a(length(a))
if (delta > Ak/a(i))
K=N;
continue
else
...
What's the first value for i?
Matlab arrays are and must be one-based.
Travis Heckler
el 27 de Jul. de 2019
Walter Roberson
el 27 de Jul. de 2019
while (i <= N)
y = a(i)^K * sin(2*pi*K*fo(i)*t(i) + phi(i));
end
The body of the while loop does not change i or N so if you ever enter the loop you would never get out of it.
Walter Roberson
el 27 de Jul. de 2019
Is a a scalar or a vector? You should read about any() and all()
You should also read about end
dpb
el 27 de Jul. de 2019
"I changed it to 1, still does not work"
Well, it will certainly have fixed the specific error...I never claimed to have debugged your entire program... :)
Travis Heckler
el 27 de Jul. de 2019
Walter Roberson
el 27 de Jul. de 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?
Respuestas (1)
Travis Heckler
el 27 de Jul. de 2019
Editada: Travis Heckler
el 27 de Jul. de 2019
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!