Please help me out with this question, I am getting an error, I have included my code in the bottom
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Write a MATLAB function called Sines_Sum_N.m to compute a sum of N sinusoids as specified in the equation
Function Format:
• Be sure to use proper style and format as discussed in previous labs and the notes.
• Inputs should not be via prompts – rather use input arguments
• Outputs should not printed out – rather use output arguments
Function Inputs:
• Vector a, whose elements hold the values a1,a2,a3....aN
• Vector f, whose elements hold the values f1,f2,f3....fN
• Vector phi, whose elements hold the values φ1,φ2,φ2....φN
• Vector t, whose elements hold the start and stop time for the range of time over which the function is to be computed
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 Other
Function Requirements:
• Check inputs to ensure their sizes are valid… Give user an error if not
• Check the inputs to be sure that all input amplitudes are non-negative
• Check the inputs to be sure that all input frequencies are non-negative
• Follow the style guidelines discussed in the notes and previous labs for commenting your function
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 (Obviously you can’t check it at all points but you should check a few points)
• Run the program several times
• Try a variety of number of stages
• Try different values for the resistors
I have tried this but it gives out an error saying array indices must be positive integers or logical values
function [y,t] = Sines_Sum_N(a,f,phi,t)
del_t = t(length(t)) - (t(length(t)-1));
max_t = t(length(t));
y = 0;
i=1;
if (a<0)
error("Please use positive amplitudes and try again !")
elseif (f<0)
error("Please use positive frequencies and try again !")
else
while (i <= max_t)
y = y + (a(i) * sin(2*pi*f(i)*t(i) + phi(i)));
i = i + del_t;
end
end
t = 1:0.5:10;
a = [1, 2, 3 ,4, 5];
f = [0.01,0.02,0.03,0.04,0.05];
phi = [1, 1.5, 2, 2.5, 6];
2 comentarios
Rik
el 27 de Jul. de 2019
Your t variable has a different number of elements as the others. You didn't check the input argument sizes, which is one of the requirements.
Respuestas (1)
Travis Heckler
el 27 de Jul. de 2019
2 comentarios
Walter Roberson
el 27 de Jul. de 2019
Only when all of the input times are integers. You are misusing indexing with i.
Rik
el 27 de Jul. de 2019
You should consider writing your function in a structured way: start with the function name and the one line description of what it should do, then describe the inputs (allowed data types and sizes), then describe the output (data types and sizes). Only then start writing code. The first code should check your inputs, to make sure everything is as you expected.
Then you can write your actual function. At this point you can assume every input is as you expect.
By using this structure you make sure that you don't miss anything.
Also, the length function is almost never the correct choice, either the size or the numel function are usually better picks.
You also forget to write any comments, which generally makes it more difficult to understand your function. That is not just important for getting a good grade, it also allows other people to read and understand what happens. Remember that future you is a different person as well.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!