Create a loop for each parameter in the magnitude vector


1) Write a function, called SuperPos,m, that will take four variables as arguments and return two output values. The first will be a time row-vector, t, the second a magnitude vector, mag, the third a phase vector, phase, and the fourth, a string vector, waveFormType, containing either 'c' or 's'. The last three vectors are required to be the same size.

The function should return a vector, y that is the same size as t, and return a vector, T, that is exactly t.

You must add appropriate comments at the beginning of your function (Description, Inputs, Outputs, written by, and date). See the example in the MATLAB introduction provided.

Define a function variable: freq = 10; This parameter will be used within the function, but not passed into or out of the function.

Allocate the memory for a (m x n,) matrix, Y, where m is the # of parameters in mag, and n is the number of parameters in t. i.e. Y = zeros(m, n); You will need to determine m & n using the MATLAB length command.

2) Write a vector For-Loop that does the following.

- Before the for-loop, create a figure, and make sure MATLAB holds the plot. flgumjgacton;

- Create a loop for each parameter in the magnitude vector. i.e. "for ii = 1:length(mag)"

- Calculate the required phase using a if-then-else condition check. i.e. "if waveFormType(ii) == 's' then phi = phase(ii)-pi/2;
"else" phi=phase(ii); Do not forget to structure this if-then-else properly. If you are having trouble, do a "help if" at the MATLAB command line prompt.

Comment on what "ii" is doing in this section and how the if-then-else loop works.

- Now calculate ith row vector of Y, by taking mag(ii), the phi you determined previously, the passed parameter t, the 2*pi*freq.

Hint: You will need to use either y(ii,:)= or Y(:,ii)=,

Comment on what Y(ii,:).= and Y(:,ii)= expect for a calculated parameter (specifically: how many rows and columns does each want)

- Plot your newly calculated row-vector versus t. Make sure each new row-vector plot is on the same graph.
- end yag,for-loop properly.
- Finally, label the x-axis, y-axis, include a title, and your full name using the text command.

Copy your figure to your worksheet.

3) Next, calculate the superposition of each row-vector. You can do this with another for loop, but that is a bad way to do it. MATLAB is a matrix based programming language and all of its commands are written to work on matrices.

Do a "help sum" at the MATLAB command line prompt. Follow the instructions to have the sum command sum down the column of your Y matrix and equate this sum to the return parameter y. i.e. y.,s,m,(,Y ...);, where the ... are what you need to figure out.

Finally, equate T=t for the last return parameter and ensure your function ends properly

4) Call this function from the Lab1 M-File using the following parameters. Note: It is the location of the variable name in the function, and not the name that matters when passing parameters to a function.

tt: row-vector starting at 0, ending at 0.5, and having a step size of 0.003. MM = [1; 0.5; 3]; %The magnitude values Phi = [pi/4; pi/3; 0]; % The phase values Sinusoid = ['s';'c','s'l; %s is a sin function, c is a cos function. Spaces in the string matter, because the % parameters must be the same length on each row!

For the return parameters, use r and then p. These are bad names, because they don't mean anything and not good programming technique, but they make the point about the names being independent.

[r, p] = SuperPos(tt, MM, Phi, Sinusoid);

Request for Solution File

Ask an Expert for Answer!!
MATLAB Programming: Create a loop for each parameter in the magnitude vector
Reference No:- TGS02152467

Expected delivery within 24 Hours