Wednesday, September 30, 2015

MATLAB Day 4

Trig Functions



     We get into some more basic functions in MATLAB. These functions are trig functions. We are able to create a vector of numbers and insert these numbers into the function to produces a vector of solutions. some of the basic functions require the input to be in radians instead of degrees, but there are functions in MATLAB that allow use to input degrees. Otherwise, we can also create a command that converts between degrees and radians.


   We show some more basic functions. This shows that MATLAB is able to handle arccos and arcsin. The function is a simple asin( ) or acos( ). To input the degree instead of radians, the command would be sind( ) or cosd( ), where the d tells MATLAB that within the trig function is a number in degrees.


   We explore MATLAB using the trig functions and try to input the cosecant trig function. I, first, make a conversion between degrees and radians, and make this conversion into a variable. Then, I try to find the cosecant of that angle in radians. At first, I spelled out cosecant completely, but MATLAB gave me an error, showing that this command was not in the library.


   I utilize the help command that is in MATLAB to help determine the command used for cosecant. This command is the same as the abbreviation of the trig function. On the other hand, I may also use the function for sine and make that solution over 1, just like the definition.

Applications For Trig Functions




   We, then, use trig functions in MATLAB to create a simple program that would solve for a specific problem in physics that involves forces. We are given the forces acting on a balloon and the direction these forces are pointed. Using Newton's Laws and the trig functions in MATLAB, we are able to break all vectors down to their components and solve for the net force actin on the balloon. There is also some commenting displayed for practice.

Finding Max, Mean, Median, Mode of a Matrix


   We look at functions in MATLAB that allow us to find values within a matrix that are max values. The max command finds the maximum value according to the column. We can also have MATLAB be more specific in the response for max values. We can display the max value and the row that the max value is in. Since the max alue is in reference to the columns, we don't need MATLAB to say which column the max value is in.


   We can find the maximum value of the entire matrix by using the max function within the max function. This finds the max according to columns and the uses those values to find the max within that set. Using the mean function works the same as the max function. It finds the mean, or average, of the values within a column.


   The mode function allows us to find which number occurs the most often in a column. We can also transpose the matrix and solve for the mean median and mode. This requires an apostrophe at the end of the variable for the matrix. Just like the max, we can find the total average of the entire matrix by using the mean function within the mean function.


   Sorting the matrix by the in order from smallest to largest can be done using the sort function. We can also tell MATLAB to sort the columns in order by using the sortrows command.


    MATLAB automatically sorts the columns in ascending order, but we can have MATLAB sort in descending order by including 'descend' after the matrix we are trying to put in order.


   When making a larger matrix, we can manipulate the matrix in the same manner. If we want to put a column in ascending order, we can use the sortrows command. Within the sortrows command, we state the matrix we are trying to sort and the column we want to sort. Just like before, we can choose whether the columns are descending or ascending.

Summary:
   We discussed some of the trig functions available in MATLAB. MATLAB does not seem to be picky on whether we use degrees or radians, as long as we call the right function. We also went over some of the applications of trig functions, such as forces in physics. We finished off by examining some functions that help us find the maximum, the mean, the median, and the mode of a set of numbers in a matrix. We found that these functions are predefined to find the mean, median, mode or max of columns. 


Sunday, September 13, 2015

MATLAB Homework 2


Classwork



   These are the problems that were not able to be done in class and was assigned as homework. It required us to factor, find the GCD, find prime numbers, find the factorial, and find the maximum number of ways to arrange a group.

Problem 1 & 2


   In problem 1, we had to use different functions and compare these functions. When finding the square root, we have three different commands to use. One out of the three commands cannot find the square root of a negative number. This command is the nthroot command. If there was a negative number, the command would display an error. These commands can also handle vector calculations.

Problem 3


   For this problem, more commands were used to divide a vector, find the remainder, find the exponential, and the sin of that vector. The commands used are displayed on the picture.

Problem 1 (4)


   In this problem, the population rate needed to be solved. The givens are initially stated as p_o for initial population, r for the rate at which the population grows or decays, and t for the time the population grows or decays. Next, I input the formula to solve for the current population and labeled this variable as current_population.


   The same idea for the last problem is used for this problem as well, except the variables are adjustable. I used the same variables to represent the same things. I, then, use the input command to allow a user to input the following information needed. The input command is also descriptive on the type of information that it requires.


   This shows the results of the previous commands, once the user inputs the information. At the end, there is a statement that shows how many people there are in the amount of years requested at the rate inputted.

Problem 2 (5)


   For problem 3, a set of numbers were given for heat. Using what we know about heat and the rate at which heat is extracted, the number of A/Cs was determined. I started by stating some of the givens and multiplying some of the givens by the amount of objects that emit heat. I added all of the heat together to get a total of heat entering the home. Then, I calculate the amount of A/Cs needed using the amount of heat an A/C can extract and the total heat going in the home. I finished everything with simple statements that organize the results.


   A similar program as the previous one is implemented. The difference is that now the user can input values for the amount of light bulbs and the amount of appliances. This was done by using the input function for the number variables under the light bulb and appliances. 

Problem 3 (6)



   The final problem involved a bit of discrete math. The first command solved for the number of ways to group 4 people. The second command solved for the number of ways to group 28 students in groups of 7. The third command did the same but with consideration of order. The forth command displayed the amount of ways to get hands of 5 in a 52 card deck. The last command displays the chances of getting a royal flush.

MATLAB Day 3


Using The fprintf Command


   The fprintf command allows us to send send the data from our table t the command window and organize it. We are able t create sentences that give more meaning to the information.There are, also, other useful commands, such as the \n command, that we may use within the fprintf command to make adjustments to the format. \n causes the next line below to be used immediately after. This is why each sentence contains it's own row.

Saving Work And M-Script


   Within the command window, we are able to save and load work using the save and load commands.


   We begin to use the M-Script file capability. This capability is the preferred method for doing work on MATLAB. To begin work on the M-Script, we simply choose "new script" on the top left corner of the program and the program opens up a tab in the middle of the program. In this tabe, we are able to write commands similar to that of the command window. The only difference is that once enter is pressed, the command is not initiated. Like programming, the commands are pre-written and when the user wants to implement the commands, he/she just simply presses the "run" tab and this tells MATLAB to run the commands in the command window. We are also able to save any commands that were written in an m-file. This allows the user to save all the work done. Also, like in standard programming practices, notes can be inputted using the percent key. This allows for other users to understand what was done and to better organize the work.

Problem Solving Using M-Script


   We take a look at equations for velocity and acceleration. All we have to do is plug in the value for time to get the speed and acceleration at that very moment.


   Using the same problem on the white board, we create a set of values for time and input it in the equations for velocity and acceleration. We create a table and used the fprintf command to create well documented answer.


   Next, we take a look at the definition of power and its relationship to velocity and force. Using this relationship, we are able to solve for the acceleration at two different velocities.


   We take the power problem to MATLAB. This time, we start to look at the input command. With the input command, we are able to create a program that will calculate the acceleration of a moving object with fixed values for power and mass. The input allows another user to run the program ind input a value, in this case, for velocity. Once the user input the value, the program calculates the acceleration.

Summary:
   We discussed some of the way to display messages on MATLAB using the fprintf command and utilizing these messages to display important information in a legible manner. We also go over some of the techniques for saving and documenting any work on MATLAB. M-Scripts provide a convenient way to keep track and save any work done on MATLAB. We do some simple calculations to get the hang of M-Script and finish off by utilizing the input command. This command allows a user to input different values in a program without messing with the actual program. 



Saturday, September 12, 2015

MATLAB Homework 1


Problem 1


   For the first problem, I declared a bunch of variables that was given for the assignment. Using these variables, I created equations to solve for the total volume and the total surface area. For the surface area, I calculated the surface area of both spheres, then the side of the cylinder, and then the top and bottom of the cylinder. I added the surface areas together but subtracted the surface area of the top and bottom cylinder. This makes sure I don't account for the small area on the sphere that is blocked by the cylinder.

Problem 2


   For the second problem, we had to calculate the temperature of a gas with the given parameters using two different equations. The temperature were similar but not the same. I stated the givens at the top. I found that everything was not in S.I. units but realized that all the units would cancel out in the end leaving a temperature in Kelvin.

Problem 3


    For problem 3, we take a look at a circuit and calculate the response denoted by S. This response has 2 values. After, I created a set of numbers for R and recalculated the response. Some of the numbers shown to be complex numbers.


   I changed the parameters for R to find the smallest value of R that produces a real number for the response. 


   Again, I change the parameters of R. I found that the smallest value for R was 634. But, I realized that I was only checking half of the equation.


I changed the equation and found that it was still at 634. I, also, found that when the R value increases, the S value decreases.

Problem 4


   The forth problem had us evaluating gravitational pull. I stated the values above and created an equation to solve for the force afterward. Then, I created a set of 10 distances that are evenly spaced using the linspace command. I plugged this set of values to the force equation while being mindful that the dot product is needed.

Problem 5


   For problem 5, I am given the amount of miles a group of cars drive a year, the miles they  can drive per gallon, and the rate at which they produce carbon dioxide. I had to find the amount of carbon dioxide produced. This is a simple unit conversion problem. I start with the rate at which cars drove a year and cancel the miles using miles er gallon, leaving me with gallon per yer and cancel out gallons with the rate for pounds of carbon dioxide per gallon. I do the same type of unit conversion for the second part when I solve for the cost of driving each car, but instead of using the amount of carbon dioxide, I use the cost rate.

Friday, September 11, 2015

MATLAB Day 2


Introduction To MATLAB


   We begin experimenting with MATLAB by running some simple calculations. We found that MATLAB can handle these calculation very well, as it follows the order of operations in basic algebra.


   On a whiteboard, we wrote down a calculation for a desired result from the professor. We, then, traded boards with another group. Once our boards were traded, we inputted exactly what was written on the white board. The white board we received did not properly write their arithmetic so that MATLAB could solve it correctly. This demonstrates that MATLAB is sensitive about how calculations are inputted.


   We start taking a look at simple matrices. First, we input a matrix defined as B two different ways. One way, we use commas, while we just use spaces for the second way. Both methods were defined by B. We found that either way produces the same exact matrix. By exact, we mean that the matrix has double data type and takes up the same amount of space. After, we state a 3 by 4 matrix C using the semi colon. Semicolons allow us to move to the next row of a matrix.



   We take a look at some plotting abilities that MATLAB has. By defining x to be a simple matrix set and y to be a different matrix set, we use the plot function to plot a liner graph. The graph displays a bunch of values and a line. After, we go over some of the ways to create good variables. Variables are important because it allows other programmer to get an idea  of what the variable means. In this case, different variables are created to calculate area, surface area and volume. Also, by placing a semi colon at the end of the statement, MATLAB is suppressed by not displaying or restating what was written in the command window.

Less Typing For Less Errors


   We continue practicing by stating variable and solving for a function, but we do so a different way. To avoid any errors in calculation, it is best to do as little typing a possible. We are able to do this if we see any common pieces of arithmetic in the equation and just create a simple variable for that piece, that way, instead of typing so much arithmetic over and over again, we can type something like A. It is also shown that the letter a was used twice. MATLAB recognizes capital and lowercase as different characters and therefore, we are able to use the same letter twice as a variable.


   We go over an ideal gas problem next. The idea here is to do as little typing as possible again. We do this when we have to change the temperature. We do this by pressing the up key on the keyboard. We we press the up key, we are able to refer back to the previous commands we inputted and reuse them. This saves us from make any calculation errors by pressing fewer keys.


   In MATLAB, we can generate a set of numbers using different commands. The first command uses colons in between numbers. For example, by typing 0:2:10, we are able to generate a set of numbers between 0 and 10 going by twos. This command requires the first number first, the last number last, and the type of arrangement for the the numbers in between in the middle. We also use a command called linspace. This command allows us to choose the first number, then the last number, and the spacing of these numbers. For example, the numbers between 10 and 20 with five spaces in between is generated in the picture. This command calculates the numbers in between and these numbers are evenly spaced. The final command is the logspace command. This commant works similar to the linspace, excect it deals with orders of ten. The first input would be the first power of ten. The second input would be the second power of ten. We are only putting the powers when inputting. For example, if we want our first number to be 100, we input 2 for 10 to the power of 2. The last number, like linspace, is the spacing in between the two values. Also, we are able to clear all of the previous data from before by typing "clear" and we are able to work on a fresh page by typing "clc".

Matrix Algebra



MATLAB is great when it comes to matrix calculations. When stating 2 matrices, it is important to remember the principles for matrix algebra. For example, when we multiply two matrices together, we have to distinguish it by telling MATLAB it is a dot product calculation. We can do this by putting a period in front of the multiplication symbol. MATLAB, then does the calculations. If this is not done, MATLAB will state an error. We get some practice with matrix calculations by knowing when to implement the period.


   MATLAB can also transpose a matrix. In order to transpose a matrix, a apostrophe needs to be put immediately after the variable that represents the matrix. This is useful, not only for linear algebra aspects, but for creating neat tables.

Computational Exercises



   This exercise was in reference to the tragic event that took place on mars, when the Mars Climate Orbiter burned up in the orbit of Mars due to conversion errors, costing a great amount of money. We use our white boards to plan how we would tackle the problem on MATLAB. We converted pounds to newtons and created a table for comparison. This demonstrates how easily this could have been avoided. MATLAB was able to convert using minimal commands. Also,, MATLAB has the ability to guess what commands a person really wanted but did not get because of typo errors. Not only that, it retypes everything and all a person has to do is press enter.



   The final problem to solve involved a conversion between miles per hour to velocity. On our white board, we come up with a way to solve the problem with the givens. We calculate the answer by hand and compare this calculation to the answer on MATLAB. With the given information, we had to find the coefficient of drag with at a certain speed and at many different speeds. This was done fairly easily with MATLAB.

Summary:
   Today, we went over some of the basic computational aspects of MATLAB. We found that MATLAB follows the order of operations. We, also, found that MATLAB contains the ability to plot a graph. MATLAB specializes with matrices and we saw a glimpse of how powerful MATLAB is when it comes to matrix calculations by crating and transposing matrices, multiplying them using the dot product, and even coming up with a set of numbers. We are able to utilize MATLAB to make conversions between units