CS 170 – Intro to Scientific and engineering Programming
description
Transcript of CS 170 – Intro to Scientific and engineering Programming
![Page 1: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/1.jpg)
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING
![Page 2: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/2.jpg)
Cold coffee
![Page 3: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/3.jpg)
An experimentTime(min) Temperature (°C)
0 93.51 90.62 87.73:30 83.97 76.610 71.318 61.526 65.834 49.252 41.9113 29.1166 25.5191 25.0
![Page 4: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/4.jpg)
Plotting data: Round 1times = [0 1 2 3.5 7]temps = [93.5 90.6 87.7 83.9 76.6]
plot(times, temps)
![Page 5: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/5.jpg)
Exercise• Help your poor Greek professor!
Write an assignment statement to convert temperature from Celcius to Fahrenheit*
*Hint: F = 32 + (9/5)C
![Page 6: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/6.jpg)
Element by element>> sample1 = [3.2 1.1 4.7 2.1 6.2]>> sample2 = [3.0 1.2 4.4 2.4 6.1]>> average = (sample1 + sample2) / 2
How about multiplication?
![Page 7: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/7.jpg)
SequencesUse colon notation>> nums = 1:2:9
Use the linspace function:linspace(start, end, num)
![Page 8: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/8.jpg)
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’)
![Page 9: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/9.jpg)
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
whereT(t) is object temperature at time tT(0) is initial temperature of objectTenv is environment temperaturek depends on material (for coffee k ~ 0.0426)
![Page 10: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/10.jpg)
Coffee Break!
![Page 11: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/11.jpg)
QUESTIONS??
![Page 12: CS 170 – Intro to Scientific and engineering Programming](https://reader036.fdocuments.us/reader036/viewer/2022062315/56816188550346895dd1227e/html5/thumbnails/12.jpg)
Resources• Lecture slides CS112, Ellen Hildreth,
http://cs.wellesley.edu/~cs112/