## Transcribed Text

Problem #1
MATLAB has a built-in primes function to generate prime numbers (look it up). If you supply it
with an integer input argument N, it will return a vector containing all the prime numbers less
than or equal to N.
Requirements – Part 1 (for version)
Write a MATLAB script that satisfies the following:
• Prompt the user for the desired value of N.
• Use the primes function to create a vector containing all the primes ≤ N.
• Use a for loop to multiply adjacent values together and store the results in a new vector.
Note: Within the loop, use array indexing to access the correct pairs of numbers.
Example:
If N is 7, the prime number vector would contain: 2 3 5 7
Your calculation would be: 2*3 3*5 5*7
The elements of the new vector are: 6 15 35
• Display both the original vector of primes and your calculated vector of products.
• Save your script using this name: hw6p1f.m
Requirements – Part 2 (while version)
Write a MATLAB script that satisfies the following:
• Replace the for loop from Part 1 with an equivalent while loop.
• Save your script using this name: hw6p1w.m
Sample Run (at the Command Window)
>>> hw6p1f
Enter desired value of N: 12
Original prime vector:
2 3 5 7 11
Adjacent products vector:
6 15 35 77
Problem #2
Calculating a table of values is a common programming task, which is made easier by using an
appropriate loop.
Note: MATLAB does have element-by-element operators that can do the job more simply and
quickly in many cases, but you will do it the “old-fashioned” way by using loops.
Requirements
Write a a MATLAB script that meets these specifications:
• Use a loop to iterate over the variable x to compute and display x, sin(x), and cos(x).
• x should range from 0 to 3 in steps of 0.2.
• You may use either a for loop or a while loop.
• The results should be displayed in a nice table format with column labels.
• Show the output values to four decimal places.
• Save your script using this name: hw6p2.m
Sample Run (at the Command Window)
>>> hw6p2
x sin(x) cos(x)
0.0000 0.0000 1.0000
0.2000 0.1987 0.9801
0.4000 0.3894 0.9211
0.6000 0.5646 0.8253
0.8000 0.7174 0.6967
1.0000 0.8415 0.5403
1.2000 0.9320 0.3624
1.4000 0.9854 0.1700
1.6000 0.9996 -0.0292
1.8000 0.9738 -0.2272
2.0000 0.9093 -0.4161
2.2000 0.8085 -0.5885
2.4000 0.6755 -0.7374
2.6000 0.5155 -0.8569
2.8000 0.3350 -0.9422
3.0000 0.1411 -0.9900
Problem #3
By now, you should all be familiar with the linear regression equations for fitting a line through a
set of empirical data points that supposedly have a linear relationship:
Slope:
( ) ( )( )
( ) ( )2 2
∑∑
∑ ∑ ∑
−
− =
k k
kk k k
xxn
yxyxn
m
y-intercept:
( )
n
xmy b ∑ k − ∑ k =
Correlation coefficient:
( ) ( )( )
( ) ( ) ( ) ( )2 2 2 2
∑∑∑∑
∑ ∑ ∑
− −
− =
k k k k
kk k k
yynxxn
yxyxn
r
k is the summation index which refers to the k-th point in a data set.
Requirements
Write a MATLAB script that satisfies the following:
• Prompt the user to enter a vector of x values.
• Prompt the user to enter a vector of y values.
• Use a for loop to compute the various sums required by m, b, and r.
• Calculate and then display the slope m, y-intercept b, and correlation coefficient r.
• Do not use the built-in MATLAB linear regression function or sum function to do this!
• Save your script using this name: hw6p3.m
Sample Run (at the Command Window)
>>> hw6p3
Enter x values as a vector: [1.0 1.5 2.0 2.5 3.0]
Enter y values as a vector: [-0.6 1.1 2.0 3.7 4.2]
Slope m = 2.44
y-intercept b = -2.8
Correlation coefficient r = 0.987345
Problem #4
One of MATLAB’s neatest features is element-by-element operators for manipulating vectors and
matrices. However, suppose that MATLAB did not support this natively. Could you duplicate this
desirable feature using loops? For this problem, you only need to handle addition, subtraction,
multiplication, and division of compatible matrices.
Requirements
Write a single MATLAB function named ele that satisfies the following:
• Accept three input arguments. The first two are the matrices that will be processed. The third
is a character that indicates the desired operation ('+', '-', '*', '/'). The single output
argument will contain the result of the operation.
• Check if the input matrices have compatible dimensions. If not, the function should print an
error message and return an empty matrix.
• If the operator is not recognized (i.e., not one of the characters defined above), then the
function should print an error message and return an empty matrix.
• Perform the requested element-by-element operation on the two input matrices and return the
result.
• Include an H1 help line
• Save your function file using this name: ele.m
Problem #5
Remember how you were taught to add numbers when you were a young child? It probably
looked something like this:
You will try to duplicate the process of adding individual digits and keeping track of the carrys.
Requirements
Write a MATLAB script that meets these specifications:
• Prompt the user to enter the first number using the input function. However, store it as a
string, and not as a numeric value. Do the same for the second number.
• Convert the digit characters ‘0’ through ‘9’ that are stored in the string (i.e., the array of
characters) into their corresponding numbers 0 through 9.
Hint: If you pass a string variable to the double function (look it up), it will return an array
that contains the equivalent ASCII values. For example, string '825' is transformed to an
array that contains the numbers 56 50 53. What can you do next to convert the ASCII
values to the digits they represent, i.e., 8 2 5 ?
• Use a for loop to access and add the individual digits in the first number’s array to the
corresponding digit in the second number’s array. Your code will have to account for the
one’s carry (if any) after each addition.
• Save your script using this name: hw6p5.m
This is a relatively difficult problem, and you have to think carefully about how to set things up.
To make it a little easier, you are allowed to assume that both of the entered numbers will have
the same amount of digits (i.e., the string lengths are the same).
(Optional) If you want to challenge yourself, figure out a way to make it work if the input values
have a different number of digits.
Sample Run (at the Command Window)
>>> hw6p5
Enter first number : 27109
Enter second number: 23582
sum = 50691
1 2
+ 5
1 7
1 1
7 8 0
+ 4 4 5
1 2 2 5
Carry
Add from right to left
(Least Significant Digit to Most Significant Digit)
Problem #6
Math Primer: Fourier Series Representation of a Square Wave
Figure 1: Square wave
The square wave in Figure 1 can be represented by this infinite Fourier series:
( )
= ∑
∞
= L
xn
π n
xf n
π sin 14
,5,3,1 K
Note: The index n is defined for odd integer values only.
By summing up a series of sine waves in a specific way, the result will converge to the square
wave as n → ∞.
The finite (truncated) Fourier series has the form:
( )
= ∑= L
xn
π n
xf
N
n
N
π sin 14
,5,3,1 K
Where N is a finite, odd integer.
As an example, the trucated series expansions for N = 1, 3, and 5 are shown below:
( ) ( )
( )
=
= L
x
L
x
π
xf π
π
π sin 41 sin
1
14
1
() () ( )
( )
+
=
+=
L
x
L
x
L
x
π
xfxf π π
π
π 3 sin
3
1 sin 43 sin
3
14
3 1
() () ( )
( )
+
+
=
+=
L
x
L
x
L
x
L
x
π
xfxf π π π
π
π 5 sin
5
13 sin
3
1 sin 45 sin
5
14
5 3
The convergence is more clear when plots of f1, f3, and f5 are superimposed on top of the square
wave, as seen in Figure 2. The case of N = 31 (f31) is also placed on the graph for comparison.
Figure 2: Superposition of Fourier expansions on top of square wave
Note: For a waveform with a jump discontinuity (e.g., at x = 0, 1, 2, …), the fourier series
representation exhibits "overshoot" at the discontinuity. This is called the Gibbs phenomenon and
is apparent in the graph for the N = 31 case.
Your Task
The graph in Figure 3 shows the Fourier series expansion of a square wave for N =1, 3, 5 (row 1)
and N = 11, 31, 51 (row 2). The last row shows the superposition of an actual square wave and
the Fourier expansions for N = 1, 3, 5. Note that L = 1 for all of the graphs, with two cycles of the
wave displayed.
Figure 3: Fourier expansion grid
Requirements
Use MATLAB to replicate exactly the content and appearance of Figure 3, which is a 3×3 tiled
graph in a single figure window.
Write a MATLAB function named fourier_sq that satisfies the following:
• The input arguments are the term index N, the value x, and the interval length L. The output
argument is the calculated value of the Fourier series expansion given those input conditions.
• The function must work correctly when x is either a scalar or a vector.
• Include an H1 help line and expanded help text (e.g., a list of required arguments with
descriptions).
• Save your function file using this name: fourier_sq.m
Write a MATLAB script (main program) that satisfies the following:
• The script invokes the fourier_sq function as needed to calculate the Fourier expansions.
• Display the resulting plots on the same graph. Use consistent labeling.
• Save your script using this name: hw6p6.m
Notes:
• Feel free to write additional function files, if it makes your code more efficient.

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

function [result] = ele(a,b,op)

%function ele

%Returns a matrix that contains the sum, difference, product, and quotient

%of each element between two matrices.

%1. Enter the first matrix

%2. Enter the second matrix

%3. Enter the desired operation. The operation must be defined as follows:

%Addition-------Enter: '+'

%Subraction-------Enter: '-'

%Multiplication-------Enter: '*'

%Division-------Enter: '/'

%

%NOTE: The characters must entered as shown above, i.e. the characters

%must be entered as strings.

ask1 = 'Enter the first matrix: ';

a = input(ask1);

ask2 = 'Enter the second matrix: ';

b = input(ask2);

ask3 = 'Enter the desired operation: ';

op = input(ask3);...