Please help me out with this question, I am getting an error, I have included my code in the bottom

1 view (last 30 days)
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];

Answers (1)

Travis Heckler
Travis Heckler on 27 Jul 2019
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;
len_a = length(a);
len_f = length(f);
len_p = length(phi);
len_t = length(t);
if (a<0)
error("Please use positive amplitudes and try again !")
elseif (f<0)
error("Please use positive frequencies and try again !")
elseif ( (len_a == len_f) & (len_f == len_p) & (len_p == len_t) )
while (i <= max_t)
y = y + (a(i) * sin(2*pi*f(i)*t(i) + phi(i)));
i = i + del_t;
end
else
error("Please make sure the vector sizes of the args are all similar")
end
This is the answer to the given question
  2 Comments
Rik
Rik on 27 Jul 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.

Sign in to comment.

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!