$24
PURPOSE: Learn about Conditional and Iterative Statements (if-end, for-end)
For each problem, create a MATLAB script file and name it FIRSTNAME_LASTNAME_LAB6_ problemX.m. Put ALL the commands for the required steps in your script file:
Be sure to clear the display and the memory.
Display your name.
Separate and label different steps using comments.
---------------------------------------------------------------------------------------------------------------------
%{
Class: ENGR15100: Software Tools for Engineers
Instructor: Xiaoli Yang
Author: [Student’s Name]
Assignment: Lab [No.]
File Name: LASTNAME_LAB[No.]_problem[No.].m
Date: [MM]/[DD]/[YY]
%}
%clear screen
clc
%clear workspace
clear
disp('Your Name Here');
disp('');
disp('starting code: ');
%Completing lab x
%your source code here%
---------------------------------------------------------------------------------------------------------------------
problem 1: Analyzing an Array (30 points)
Write a program that performs the following steps. Unless otherwise specified, suppress output to the Command Window.
Create a variable named V and assign to it a 250-element row vector whose elements are each randomly generated real numbers chosen uniformly from the open interval (-4.5, 5.5).
Create variables numNegative, numPositive, sumPositive, meanPositive, and prodRange. Initialize each variable to an appropriate scalar value.
numNegative, numPositive: number of negative and positive elements in V, respectively
sumPositive, meanPositive: sum of all the positive elements in V, respectively.
prodRange: the product of all real numbers in vector V in the range [2.3, 2.6]
Declare a for-end statement using a loop variable named k that will be assigned to every element of a row vector whose elements represent the indices/positions of row vector V.
The body of the for-end statement should update, when applicable, the values of numNegative, numPositive, sumPositive, and prodRange. The use of built in functions sum, mean, and prod is NOT allowed.
After the for-end statement, compute the mean of the positive real numbers in vector V and assign the mean to a variable named meanPositive.
Using multiple instances of fprintf, display the values contained in variables numNegative, numPositive, sumPositive, meanPositive, and prodRange, respectively.
Format variables numNegative and numPositive as integers.
Format variables sumPositive, meanPositive and prodRange as fixed-point real numbers, each showing a maximum of 3 digits after the decimal point.
FIRSTNAME_LASTNAME_LAB6_ problem1
Vector V has 119 negative elements.
Vector V has 131 positive elements.
The sum of vector V's positive elements is 363.659.
The mean of vector V's positive elements is 2.776.
The product of the elements in the range [2.3, 2.6] is 229.376.
The result of executing your program should look similar to the sample output shown below.
problem 2: Maximum of a Function in an Interval (30 points)
Write a MATLAB program that finds the maximum value of function (𝑥) = (𝑥 + 1)3(𝑥 − 1)(𝑥 − 2) in the interval 𝑥 = [−2, +2]. The program also finds the value of 𝑥 in the interval that caused the maximum (𝑥) to occur. Unless otherwise specified, suppress output to the Command Window.
Create variables xMax and yMax. Initialize each variable to an appropriate scalar value.
Declare a for-end statement with a loop variable named x that will be assigned to every element of a row vector whose elements are equally spaced values starting with -2.0, ending with +2.0, with a step size of 0.01.
The body of the for-end statement should perform the following:
Compute the current scalar value of a variable y in terms of the current scalar value of loop variable x according to the function (𝑥) = (𝑥 + 1)3(𝑥 − 1)(𝑥 − 2).
When appropriate, update the scalar values of xMax and yMax. You may not use any built in functions, including built-in function max.
Using multiple instances of the built-in fprintf function, display the values of yMax and xMax according to the following formatting specifications:
Format the value of yMax as a fixed-point real number.
Format the value of xMax as a fixed-point real number.
FIRSTNAME_LASTNAME_LAB6_ problem2
The maximum value of y is 2.640522.
The value of x causing the maximum value of y is 0.370000.
After completing the above steps, the result of executing the script should look like the output below.
problem 3: Modifying and Formatted Vector Output (40 points)
FIRSTNAME_LASTNAME_LAB6_ problem3
Enter the number of elements for a vector v: 10
The elements of vector v are:
----------------------------
12 60 23 40
12 45 19 49
52 56
V contains 2 prime numbers.
V contains 1 multiples of 3 in the range (25, 55).
After updating the multiples of 3, V now contains: --------------------------------------------------
-2 -4 23 40
-10 -12 19 49
52 56
Write a program according to the following specifications. Unless specified, suppress all MATLAB Command Window output. After completing the program, the result of executing your program should look similar to the output shown below.
Create a variable named N and assign to it an integer obtained by prompting the user to enter the size of a row vector V. Assume the user always enters a positive integer.
Create a variable named V and assign to it an N-element row vector whose elements are randomly generated integers uniformly chosen from the closed interval [0, 75].
Declare another for-end statement with a loop-variable named k that will be assigned to every element of a row vector whose elements represent the indices/positions of row vector V. In the body of this for-end statement, perform the following:
Count the number of prime numbers contained in row vector V. Use the built-in function isprime.
Count the number of elements of row vector V that are multiples of 3 (i.e. evenly divisible by 3), greater than 25, but less than 55.
Multiply (and update) each of the elements in vector V that are only multiples of 3 by two times the negative of their index/position within row vector V.
Using multiple instances of the built-in fprintf function, display the following:
Number of prime numbers contained in V. Format the number as an integer.
Number of multiples of 3 contained in V in the range (25, 55). Format the number as an integer.
Repeat step (d) to display all the elements contained in row vector V. Re-use as much of the code as possible that was as part of completing step (d).
Test your program with at least the following scalar values for N: +1, +10, and +24.
SUBMITTING YOUR LAB:
Submit your lab by uploading .m file using the Blackboard Assignment feature no later than the date specified.