Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cold Coffee Vectors and plotting

Similar presentations


Presentation on theme: "Cold Coffee Vectors and plotting"— Presentation transcript:

1 Cold Coffee Vectors and plotting
- so far, learned how to create variables to store single value that may be a number or string of text - in most applications, there’s a collection of data to work with - today, learn how to use vectors to store collection of values, and how to visualize data with plotting

2 Cold, funny-tasting coffee
Time(min) Temperature (°C) 0 93.5 1 90.6 2 87.7 3: 7 76.6 - consider problem of coffee cooling in cup – allow myself one cup of copy a day, so pour first thing in morning, then savor throughout morning – means frequent runs to microwave to reheat. Seems to cool very rapidly… - suppose want to examine this more closely: how fast does it cool? Place thermometer in cup, measure temp over time. Lots of data here - suppose want to visualize as graph. Could create lots of variables to hold it all: time0 = 0 temp0 = 93.5 time1 = 1 temp1 = … but somehow this doesn’t seem like a very good idea, very cumbersome - be nice if could have single variable to store all times, another to store all temps Vectors

3 Vectors A vector is an ordered collection of numbers
times = [ ] temps = [ ] times 1 2 3.5 7 - can store collection of numbers in vector - several ways to specify collection of numbers want to store in vector – one way is to list values inside brackets - for simplicity, just wrote first 5 values of times/temps - text - values separated by commas, but not necessary - what kind of statement here? assignment statement, (variable name = expression) - creating vectors named times and temps, each storing 5 values - visualize as sequence of connected boxes storing values in order typed - happens that times increasing order, temps decreasing, but contents can be any collection of numbers – stored in order entered in assignment statement - so having constructed vector of values, what do with it? temps 93.5 90.6 87.7 83.9 76.6 Vectors

4 Plotting data: Round 1 Create a 2D graph with the plot function
times = [ ] temps = [ ] plot(times, temps) vector of x coordinates - create 2D graph with plot – many ways to call plot, here provided two vectors as inputs - MATLAB assumes 1st vector stores all x coords, 2nd vector all y coords, pairs x’s and y’s, and draws a line connecting 5 points - important note: vectors storing x and y coordinates must be same size! If not, generate an error - where does MATLAB actually display graph? - MATLAB: >> times >> temps >> plot(times, temps) figure window is opened, labeled Figure 1, displaying graph - automatically determines range of values on axes and places tickmarks at evenly spaced intervals - later, learn how to control appearance of plot - done admiring, close window with close box vector of y coordinates Vectors

5 Computations on collections of data
Perform the same arithmetic operation on each element of vector >> times = [ ] >> times = 60 * times times 1 2 3.5 7 - another benefit of storing collection of numbers in vector – perform computation on collection all at once - have times in minutes, want to store as seconds - multiply each value by 60 - MATLAB: just write 60*times, and performs same arithmetic operation on each element of a vector - in same way that you write arithmetic expressions with single numbers, can write expressions with names of vectors, same operation applied to each element of vector times 60 120 210 420 Vectors

6 Time-out exercise Write an assignment statement to convert temperature from Celcius to Fahrenheit* >> temps = [ ] >> ... temps 93.5 90.6 87.7 83.9 76.6 - temps = 32 + (9/5)*temps - do this and plot data again - MATLAB automatically adjusts the range of values on the axes temps 200.3 195.08 189.86 183.02 169.88 * Hint: F = 32 + (9/5)C Vectors

7 More vector magic Perform element-by-element computations on two vectors, e.g. average the two data samples* >> sample1 = [ ] >> sample2 = [ ] sample1 3.2 1.1 4.7 2.1 6.2 - suppose have two data samples, each providing measurements of 5 different quantities, and want to average two first values, average second, etc. - average = (sample1 + sample2)/2 - use same statement to compute element-by-element average two vectors sample2 3.0 1.2 4.4 2.4 6.1 * Hint: How do you calculate the average of two single numbers sample1 and sample2? Vectors

8 The envelope please Element-by-element average:
>> sample1 = [ ] >> sample2 = [ ] >> average = (sample1 + sample2) / 2 sample1 3.2 1.1 4.7 2.1 6.2 - two vectors must be same size, and produce resulting vector of same size – automatically create vector as result (rather than number), because asking to process values that are vectors sample2 3.0 1.2 4.4 2.4 6.1 average 3.1 1.15 4.55 2.25 6.15 Vectors

9 Let’s try that one again
Perform element-by-element computations on two vectors, e.g. multiply corresponding values in the two data samples* >> sample1 = [ ] >> sample2 = [ ] sample1 3.2 1.1 4.7 2.1 6.2 - we’re on a roll – try another arithmetic operation between two vectors: let’s try multiplying: MATLAB: >> sample1 * sample2 Error! Inner matrix dimensions must agree! what’s going on here? MATLAB is attempting matrix multiplication - to perform element-by-element multiplication of two vectors, use .* >> products = sample1 .* sample2 ( …) - use ./ for division and .^ for powers (e.g. squares = sample2.^2 ( …) - do we need a .-? (no - matrix subtraction, like matrix addition, is defined as element by element) - again, vectors must be same size! sample2 3.0 1.2 4.4 2.4 6.1 * oops… Vectors

10 Still more magic Compute a single value from the contents of a vector
>> sample1 = [ ] >> totalSum = sum(sample1) >> totalProd = prod(sample1) >> sampleSize = length(sample1) >> minValue = min(sample1) >> maxvalue = max(sample1) >> avgValue = mean(sample1) sample1 3.2 1.1 4.7 2.1 6.2 - built-in MATLAB functions that combine all values in vector, do what expect from name - suppose didn’t have the mean function? how calculate average value from other functions? >> avgValue = sum(sample1) / length(sample1) Vectors

11 Creating sequences Often you will want to create a sequence of evenly spaced numbers* Use colon notation >> nums = 1:2:9 end start step - often, need arises to create sequence evenly spaced numbers from scratch (trust me…) - use colon notation to describe sequence: start, step, end – evaluation of expression creates set of numbers and stores them in vector - variations: step is optional - if omitted, step is assumed to be 1: >> nums = 5:9  [ ] - negative step: >> nums = 20:-3:7 [ ] (go as far as you can) - floating point: >> nums = 4:1.5:10 - expressions with calculations: >> angles = 0:pi/4:2*pi nums 1 3 5 7 9 * Trust me, you really will want to do this someday! Vectors

12 Creating evenly spaced sequences
Use the linspace function: linspace(start, end, num) to create a vector of num evenly spaced numbers from start to end >> nums = linspace(0, 10, 5) - Given such a valuable thing to do (trust me!) - second way is to use linspace function: >> nums = linspace(1, 1000, 5000) <-- do this lots of printout! This is when you appreciate that can suppress the printout of the value produced by a statement by placing a semi-colon (;) at end of the statement nums 0.0 2.5 5.0 7.5 10.0 * Write an assignment statement that stores a sequence of 5000 evenly spaced numbers from 1 to 1000 Vectors

13 Plotting data: Round 2 Adjust the appearance of lines and markers with a third input to the plot function: plot(times,temps,‘g--*’); Third input is a string of 1 to 4 characters in single quotes: color: b g r c m y k w marker: . o x + * s d v ^ < > p h linestyle: : Add text labels: xlabel(‘Time, minutes’) ylabel(‘Temperature, Celcius’) title(‘Cooling a Cup of Coffee’) Our original plot of the temperature vs. time data was a little plain - fortunately we can control its appearance in many ways - here are two things we can do (will learn lots of other ways to enhance figures in lab this week) Vectors

14 Newton’s Law of Cooling
An object’s rate of cooling depends on the difference between object’s temperature and ambient temperature Temperature decreases exponentially over time According to Newton: T(t) = Tenv + (T(0) - Tenv)e-kt where T(t) is object temperature at time t T(0) is initial temperature of object Tenv is environment temperature k depends on material (for coffee k ~ ) - all know about Newton and falling apple - one other accomplishment is to formulate law that describes how rapidly materials cool - suppose want to compare our measured results with what Newton predicts - write code that performs computations in this equation and generates sequence times and temps according to model, use plot to add graph to figure and compare Vectors

15 Coffee break % cooling.m
% compares the direct measurement of cooling of coffee with % the cooling rate predicted by Newton’s Law of Cooling timeData = [ ]; tempData = [ ]; plot(timeData, tempData, ‘b-*’) xlabel(‘Time, minutes’) ylabel(‘Temperature, degrees Celcius’) title(‘Cooling a Cup of Coffee’) timeSamples = 0:5:200; tempLaw = * exp( * timeSamples); hold on plot(timeSamples, tempLaw, ‘g--o’) hold off - develop tempLaw calculations on board… Vectors

16 Ta Da! Vectors

17 Exercise: Center of Mass
% coordinates and masses of particles xcoords = [ ]; ycoords = [ ]; masses = [ ]; % calculate the center of mass of the particles xcenter = ycenter = % print center of mass coordinates using disp - split into small groups and work out solution on the board - calculate xcenter/ycenter with single statement Vectors


Download ppt "Cold Coffee Vectors and plotting"

Similar presentations


Ads by Google