TRAIN Scholarship Week 2
Genaro Rivera
September 19, 2022
Train Scholarship Personal
Research MATLAB coding
Script 1: Plotting
Two Lines on One Graph
For my first script this week I graphed the momentum and kinetic energy of a car as a function of time. First, I gave a descriptive title and description. Additionally, I labeled the section headers. Secondly, I defined the parameters. The mass (m = 907 kg) and the minimum and maximum velocities are givens. Thirdly, I performed computations. I computed the x-values as a row vector starting from the minimum velocity and ending with the maximum velocity in increments of one. Importance notice goes to the fact that I labeled the minimum velocity (minimum x valued) and maximum velocity (maximum x values) as variables. Since I set each equal to 0 and 8, MATLAB recognizes the word as numerical values and computes them as x = [0:8]. For kinetic energy, I used the physics formula E_k = (1/2) mv. ^2. The reason for a ‘.’ after the v and before the exponent symbol is because any matrix raised to a power is undefined according to college algebra rules. Because of this, I must use a dot. The dot computes the matrix to an array and computes an element-by-element operation. In other words, each element is raised to a power of 2 individually rather than multiply the whole matrix by itself. For momentum, I used the physics formula L = mv. Fourthly, I used the plot function to plot the graph. I chose the plot function over the fplot or ezplot functions because fplot and ezplot create symbolic equations with more points at the lines of curvature whereas the plot function displays a discrete set of points (AKA ordered pairs) which is useful for my data that consists of 9 ordered pairs. Remember, MATLAB graphs discretely and connects the points with a line. The graph is not continuous everywhere despite the appearance. The general formula for plotting two lines on one graph is plot(x, y, x, y) You can add text in single quotes which indicates a change in visual display. Here, ‘b—’ means to graph a blue dashed line and ‘r’ means to graph a red line where MATLAB defaults to the solid line if you do not follow the letter with a dash of any kind. Lastly, I labeled the x values using xlabel(), and I labeled the legend using legend(). Here, I needed to include single quotes to distinguish from a string (simple text) from variables containing numerical values. I moved the legend to the top left by typing location and northwest. To change the font size I typed “ ‘FontSize’ ” with 18 to indicate the exact size. In conclusion, I learned how to plot two lines on one graph, and how to modify the graph.
Script 1: Plotting Two Lines on Two Different Graphs
Still
on my first script, I modified the displacement of the graphs. Instead of one
graph, I showed two. First, I used the subplot function subplot (2,1,1). The 2,1
indicates that I want two rows and one column creating a 2 by 1 matrix. If the
dimensions of the subplot increase, you read it from left to right like a book
regarding the locations. For example, if I use subplot (3,7,5) it would create a
3 by 7 matrix where I want to access the 5th element. Reading like a
book (left to right) the 5th element wold be in row 1 column 5). For
this, the 1 at the end of the command indicates that I want to access the first
block of this matrix. The plot function displays the graph there where I use
xlabel and ylabel to label the graphs. I repeat the same steps for the second
graph by accessing the second element using subplot (2,1,2). In conclusion, I
learned to plot two lines in one and two separate graphs and the different ways
to display the information.
Script 2: Plotting CC
For my second script I wrote a code that displays a plot
resembling the letters SCC. Firstly, I set the ordered pairs as row vectors.
The corresponding elements represent points on a graph. For example, C_x and
C_y have points (7,10) (0,10) (0,0) (7,0). Secondly, I labeled figure 1 to
ensure all points for all letters appear on the same figure. Notice how I have
two sets of C_x and C_y with different shift coordinates. The reason why MATLAB
distinguishes the two, despite the same names, is the hold on function. The
hold on function ensures no points are overwritten with others; it saves
everything in the code that comes before it, so it saved the first set of C_x
and C_y. The axis command sets the x-range and y-range: axis ([x min, x max, y
min, y max]). Thirdly, I used the shift command to shift the letter c to the
right appropriately. I added the x and y shifts to their respective matrices so
that every element is added by the labeled x and y shifts of 21 and 1. Lastly,
I plotted the letter. The ‘k-‘ made the outline black and the ‘Linewidth’, 7
made the outline have a width of 7. The ‘g-‘ made the letter green. The
procedure was the same for the second letter c with different coordinates for
the shift command and a different color (yellow: ‘y-‘). In conclusion I graphed
the two-letter c’s through a straightforward procedure.
Script 2: Plotting S
On the same code, I graphed
the letter S. Here the procedure was the same, but the row vectors were
different. Here, I learned that the order you place the ordered pairs matters.
For example, with C, which I got lucky with on the first try, you start on the
top right, go to top left, go to bottom left, and finish with the bottom right
(MATLAB connects the points in the order they appear). For S, you start on top
right, go to top left, go to mid to top left, go to mid to top right, go to
bottom right, and finsih on the bottom left. The coloring and line width was
the same as the first letter c. In conclusion I graphed the letter S in the same
way I graphed the two letters c’s.
Great work again this week, Genaro. Keep it up!
ReplyDelete