GENERAL GUIDANCE FOR HOMEWORK IN ORDER TO GET FULL CREDIT

Important announcements from your instructor regarding MAE10 Homework

Homework 1

Due: Thursday, October 3rd, 2019 by 01:00 pm

Read: Chapters 1, 2, 3
There are no assigned problems for this homework. However, you should test uploading a single M-File on Canvas in the "Assignments" section under "Homework 1" to avoid any issues with future submissions. The M-File should be named appropriately, following the format/file name convention given on the top of this page. The contents of this M-File should be your name and ID number on line 1, followed by the homework number and problem number on line 2 (both as comments). For example:

% Peter Anteater, ID#12345678
% Homework 1, Problem 1



Homework 2

Due: Thursday, October 10th, 2019. Due by 01:00pm

Read: Chapter 4

Problems: Turn in 5 M-Files for HW2, named appropriately

On your own - Do Problem 2.1 on page 55 to make sure you understand the concept of order of operations. You do not need to turn anything in for this problem.

(1) Ask the user for a number x as input, then tell the user the value of the following items:
sin(x), cos(x), sqrt(x), abs(x), exp(x), log(x), log10(x)


(2) Allow the user to input the base and height of three triangles and store this information in two arrays (a base and height array, each containing three elements). Find the area of all three triangles using element-wise multiplication. Then, tell the user the area of all three triangles.


(3) A cannon is fired on level ground. Allow the user to input the initial total velocity v0 and initial elevation angle theta in degrees (0 degrees = cannon fired horizontally, 90 degrees = cannon fired straight up). Output the x and y position of the cannon ball from 0 seconds to 1 second in increments of 0.1 seconds in a nice table format with three columns. Your output should look similar to the following table:

time(s) x(m) y(m)
0 0 0
0.1000 0.7071 0.6581
0.2000 1.4142 1.2182
(and so on until t = 1.0)

Remember that theta should be in radians for use in trigonometic functions, not degrees. Assume the initial position is (x,y) = (0,0). The vertical acceleration due to gravity is -9.8 m/s2.


(4) Create a table of x, cos(x), and sin(x) where x goes from 0 to 2pi in increments of 0.1pi . The output should be in a three column table, the first column containing the values of x, the second column containing the values of cos(x), the third column containing the values of sin(x).


(5) For the following array of test scores,

[99,58,87,78,100,100,91,93,78,66]

Find the mean, standard deviation, minimum value, maximum value, and total number of scores using MATLAB's built-in functions (such as mean( )) and tell the user these values.

Note: In future problems, I will not allow you to use these built-in functions since they are specific to MATLAB (there is no mean( ) function in Fortran). However, it is important that you know these functions exist and how they operate. If it is unclear whether or not you are allowed to use a built in function in a given problem, please ask.


Homework 3

Due: Tuesday, October 17th, 2019. Due by 01:00 pm

Read: Chapter 8.1-8.4, begin reading Chapter 8.5

Problems: Turn in 6 M-Files for HW3 to the Canvas labeled "MAE10 Homework 3"

(1) Use the following arrays to do problem 1.

a =
1 3 5 9
9 1 1 1
0 0 10 3

b =
1
1
9

c =
10 11 12 9

(1a) Create a 3x1 array d that is the sum of b and the second column of a. The rules for matrix addition are here.

(1b) Create a 1x4 array f that is the sum of c and the first row of a.

(1c) Create a 3x4 array g, where the first element of b is added to every element in the first row of a, the second element of b is added to every element in the second row of a, and the third element of b is added to every element in the third row of a.

(1d) Create a 1x7 array h, where the elements of b make up the first three elements of h and the elements of c make up the last four elements of h.


(2) The ideal gas law is P = rho*R*T, where
P = pressure of gas (Pa)
rho = mass density, (kg/m3)
R = specific gas constant for air = 287.05 (J/(kg*K))
T = temperature (K)

Create a two-dimensional table to determine the density of air at many different values of Pressure and Temperature. Specifically, vary P from 50 kPa to 150 kPa in increments of 10 kPa and vary T from 250 K to 350 K in increments of 10 K. Vary P in the x-direction and vary T in the y-direction. Your table should be 11x11. You do not need to include the table headings that list the pressure and temperature.
Hint: The meshgrid( ) command may be helpful.


(3) Note: you can use the mean() function for this problem.
A teacher has the following spreadsheet:

Student1 Student2 Student3
Quiz1 99 85 67
Quiz2 90 65 69
Quiz3 98 95 97
Quiz4 80 85 89
Quiz5 98 77 87

(3a) Create a 5x1 array called mean_quiz_scores containing the mean scores from all 5 quizzes.

(3b) Create an 3x1 array called mean_student_scores containing the mean scores for all three students.

(3c) Determine the mean score for all quizzes and students (the mean of all 15 values in the table) by taking the mean of both mean_quiz_scores and mean_student_scores. Did you get the same answer?

(3d) If the average score of Quiz 5 is greater than the average score of Quiz 4, print "Quiz 5 was easy" to the screen.

(3e) There are 15 total quizzes (3 students x 5 quizzes).
- If student 3 had the highest quiz score out of all 15 quizzes, print "Student 3 is my hero" to the screen.
- If he did not have the highest quiz score, print "Student 3 is a zero" to the screen.
(Determine who has the highest individual score using your MATLAB program, don't just read it off the table with your eyes.)


(4) Allow the user to input 3 numbers (A, B, and C). Do the following tasks in this order:
NOTE: It must be possible for all three of the tasks to happen.

- If A is greater than B, switch the two numbers (the value of A now is stored in B and the value of B now is stored in A).
- If B is the largest number, set it to zero. Otherwise, set it equal to 100.
- If C is a negative number, set it equal to zero.
- Print the 3 numbers to the screen.


(5) Allow the user to input their test score.

- If the score is less than 60, inform the user he is kicked out of the class.
- If the score is greater than or equal to 60 and less than 90, inform the user that he is an average student.
- If the score is 90 or greater, inform the user he is a champion.


(6) Allow the user to input their name. Use switch/case to do the following:

- If the user inputs 'Paul' inform the user that he may be the instructor for MAE-10.
- If the user inputs 'John' or 'Bill' inform the user that he has 4 letters in his name.
- If the user inputs 'Susan' ask the user for a number. If the number is greater than 10, print 'Hello!' to the screen. Otherwise, print 'Bye!' to the screen. You may use an if statement for this part of the problem.
- If the user's name is none of the above, then inform the user that he/she should eat a taco.


Homework 4

Due: Thursday, October 31st, 2019. Due by 01:00pm

Read: Chapter 9

Problems: Turn in 4 M-Files for HW4 to the Canvas labeled "MAE10 Homework 4". Do not use MATLAB's built-in functions, such as mean( ), std( ), sum( ), etc, to do the problems (except to check your answer as mentioned below).

(1) Use a for loop to calculate the average and standard deviation of the following array:

[112, 222, 33, 44, 55, 66, 77, 88, 99]

Now, use a while loop to calculate the average and standard deviation. Check your answer with the mean( ) and std( ) functions.

The mathematical formula for the standard deviation can be found here (scroll down to see the forumla for the standard deviation).


(2) The Fibonacci sequence is the following:

1, 1, 2, 3, 5, 8, 13, 21, ...

The sequence starts with 1 and 1, then all subsequent values are determined by summing the previous two numbers in the sequence.

Have your program display the first N Fibonacci numbers in a column, where N is a number you select. Choose a value of N that is greater than 2 (do not use the input function, simply hard-code the value of N by typing N = some number greater than 2). For example, if you choose N=6 the program should display:
1
1
2
3
5
8

Calculate all Fibonacci numbers except the first two (1, 1) using a for loop and display them on the screen. Although you will assign a value to N in your program, make your algorithm general for any value of N > 2.


(3) Make a program that calculates N! where,

N! = N * (N-1) * (N-2) * ... * 3 * 2 * 1

N is a value you choose (as in problem 2, do not use the input function, simply state N = some number). For example, if you choose N=5 your program should calculate 120 and display the result to the screen.

Calculate N! using a for loop first, then recalculate N! using while loop. Did you get the same result? Check your answer with the factorial( ) function.


(4) Revisit the cannon problem from Homework 2.

A cannon is fired on level ground. Assume the user inputs an initial velocity (v0) of 100 m/s and an initial elevation angle of theta = 25 degrees. Using a while loop, calculate the x and y position of the cannon ball in time steps of 0.1 seconds until the cannon ball hits the ground. Display the time, x-position and y-position in a nice table format with three columns. (Your output should look similar to the table from HW2, problem 3)

Remember that trigonometric functions require input in radians, not degrees. Assume the initial position is (x,y) = (0,0). The vertical acceleration due to gravity is -9.8 m/s2.

Can you think of how to do this problem with a for loop? (not required)


Homework 5

Due: Thursday, November 7th, 2019. Due by 01:00pm

Read: Chapter 7.1-7.2, 7.5

Problems: Turn in 3 M-Files for HW5 to the Canvas labeled "MAE10 Homework 5". You MAY use MATLAB's built-in functions, such as mean( ), to do the problems.


(1) (Problem 7.9 from the book) Use fprintf( ) and a for loop to create a multiplication table from 1 to 13 for the number 6. Your table should look like this:

  1 times 6 is   6
  2 times 6 is 12
  3 times 6 is 18
<4x6 to 12x6 prints here>
13 times 6 is 78


(2) At 9:23pm PST on February 2nd, 2010, the US dollar was worth:

90.5141202 Japanese yen
00.7159221 Euros
00.6250000 British pounds
01.0569998 Canadian dollars

Create a conversion table of US dollars to the four other currencies listed above. The left column of the table will list the US dollar amount from $10 to $100 in step sizes of $10. The four other columns will list the corresponding amount of yen, euros, pounds, and Candian dollars.

Use the fprintf( ) command to display the currency values on the screen and provide column headings ("USD", "YEN", etc.). All the currencies should be accurate to two decimal places. The first three rows should look similar to this (though you will not be printing in bold):

USD   YEN   EURO BP   CAD
10.00     905.14     7.16   6.25   10.57
20.00   1810.28   14.32 12.50   21.14
30.00   2715.42   21.48 18.75   31.71

Next, use fprintf( ) to write the data in this table in scientific notation (with the same precision as above) to a text file called currency.txt. Do not submit the currency.txt file to the Canvas.


(3) Download this text file containing monthly rainfall totals for the fictional city of Octaville during 1988-1991. You can download this file by right clicking the link, choosing "Save link as," and saving the file to your computer. "OctavilleRainfall.txt" must be in the same directory as your M-File when you execute your program. Do not submit the "OctavilleRainfall.txt" file to the Canvas.

Read in this data using the fscanf( ) command and write code to answer the following questions (You are allowed to use built-in functions):

(3a) Using fprintf( ) create a two column table displaying the year and the total rainfall for each year accurate to one decimal place.

(3b) Using fprintf( ) create a two column table displaying the month (1-12) and the average rainfall for each month accurate to one decimal place.

(3c) Which month (1-12) had the highest average rainfall? Which year had the highest total rainfall? Do not read this data off the table. You can create your own algorithm to determine this.


Homework 6

Due: Thursday, November 21st, 2019. Due by 01:00 pm

Read: Chapter 6

Problems: Turn in 3 M-Files for HW6 to the Canvas labeled "MAE10 Homework 6".
This assignment consists of three problems. For each problem, you are given a section of code (the "main program") that calls another function in the same M-File. You must create the function that is called by the main program on your own, but turn in both the main program (given) and the function that you write in the same M-File (you only turn in one M-File per problem). Thus, for each problem you will need to put both the main program and the function you create in the same M-File.


(1) Create a function called intpoly( ) that can numerically integrate the polynomial function y = ax2 + bx + c from a lower limit to an upper limit, where a, b, and c are constants. Your numerical integrator must allow the user to use the
Trapezoidal method or Riemann sums method depending on the choice of the user. If the value of choice is 1, the Riemann sums method will be used to numerically integrate the polynomial (you may use either a left, right, or midpoint Riemann sums method). If the value of choice is 2, the Trapezoidal method will be used to numerically integrate the polynomial.

The main program that calls the function intpoly( ) has the following form:

% NAME, ID#
% Homework 6, Problem 1
% numerically integrate y(x) = a*x^2 + b*x + c using the given
% coefficients in the range defined by lowerlimit and upperlimit
clear; clc;
% define given parameters and number of discretizations
a = 2;
b = 2;
c = 1;
lowerlimit = 0;
upperlimit = 1;
N = 1000;
% choice: 1 = Reimann, 2 = Trapezoidal
% be sure to test both values of choice
choice = 1;
% call the function and display the output
fprintf('Using method %i, the area is: ',choice)
disp( intpoly(a,b,c,lowerlimit,upperlimit,N,choice) )


The arguments passed down to intpoly( ) are the polynomial coefficients (a,b,c), the lower and upper limits of integration (lowerlimit,upperlimit), the number of discretizations (N), and the method of integration (choice). The area is returned to the main program.


(2) Create a function called cannon( ) that calculates the maximum height of a cannon ball's trajectory, the time at which this occurs, and the speed at that time. These three values are returned to the main program, which has the following form:

% NAME, ID#
% Homework 6, Problem 2
% This program will give the approximate maximum height, the time
% at the maximum height, and the speed at the maximum height of a
% cannon ball's trajectory
clear; clc;
% define constants and parameters
x0 = 0;
y0 = 0;
v0 = 20; % (m/s)
angle = 45; % degrees
% call the function and print the results
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f meters \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f m/s \n' , speed)

The arguments passed down to cannon( ) are the initial x- and y-coordinates (x0,y0), the initial speed (v0), and elevation angle (angle). This problem must be solved using a time step and while loop (i.e. similar to Homework 4 Problem 4 - you are not allowed to solve "directly" for the maximum height and the time at which it occurs using those physics formulas). Additionally, your while loop should stop when you reach the maximum height (do not calculate the entire trajectory then find the maximum height).


(3) A magic square of order n is an arrangement of n2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. Create a function called magicsq( ) that creates a magic square of order n, where n is an odd integer greater than or equal to 3. The function will return as output an nxn array to the main program. You may NOT use the built-in function magic( ) for this problem. You must create your own algorithm in the function magicsq( ).

The methodology for creating an odd magic square of order n can be found here.

The main program that calls the function magicsq( ) is the following:

% NAME, ID#
% Homework 6, Problem 3
% This program will create an nxn magic square for any odd n>=3
clear; clc;
n=7; % test several odd values of n
magicarray = magicsq(n)


Homework 7

Due: Thursday, December 5th, 2019. Due by 01:00pm

Read: Chapter 5

Problems: Turn in 3 M-Files for HW7 to the Canvas labeled "MAE10 Homework 7".
This assignment consists of three problems. For each problem, you are given a section of code (the "main program") that calls another function in the same M-File. You must create the function that is called by the main program on your own, but turn in both the main program (given) and the function that you write in the same M-File (you only turn in one M-File per problem). Thus, for each problem you will need to put both the main program and the function you create in the same M-File.


(1) The quadratic formula allows us to find the roots of a quadratic equation of the form y = ax2 + bx + c, where a, b, and c are constants.

Create a function called quadform( ) that takes as input the coefficients of the quadratic equation (a,b,c) and returns the two distinct real roots (x1,x2) as output, if they exist. In addition, the function returns a message (flag) that informs the user if an error occurred when trying to find two distinct real roots. The possible errors are: (1) "Only one root is found"; (2) "Imaginary roots are found"; (3) "Any value of x is a solution"; (4) "No zeroes exist." If no errors occur, then flag should be "No errors".

NOTE: Case (2) and case (4) are not the same condition. Also, MATLAB can handle imaginary numbers, so values for x1 and x2 can still be calculated in case (2).

The main program that calls the function quadform( ) is the following:

% NAME, ID#
% Homework 7, Problem 1
% this program will find the roots of a quadratic equation
% if they exist
clear; clc;
% define parameters passed to the function quadform
% be sure to change these to test all conditions
a = 1;
b = 2;
c = -1;
% call the function and get the results
[x1,x2,flag] = quadform(a,b,c)


(2) A prime number N is an integer that is only divisible by 1 and itself (N). Create a function called primey( ) that takes as input an integer N and returns as output two items:

(Item 1) The answer to the question, "Is N prime?". If N is a prime number, the function primey( ) returns 'Yes' to the main program and stores it in a variable called answer. If N is not a prime number, the function primey( ) returns 'No' to the main program and stores it in answer.

(Item 2) The prime factors of N, which are stored in the array primefactors in the main program. For example, if N is 12, the numbers 2, 2, and 3 will be stored in primefactors. If N is 39, the numbers 3 and 13 will be stored in primefactors. If N is a prime number, the numbers 1 and N will be stored in primefactors.

You may NOT use the built-in functions factor( ) or isprime( ) (except to check your answer).

The main program that calls the function primey( ) is the following:

% NAME, ID#
% Homework 7, Problem 2
% this program will determine if an integer is prime and determine
% its prime factors
clear; clc;
% define number(s) to be checked, call function, and print output
N = 15485863;
[answer,primefactors] = primey(N);
fprintf('Is %i a prime? %s \n', N, answer)
fprintf('The primefactors of %i are: \n' , N)
fprintf('%i \n' , primefactors)
N = 12345678;
[answer,primefactors] = primey(N);
fprintf('Is %i a prime? %s \n', N, answer)
fprintf('The primefactors of %i are: \n' , N)
fprintf('%i \n' , primefactors)


(3) One method for finding a root of a function is the bisection method. Write a function bisect( ) that finds the root of a polynomial y = ax3 + bx2 + cx + d over an interval [A,B]. The function takes as input the coefficients a, b, c, d, and the interval limits A and B. The function returns as output the value of the root over the interval [A,B], if it exists. The function also returns as output a message (flag) that informs the user if no roots exist over the interval [A,B].

The main program that calls the function bisect( ) is the following:

% NAME, ID#
% Homework 7, Problem 3
% this program will use the bisection method to find the root of a
% polynomial y = ax^3 + bx^2 + cx + d over an interval [A,B]
clear; clc;
% define given coefficients and interval
a = -1;
b = 1;
c = -1;
d = 10;
A = 0;
B = 5;
[root,flag] = bisect(a,b,c,d,A,B)


Homework 8

Due: Thursday, December 5th, 2019. Due by 01:00pm

Read: Chapter 11

Problems: Turn in 3 M-Files for HW8 to the Canvas labeled "MAE10 Homework 8".

(1) There are two functions,

w = t - 1
y = t2 + 2

Plot these two functions from t = -1 second to t = 1 second with a step size of 0.1 second in the following way:

(Figure 1) Use a single plot command to plot both functions on Figure 1. The line for w(t) should be solid in the color red with circles at the data points. The line for y(t) should be dashed in the color blue with no special marks at the data points. Include a legend for w(t) and y(t) in the southeast location. Be sure to label the axes and give a title to the figure.

(Figure 2) Use two separate plot commands to plot both functions on Figure 2. There is no line for w(t), but there are green asterisks (*) at the data points. The line for y(t) should be dotted in the color black with x's at the data points. Include a legend for w(t) and y(t) in the "best" location. Make the axis labels size 14 and the title size 18 for this figure.


(2) You are a college professor and your students received the following scores on an exam:

78, 98, 77, 88, 43, 56, 76, 81, 19, 90, 79, 84, 86, 99, 100, 100, 94, 91, 34, 54, 65, 66, 63, 34, 59, 60, 67, 92, 84, 79, 61, 64, 61, 79, 87, 82, 91, 90

Plot the scores on a histogram with 5 bins having the ranges 0-20, 20-40, 40-60, 60-80, 80-100.
NOTE: Be sure that your 5 bins line up correctly in the corresponding range (first bin 0-20, second bin 20-40, etc).


(3) Triangle Madness:
This problem will utilize almost every concept you have learned in this course. Read the problem statement here.